SpringBoot集成Spring Data Redis
实现步骤:
1. 添加Redis起步依赖
<!--spring data redis 依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2. 在application.properties中配置redis端口、地址
# Redis 配置(不填也是可以的) spring.redis.host=localhost spring.redis.port=6379
3. 注入RedisTemplate操作Redis缓存查询所有用户数据
@Autowired private RedisTemplete redisTemplete @Test public void testRedis() throws JsonProcessingException { String users = (String)redisTemplate.boundValueOps("user.findAll").get(); if (users == null) { List<User> userList = userMapper.queryUserList(); ObjectMapper jsonFormat = new ObjectMapper(); users = jsonFormat.writeValueAsString(userList); redisTemplate.boundValueOps("user.findAll").set(users); System.out.println("==============从数据库中获取用户数据==================="); }else { System.out.println("==============从Redis缓存中获取用户数据==================="); } System.out.println(users); }