9.Spring Boot整合 Spring Data Redis

1.Spring Boot 整合 Spring Data Redis

Spring Data Redis 是属于 Spring Data 下的一个模块。作用就是简化对于 redis 的操做

1.1修改 pom 文件添加 Spring Data Redis 的坐标

<!-- Spring Data Redis 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

1.2编写 Spring Data Redis 的配置类(重点)

2.2编写 Spring Data Redis 的配置类(重点)
/**
* 完成对 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.70.128");
//端口
factory.setPort(6379);
return factory;
}
/**
* 3.创建 RedisTemplate:用于执行 Redis 操作的方法
*/
@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;
}
}

1.3编写测试代码,测试整合环境

1.4编写测试类

/**
* Spring Data Redis 测试
*
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=App.class)
public class RedisTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 添加一个字符串
*/
@Test
public void testSet(){
this.redisTemplate.opsForValue().set("key", "北京尚学堂");
}
/**
* 获取一个字符串
*/
@Test
public void testGet(){
String value =
(String)this.redisTemplate.opsForValue().get("key");
System.out.println(value);
}
}

2. 提取 redis 的配置信息

2.1在 src/main/resource/ 目 录 下 新 建 一 个 配 置 文件:application.properties
spring.redis.pool.max-idle=10
spring.redis.pool.min-idle=5
spring.redis.pool.max-total=20
spring.redis.hostName=192.168.70.128    //redis的ip地址
spring.redis.port=6379     //redis的端口号

2.2修改配置类

/**
* 完成对 Redis 的整合的一些配置
*
*
*/
@Configuration
public class RedisConfig {
/**
* 1.创建 JedisPoolConfig 对象。在该对象中完成一些链接池配置
* @ConfigurationProperties:会将前缀相同的内容创建一个实体。


*/
@Bean
@ConfigurationProperties(prefix="spring.redis.pool")
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig config = new JedisPoolConfig();
/*//最大空闲数
config.setMaxIdle(10);
//最小空闲数
config.setMinIdle(5);
//最大链接数
config.setMaxTotal(20);*/
System.out.println("默认值:"+config.getMaxIdle());
System.out.println("默认值:"+config.getMinIdle());
System.out.println("默认值:"+config.getMaxTotal());
return config;
}
/**
* 2.创建 JedisConnectionFactory:配置 redis 链接信息
*/
@Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisConnectionFactory
jedisConnectionFactory(JedisPoolConfig config){
System.out.println("配置完毕:"+config.getMaxIdle());
System.out.println("配置完毕:"+config.getMinIdle());
System.out.println("配置完毕:"+config.getMaxTotal());
JedisConnectionFactory factory = new JedisConnectionFactory();
//关联链接池的配置对象
factory.setPoolConfig(config);
//配置链接 Redis 的信息
//主机地址
/*factory.setHostName("192.168.70.128");
//端口
factory.setPort(6379);*/
return factory;
}
/**
* 3.创建 RedisTemplate:用于执行 Redis 操作的方法
*/
@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;
}
}

2.3Spring Data Redis 操作实体对象

2.4创建实体类
public class Users implements Serializable {
private Integer id;
private String name;
private Integer age;

2.5测试代码
/**

* 添加 Users 对象
*/
@Test
public void testSetUesrs(){
Users users = new Users();
users.setAge(20);
users.setName("张三丰");
users.setId(1);
//重新设置序列化器
this.redisTemplate.setValueSerializer(new
JdkSerializationRedisSerializer());
this.redisTemplate.opsForValue().set("users", users);
}
/**
* 取 Users 对象
*/
@Test
public void testGetUsers(){
//重新设置序列化器
this.redisTemplate.setValueSerializer(new
JdkSerializationRedisSerializer());
Users users =
(Users)this.redisTemplate.opsForValue().get("users");
System.out.println(users);
}

5 Spring Data Redis 以 JSON 格式存储实体对象

/**
* 基于 JSON 格式存 Users 对象
*/
@Test
public void testSetUsersUseJSON(){
Users users = new Users();
users.setAge(20);
users.setName("李四丰");
users.setId(1);
this.redisTemplate.setValueSerializer(new
Jackson2JsonRedisSerializer<>(Users.class));
this.redisTemplate.opsForValue().set("users_json", users);
}
/**
* 基于 JSON 格式取 Users 对象
*/
@Test
public void testGetUseJSON(){
this.redisTemplate.setValueSerializer(new
Jackson2JsonRedisSerializer<>(Users.class));
Users users =
(Users)this.redisTemplate.opsForValue().get("users_json");
System.out.println(users);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值