SpringBoot集成Spring Data Redis
Implementation steps:
1. Add start dependent Redis
<-! Spring data redis dependent -> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
Configuring redis port address in the application.properties
# Redis configuration (not fill are possible) spring.redis.host=localhost spring.redis.port=6379
3. Fill RedisTemplate operating Redis cache query all user data
@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 ( "Get ============== =================== user data from the database"); }else { System.out.println ( "Get ============== =================== user data from the cache Redis"); } System.out.println(users); }