集成方法
一、创建一个SpringBoot项目
(1)
(2)
二、观察新的springBoot项目的依赖
1、学习redis依赖底层
<!--操作Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
点击去底层Jedis,被替换成了letuce,后边再配置连接池也要记得配置lettuce而不要配置Jedis
学习jedis为什么被取代?
jedis:采用直连的方式,多个线程操作的话,是不安全的,如果想要避免不安全,就得使用Jedis pool连接池技术,类似于BIO模式
lettuce:则是采用了netty,实例可以在多个线程中进行共享,不存在线程不安全的情况,可以减少线程数量,类似于NIO模式!
2、分析SpringBoot项目中的redis自动配置类
观察绑定的配置文件,学习在application.properties中可以配置哪些信息
三、编写application.properties,配置Redis
##Redis 配置
spring.redis.database=13 # Redis数据库索引(默认为0)
spring.redis.host=127.0.0.1 # Redis服务器地址
spring.redis.port=6379 # Redis服务器连接端口
spring.redis.password=123456 # Redis服务器连接密码
spring.redis.jedis.pool.max-active=8 # 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-idle=8 # 连接池中的最大空闲连接
spring.redis.jedis.pool.min-idle=0 # 连接池中的最小空闲连接
spring.redis.timeout=5000 # 连接超时时间(毫秒)
四、测试
@SpringBootTest
class Redis01SpringbootApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
//1、获取Redis数据库连接
RedisConnectionFactory connectionFactory = redisTemplate.getConnectionFactory();
RedisConnection connection = connectionFactory.getConnection();
connection.flushDb();
connection.flushAll();
//2、该模板中有很多的方法,都是对应Jedis操作不同的数据类型
/*opsForValue():操作字符串
* opsForList():操作列表
* opsForSet():操作set集合
* opsForZSet()
* opsForHash()
* opsForGeo()
* opsForHyperLogLog()
* ...*/
redisTemplate.opsForValue().set("name","zhangsan");
System.out.println(redisTemplate.opsForValue().get("name"));
//3、除了操作基本的数据,还有一些常用的执行命令,比如操作事务
redisTemplate.exec(); //执行
redisTemplate.discard(); //放弃事务
}
}
控制台可以正常打印
五、我配置了Redis本地的登陆密码
如果登陆本地的redis客户端需要密码,那么就输入
auth 123456
自定义RedisTemplate
一、首先需要了解,在Redis存储对象的时候,需要将对象序列化,如果不序列化,那么就会报错
两个办法:
1、实体类上实现序列化接口
2、直接往Redis中存储一个Json字符串
但是如果使用实现序列化接口或者存Json字符串,都是底层通过JDK来实现序列化的,在Redis中会转译,不是我们想要的字符串key,所以这个时候有必要自己重写一个RedisTemplate模板
二、自定义Redistemplate
该模板可以直接使用!!!!!!
@Configuration
public class RedisConfig {
//自定义配置RedisTemplate的固定模板,将来企业直接使用!!!!!!
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//为了方便,我们一般都是使用string,object的配置
RedisTemplate<String, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
//配置具体的序列化方式
//Jackson的序列化配置
Jackson2JsonRedisSerializer<Object> objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
objectJackson2JsonRedisSerializer.setObjectMapper(om);
//String的序列化配置
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用String的序列化
template.setKeySerializer(stringRedisSerializer);
//hash的key也采用String方式序列化
template.setHashKeySerializer(stringRedisSerializer);
//value的序列化,采用jackson的方式序列化
template.setValueSerializer(objectJackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
三、配置完自己的redistemplate,要修改自动装配的这个模板的位置,要换成我们自己的
@SpringBootTest
class Redis01SpringbootApplicationTests {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
public void test() throws JsonProcessingException {
User user = new User("zhangsan", 4);
String jsonUser = new ObjectMapper().writeValueAsString(user);
redisTemplate.opsForValue().set("user",user);
System.out.println(redisTemplate.opsForValue().get("user"));
}
四、这样,我们启动测试类,打开Redis客户端,就能查到正确的字符串key,而不是经过JDK转译后乱的字符串了
至此,你已经掌握了Spring集成Redis的方法,后续还会持续更新,敬请期待!