﻿{"id":498,"date":"2019-07-23T20:56:46","date_gmt":"2019-07-23T12:56:46","guid":{"rendered":"http:\/\/blog.sway.com.cn\/?p=498"},"modified":"2019-07-23T20:56:46","modified_gmt":"2019-07-23T12:56:46","slug":"springboot2jpa%e5%bc%95%e5%85%a5%e5%af%b9redis%e7%9a%84%e6%94%af%e6%8c%81","status":"publish","type":"post","link":"http:\/\/blog.sway.com.cn\/?p=498","title":{"rendered":"SpringBoot2+JPA\u5f15\u5165\u5bf9Redis\u7684\u652f\u6301"},"content":{"rendered":"<p>\u5728pom.xml\u52a0\u5165\uff1a<\/p>\n<pre class=\"lang:default decode:true\">        &lt;!-- \u5bf9Redis\u7684\u652f\u6301 --&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;spring-boot-starter-data-redis&lt;\/artifactId&gt;\r\n        &lt;\/dependency&gt;<\/pre>\n<p>\u589e\u52a0\u914d\u7f6e\u6587\u4ef6RedisConfig.java\uff1a<\/p>\n<pre class=\"lang:java decode:true\">package com.zero4j.config;\r\n\r\nimport com.fasterxml.jackson.annotation.JsonAutoDetect;\r\nimport com.fasterxml.jackson.annotation.PropertyAccessor;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\nimport org.springframework.cache.CacheManager;\r\nimport org.springframework.cache.annotation.CachingConfigurerSupport;\r\nimport org.springframework.cache.annotation.EnableCaching;\r\nimport org.springframework.cache.interceptor.KeyGenerator;\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.data.redis.cache.RedisCacheManager;\r\nimport org.springframework.data.redis.connection.RedisConnectionFactory;\r\nimport org.springframework.data.redis.core.RedisTemplate;\r\nimport org.springframework.data.redis.core.StringRedisTemplate;\r\nimport org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;\r\n\r\nimport java.lang.reflect.Method;\r\n\r\n\/**\r\n * Redis \u7f13\u5b58\u914d\u7f6e\u7c7b\uff08\u901a\u7528\uff09\r\n * @author linhongcun\r\n *\r\n *\/\r\n@Configuration\r\n@EnableCaching\r\npublic class RedisConfig extends CachingConfigurerSupport {\r\n\r\n    \/**\r\n     * \u7f13\u5b58\u5bf9\u8c61\u96c6\u5408\u4e2d\uff0c\u7f13\u5b58\u662f\u4ee5 key-value \u5f62\u5f0f\u4fdd\u5b58\u7684\u3002\u5f53\u4e0d\u6307\u5b9a\u7f13\u5b58\u7684 key \u65f6\uff0cSpringBoot \u4f1a\u4f7f\u7528 SimpleKeyGenerator \u751f\u6210 key\u3002\r\n     * @return\r\n     *\/\r\n    @Bean\r\n    public KeyGenerator wiselyKeyGenerator() {\r\n        return new KeyGenerator() {\r\n            @Override\r\n            public Object generate(Object target, Method method, Object... params) {\r\n                StringBuilder sb = new StringBuilder();\r\n                sb.append(target.getClass().getName());\r\n                sb.append(method.getName());\r\n                for (Object obj : params) {\r\n                    sb.append(obj.toString());\r\n                }\r\n                return sb.toString();\r\n            }\r\n        };\r\n\r\n    }\r\n\r\n    @Bean\r\n    public CacheManager cacheManager(RedisConnectionFactory factory) {\r\n    \treturn RedisCacheManager.create(factory);\r\n    }\r\n\r\n    @Bean\r\n    public RedisTemplate&lt;String, String&gt; redisTemplate(RedisConnectionFactory factory) {\r\n        StringRedisTemplate template = new StringRedisTemplate(factory);\r\n        @SuppressWarnings({ \"rawtypes\", \"unchecked\" })\r\n        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);\r\n        ObjectMapper om = new ObjectMapper();\r\n        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);\r\n        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);\r\n        jackson2JsonRedisSerializer.setObjectMapper(om);\r\n        template.setValueSerializer(jackson2JsonRedisSerializer);\r\n        template.afterPropertiesSet();\r\n        return template;\r\n    }\r\n}<\/pre>\n<p>\u5728application.properties\u4e2d\u52a0\u5165\uff1a<\/p>\n<pre class=\"lang:default decode:true \"># Redis\r\nspring.redis.host=127.0.0.1\r\nspring.redis.port=6379<\/pre>\n<p>\u5bf9Service\u7c7b\u52a0\u5165\u6ce8\u89e3@CacheConfig\u548c@Cacheable\uff0c\u5982\uff1a<\/p>\n<pre class=\"lang:java mark:12,16 decode:true \">package com.zero4j.model.config;\r\n\r\nimport java.util.List;\r\n\r\nimport org.springframework.cache.annotation.CacheConfig;\r\nimport org.springframework.cache.annotation.Cacheable;\r\nimport org.springframework.data.jpa.repository.JpaRepository;\r\nimport org.springframework.data.jpa.repository.Query;\r\nimport org.springframework.stereotype.Repository;\r\n\r\n@Repository\r\n@CacheConfig(cacheNames = \"configRepository\")\r\npublic interface ConfigRepository extends JpaRepository&lt;Config, String&gt; {\r\n\r\n\t@Query(\"SELECT config FROM Config config\")\r\n\t@Cacheable(value = \"findAll\",keyGenerator=\"wiselyKeyGenerator\")\r\n\tList&lt;Config&gt; findAll();\r\n\t\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728pom.xml\u52a0\u5165\uff1a &lt;!&#8211; \u5bf9Redis\u7684\u652f\u6301 &#8211;&gt; &lt;dependency&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[167],"tags":[172,168],"class_list":["post-498","post","type-post","status-publish","format-standard","hentry","category-springboot","tag-redis","tag-springboot"],"_links":{"self":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=498"}],"version-history":[{"count":1,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/498\/revisions"}],"predecessor-version":[{"id":499,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/498\/revisions\/499"}],"wp:attachment":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=498"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}