Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch, Solr和Cassandra。
项目结构
pom.xml依赖
Spring Boot提供了Jedis客户端库的基本自动配置 和Spring Data Redis提供的抽象。
可以通过引入spring-boot-starter-data-redis
来配置依赖关系。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lf</groupId>
<artifactId>cache-RedisTemplate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cache-RedisTemplate</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
参数配置application.peoperties
# 驱动配置信息
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = adminter
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis
mybatis.type-aliases-package=com.lf.entity
mybatis.mapper-locations=classpath:mapper/*.xml
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
spring.profiles.active=dev
其中spring.redis.database的配置通常使用0即可,Redis在配置的时候可以设置数据库数量,默认为16,可以理解为数据库的schema
StringRedisTemplate
你可以注入一个自动配置的RedisConnectionFactory,StringRedisTemplate或普通的跟其他Spring Bean相同的RedisTemplate实例。默认情况下,这个实例将尝试使用localhost:6379连接Redis服务器。
package com.lf.redisTemplate;
import com.lf.Application;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Created by LF on 2017/4/20.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class RedisTemplateTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void context() {
// 保存字符串
redisTemplate.opsForValue().set("aaa", "111");
Assert.assertEquals("111", redisTemplate.opsForValue().get("aaa"));
}
}
RedisTemplate
StringRedisTemplate对象进行Redis的读写操作,该对象从命名中就可注意到支持的是String类型。如果有使用过spring-data-redis的开发者一定熟悉RedisTemplate<K, V>
接口,StringRedisTemplate就相当于RedisTemplate<String, String>
的实现。
除了String类型,实战中我们还经常会在Redis中存储对象,这时候我们就会想是否可以使用类似RedisTemplate<String, City>
来初始化并进行操作。但是Spring Boot并不支持直接使用,需要我们自己实现RedisSerializer接口来对传入对象进行序列化和反序列化,下面我们通过一个实例来完成对象的读写操作。
- 创建要存储的对象:
package com.lf.entity;
import java.io.Serializable;
/**
* Created by LF on 2017/4/18.
*/
public class City implements Serializable{
private String id;
private String name;
private String state;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
- 实现对象的序列化接口
package com.lf.config;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.util.ObjectUtils;
/**
* Created by LF on 2017/4/20.
*/
public class RedisObjectSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter();
@Override
public byte[] serialize(Object o) throws SerializationException {
if (o==null)
return new byte[0];
return serializer.convert(o);
}
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
if (ObjectUtils.isEmpty(bytes))
return null;
return deserializer.convert(bytes);
}
}
- 配置针对City的RedisTemplate实例
package com.lf.config;
import com.lf.entity.City;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Created by LF on 2017/4/20.
*/
@Configuration
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public RedisTemplate<String, City> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, City> template = new RedisTemplate();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new RedisObjectSerializer());
return template;
}
}
- 完成了配置工作后,编写测试用例实验效果
package com.lf.redisTemplate;
import com.lf.Application;
import com.lf.entity.City;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Created by LF on 2017/4/20.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class RedisTemplateTest {
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private RedisTemplate<String, City> cityRedisTemplate;
@Test
public void context() {
// 保存字符串
redisTemplate.opsForValue().set("aaa", "111");
Assert.assertEquals("111", redisTemplate.opsForValue().get("aaa"));
}
@Test
public void test() throws Exception {
City city = new City();
city.setId("1");
city.setName("name");
cityRedisTemplate.opsForValue().set("1",city);
City o = cityRedisTemplate.opsForValue().get("1");
Assert.assertEquals("name", o.getName());
}
}
注解支持
spring-boot-starter-data-redis
实现了Cache和CacheManager等,不用配置了。
只需要很少的配置。
开启缓存支持
添加@EnableCaching
package com.lf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableCaching//开启缓存支持
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建实体对象
一定要实现序列化
package com.lf.entity;
import java.io.Serializable;
/**
* Created by LF on 2017/4/18.
*/
public class City implements Serializable{
private String id;
private String name;
private String state;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
数据库访问
package com.lf.mapper;
import com.lf.entity.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* Created by LF on 2017/4/18.
*/
@Mapper
public interface CityDao {
@Select("select * from city")
List<City> getAll();
}
服务接口的定义
package com.lf.serive;
import com.lf.entity.City;
import java.util.List;
/**
* Created by LF on 2017/4/18.
*/
public interface CityService {
List<City> getAll();
}
服务的实现
package com.lf.serive.impl;
import com.lf.entity.City;
import com.lf.mapper.CityDao;
import com.lf.serive.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by LF on 2017/4/19.
*/
@Service
@CacheConfig(cacheNames = "base-city")
public class CityServiceImpl implements CityService {
@Autowired
private CityDao cityDao;
@Override
@Cacheable
public List<City> getAll() {
return cityDao.getAll();
}
}
测试用例
package com.lf;
import com.lf.serive.CityService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class RedisApplicationTests {
@Autowired
private CityService cityService;
@Test
public void contextLoads() {
cityService.getAll();
cityService.getAll();
}
}
缓存注解的详细介绍查看,http://jinnianshilongnian.iteye.com/blog/2001040