1 添加redis支持
在pom.xml中添加
1. <dependency>
2. <groupId>org.springframework.boot</groupId>
3. <artifactId>spring-boot-starter-redis</artifactId>
4. </dependency>
1.
2.
3.
4.
2 redis配置
1. package
2.
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16. import
17.
18. import
19.
20.
21. @Configuration
22. @EnableCaching
23. public class RedisConfig extends
24.
25. @Bean
26. public
27. return new
28. @Override
29. public
30. new
31. sb.append(target.getClass().getName());
32. sb.append(method.getName());
33. for
34. sb.append(obj.toString());
35. }
36. return
37. }
38. };
39.
40. }
41.
42. @Bean
43. public
44. @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
45. return new
46. }
47.
48. @Bean
49. public
50. RedisConnectionFactory factory) {
51. new
52. new Jackson2JsonRedisSerializer(Object.class);
53. new
54. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
55. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
56. jackson2JsonRedisSerializer.setObjectMapper(om);
57. template.setValueSerializer(jackson2JsonRedisSerializer);
58. template.afterPropertiesSet();
59. return
60. }
61. }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
3 redis服务器配置
1. # REDIS (RedisProperties)
2. spring.redis.database= # database name
3. spring.redis.host=localhost # server host
4. spring.redis.password= # server password
5. spring.redis.port=6379
6. spring.redis.pool.max-idle=8
7. spring.redis.pool.min-idle=0
8. spring.redis.pool.max-active=8
9. spring.redis.pool.max-wait=-1
10. spring.redis.sentinel.master= # name of Redis server
11. spring.redis.sentinel.nodes= # comma-separated list of host:port pairs
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
4 应用
测试两个实体类
1. package
2.
3.