springboot整合SpringDataRedis
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.x</groupId>
<artifactId>21-spring-boot-view-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- spring-boot启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- redis启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- junit启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
/**
* 完成对redis的整合的一些配置
*/
@Configuration
public class RedisConfig {
/**
* 1、创建JedisPoolConfig对象,在该对象中完成一些连接池的配置
*/
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
//不设置将走默认!!!
//最大空闲数
config.setMaxIdle(10);
//最小空闲数
config.setMinIdle(5);
//最大连接数
config.setMaxTotal(20);
return config;
}
/**
* 2、创建JedisConnectionFactory对象,配置redis连接信息
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
JedisConnectionFactory factory = new JedisConnectionFactory();
//关联连接池的配置对象
factory.setPoolConfig(config);
//配置连接redis的信息
//主机地址
factory.setHostName("192.168.0.106");
//端口
factory.setPort(6379);
return factory;
}
/**
* 3、创建RedisTemplate:用于执行redis操作的方法
*/
/**
* @param factory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory){
RedisTemplate<String, Object> template = new RedisTemplate<>();
//关联
template.setConnectionFactory(factory);
//为key设置序列化器
template.setKeySerializer(new StringRedisSerializer());
//为value设置序列化器
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=App.class)
public class RedisTest {
@Autowired
private RedisTemplate<String, Object> template;
//添加一个字符串
@Test
public void testSet() {
this.template.opsForValue().set("key", "hello");
}
//取出字符串
@Test
public void testGet() {
String value = (String)this.template.opsForValue().get("key");
System.out.println(value);
}
}
提取redis的配置信息到文件
@ConfigurationProperties |
---|
会对配置文件中前缀相同的信息创建一个实体,后缀为其属性名。 |
例如:@ConfigurationProperties(prefix="spring.redis.pool") 获取的就是配置文件中以spring.redis.pool 为前缀的信息。 |
注意:配置文件组成为前缀.后缀 ,这里前缀可以自定义,但后缀一定要对应配置类中的方法名。例如:max-idle 对应config.setMaxIdle(10); !!! |
- 在
src/main/resources
下新建全局配置文件application.properties
#Redis
spring.redis.pool.max-idle=10
spring.redis.pool.min-idle=5
spring.redis.pool.max-total=20
spring.redis.hostName=192.168.0.106
spring.redis.port=6379
/**
* 完成对redis的整合的一些配置
*/
@Configuration
public class RedisConfig {
/**
* 1、创建JedisPoolConfig对象,在该对象中完成一些连接池的配置
* @ConfigurationProperties:会对配置文件中前缀相同的信息创建一个实体,后缀为其属性名。
*/
@Bean
@ConfigurationProperties(prefix="spring.redis.pool")
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
/**
* 2、创建JedisConnectionFactory对象,配置redis连接信息
*/
@Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
JedisConnectionFactory factory = new JedisConnectionFactory();
//关联连接池的配置对象
factory.setPoolConfig(config);
return factory;
}
/**
* 3、创建RedisTemplate:用于执行redis操作的方法
*/
/**
* @param factory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory){
RedisTemplate<String, Object> template = new RedisTemplate<>();
//关联
template.setConnectionFactory(factory);
//为key设置序列化器
template.setKeySerializer(new StringRedisSerializer());
//为value设置序列化器
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}
存取Java对象
- 创建实体类,该类需要实现
Serializable
接口。
public class Users implements Serializable {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + "]";
}
}
//添加User对象,需要序列化
@Test
public void testSetUser() {
Users user = new Users();
user.setId(1);
user.setName("张三");
//重新设置序列化器,这一步是必须的
this.template.setValueSerializer(new JdkSerializationRedisSerializer());
this.template.opsForValue().set("user", user);
}
//获取User对象,需要反序列化
@Test
public void testGetUser() {
//重新设置序列化器,这一步是必须的
this.template.setValueSerializer(new JdkSerializationRedisSerializer());
Users user = (Users)this.template.opsForValue().get("user");
System.out.println(user);
}
以JSON格式存取Java对象
//以JSON格式添加User对象,需要序列化
@Test
public void testSetUserByJSON() {
Users user = new Users();
user.setId(2);
user.setName("李四");
//重新设置序列化器,这一步是必须的
this.template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
this.template.opsForValue().set("user1", user);
}
//以JSON格式添加User对象获取User对象,需要反序列化
@Test
public void testGetUserByJSON() {
//重新设置序列化器,这一步是必须的
this.template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
Users user = (Users)this.template.opsForValue().get("user1");
System.out.println(user);
}