springboot整合redis


步骤:

一、引入redis的starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

二、在springboot配置文件中配置连接redis的主机地址

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.1.6:3306/spring_cache?useUnicode=true\
  &characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
mybatis.configuration.map-underscore-to-camel-case=true
logging.level.com.xj.springboot.mapper=debug
spring.redis.host=192.168.1.6

三、使用

当我们导入了redis的starter后,springboot中的自动配置类RedisAutoConfiguration就生效了,RedisAutoConfiguration自动配置类会自动往容器中添加两个组件:StringRedisTemplate和RedisTemplate。我们要使用的话直接通过Autowired注解将这两个组件注入就可以使用了。

@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
  • StringRedisTemplate:用于操作key和value都是字符串的数据。
  • RedisTemplate:用于操作key和value都是对象的数据。

 注意:使用RedisTemplate操作对象时,对象必须是可序列化的,即需要实现序列化接口。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM