{"id":201294,"date":"2025-05-10T11:12:08","date_gmt":"2025-05-10T03:12:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201294\/"},"modified":"2025-05-10T11:12:08","modified_gmt":"2025-05-10T03:12:08","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8redis%e4%bd%9c%e4%b8%bamybatis%e7%9a%84%e4%ba%8c%e7%ba%a7%e7%bc%93%e5%ad%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201294\/","title":{"rendered":"\u5982\u4f55\u5229\u7528Redis\u4f5c\u4e3aMybatis\u7684\u4e8c\u7ea7\u7f13\u5b58"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5229\u7528Redis\u4f5c\u4e3aMybatis\u7684\u4e8c\u7ea7\u7f13\u5b58<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728\u6570\u636e\u5e93\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u5229\u7528Redis\u4f5c\u4e3aMybatis\u7684\u4e8c\u7ea7\u7f13\u5b58\u300b\uff0c\u804a\u804a\u7f13\u5b58\u3001RedisMybatis\u3001\u4e8c\u7ea7\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>\u90a3\u4eca\u5929\u5c31\u804a\u804a\u600e\u4e48\u4f18\u96c5\u7684\u7528Redis\u4f5c\u4e3aMybatis\u7684\u4e8c\u7ea7\u7f13\u5b58\u3002<\/p>\n<h2>\u8981\u4f18\u96c5\u5c31\u9009\u62e9Mybatis-Plus<\/h2>\n<p>\u5173\u4e8eMybatis-Plus\u7684\u57fa\u7840\u8bbe\u7f6e\u5c31\u4e0d\u591a\u505a\u4ecb\u7ecd\u4e86\uff0c\u53ea\u8bf4\u548c\u4e8c\u7ea7\u7f13\u5b58\u6709\u5173\u7684\u3002<\/p>\n<p>\u9996\u5148\u5728\u914d\u7f6e\u6587\u4ef6\u5f00\u542f\u4e8c\u7ea7\u7f13\u5b58\u3002<\/p>\n<pre>mybatis-plus:\n  configuration:\n &nbsp;  log-impl: org.apache.ibatis.logging.stdout.StdOutImpl\n &nbsp;  cache-enabled: true &nbsp; # \u5f00\u542f\u4e8c\u7ea7\u7f13\u5b58\n  mapper-locations: classpath:*\/mapper\/*.xml<\/pre>\n<h2>Redis\u914d\u7f6e<\/h2>\n<p><strong>\u8fd9\u90e8\u5206\u5c31\u662fRedis\u7684\u57fa\u672c\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>  redis:\n &nbsp;  host: 101.411.160.111\n &nbsp;  database: 0\n &nbsp;  port: 6311\n &nbsp;  password: 1111111<\/pre>\n<p>\u914d\u7f6eRedisTemplate<\/p>\n<pre>@Configuration\npublic class RedisConfig {\n &nbsp; &nbsp;\/**\n &nbsp; &nbsp; * \u8bbe\u7f6e\u7cfb\u5217\u5316\u65b9\u5f0f\u3001\u4e8b\u52a1\u7b49\u914d\u7f6e\n &nbsp; &nbsp; *\/\n &nbsp; &nbsp;@Bean\n &nbsp; &nbsp;public RedisTemplate\n   \n     redisTemplate(LettuceConnectionFactory lettuceConnectionFactory)\n &nbsp;  {\n &nbsp; &nbsp; &nbsp; &nbsp;RedisTemplate\n    \n      redisTemplate = new RedisTemplate(); \u200b &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate.setConnectionFactory(lettuceConnectionFactory); &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8bbe\u7f6ekey\u5e8f\u5217\u5316\u65b9\u5f0fstring &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate.setKeySerializer(new StringRedisSerializer()); &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8bbe\u7f6evalue\u7684\u5e8f\u5217\u5316\u65b9\u5f0fjson &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); \u200b &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate.setHashKeySerializer(new StringRedisSerializer()); &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); \u200b &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate.afterPropertiesSet(); \u200b &nbsp; &nbsp; &nbsp; &nbsp;return redisTemplate; &nbsp; } }\n    \n   <\/pre>\n<h2>\u81ea\u5b9a\u4e49Mybatis\u7f13\u5b58<\/h2>\n<p>\u6211\u4eec\u53ea\u9700\u8981\u5b9e\u73b0<code>Cache<\/code>\u8fd9\u4e2a\u63a5\u53e3\u3002<\/p>\n<pre>@Slf4j\npublic class MybatisRedisCache implements Cache {\n &nbsp; &nbsp;private static final String COMMON_CACHE_KEY = \"mybatis\";\n &nbsp; &nbsp;\/\/ \u8bfb\u5199\u9501\n &nbsp; &nbsp;private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true)\n &nbsp; &nbsp;private final RedisTemplate\n   \n     redisTemplate;\n &nbsp; &nbsp;private final String nameSpace;\n\u200b\n &nbsp; &nbsp;public MybatisRedisCache(String nameSpace) {\n &nbsp; &nbsp; &nbsp; &nbsp;if (nameSpace == null) {\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new IllegalArgumentException(\"Cache instances require an ID\");\n &nbsp; &nbsp; &nbsp;  }\n &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate = SpringUtil.getBean(\"redisTemplate\");\n &nbsp; &nbsp; &nbsp; &nbsp;this.nameSpace = nameSpace;\n &nbsp;  }\n &nbsp; &nbsp;@Override\n &nbsp; &nbsp;public String getId() {\n &nbsp; &nbsp; &nbsp; &nbsp;return this.nameSpace;\n &nbsp;  }\n\u200b\n &nbsp; &nbsp;private String getKeys() {\n\u200b\n &nbsp; &nbsp; &nbsp; &nbsp;return COMMON_CACHE_KEY + \"::\" + nameSpace + \"::*\";\n &nbsp;  }\n\u200b\n &nbsp; &nbsp;private String getKey(Object key) {\n &nbsp; &nbsp; &nbsp; &nbsp;return COMMON_CACHE_KEY + \"::\" + nameSpace + \"::\" + DigestUtils.md5Hex(String.valueOf(key));\n &nbsp;  }\n &nbsp; &nbsp;@Override\n &nbsp; &nbsp;public void putObject(Object key, Object value) {\n &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate.opsForValue().set(getKey(key), value, 10, TimeUnit.MINUTES);\n &nbsp;  }\n &nbsp; &nbsp;@Override\n &nbsp; &nbsp;public Object getObject(Object key) {\n &nbsp; &nbsp; &nbsp; &nbsp;try {\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return redisTemplate.opsForValue().get(getKey(key));\n &nbsp; &nbsp; &nbsp;  } catch (Exception e) {\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace();\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.error(\"\u7f13\u5b58\u51fa\u9519 \");\n &nbsp; &nbsp; &nbsp;  }\n &nbsp; &nbsp; &nbsp; &nbsp;return null;\n &nbsp;  }\n\u200b\n &nbsp; &nbsp;@Override\n &nbsp; &nbsp;public Object removeObject(Object o) {\n &nbsp; &nbsp; &nbsp; &nbsp;Object n = redisTemplate.opsForValue().get(getKey(o));\n &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate.delete(getKey(o));\n &nbsp; &nbsp; &nbsp; &nbsp;return n;\n &nbsp;  }\n\u200b\n &nbsp; &nbsp;@Override\n &nbsp; &nbsp;public void clear() {\n &nbsp; &nbsp; &nbsp; &nbsp;Set\n    \n      keys = redisTemplate.keys(getKeys()); &nbsp; &nbsp; &nbsp; &nbsp;if (CollectionUtil.isNotEmpty(keys)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;assert keys != null; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate.delete(keys); &nbsp; &nbsp; &nbsp; } &nbsp; } &nbsp; &nbsp;@Override &nbsp; &nbsp;public int getSize() { &nbsp; &nbsp; &nbsp; &nbsp;Set\n     \n       keys = redisTemplate.keys(getKeys()); &nbsp; &nbsp; &nbsp; &nbsp;if (CollectionUtil.isNotEmpty(keys)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;assert keys != null; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return keys.size(); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp;return 0; &nbsp; } &nbsp; &nbsp;@Override &nbsp; &nbsp;public ReadWriteLock getReadWriteLock() { &nbsp; &nbsp; &nbsp; &nbsp;return this.readWriteLock; &nbsp; } }\n     \n    \n   <\/pre>\n<h2>\u6d4b\u8bd5<\/h2>\n<p>1.\u7b2c\u4e00\u6b21\u67e5\u8be2\uff0c\u8d70\u6570\u636e\u5e93\uff0c\u5e76\u5199\u5165\u7f13\u5b58\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167257267663b16f042aae4.png\" class=\"aligncenter\"><\/p>\n<p><strong>\u770b\u770bRedis\u7684\u8bb0\u5f55\uff1a<\/strong><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167257267663b16f0490f3a.png\" class=\"aligncenter\"><\/p>\n<p>2.\u7b2c\u4e8c\u6b21\u67e5\u8be2\uff0c\u76f4\u63a5\u8d70\u7f13\u5b58<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167257267663b16f04ef3d6.png\" class=\"aligncenter\"><\/p>\n<p>3.\u91cd\u542f\u9879\u76ee\uff0c\u4f9d\u7136\u53ef\u4ee5\u76f4\u63a5\u67e5\u7f13\u5b58<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167257267763b16f0572265.png\" class=\"aligncenter\"><\/p>\n<h2>\u7f13\u5b58\u547d\u4e2d\u7387\uff08Cache Hit Ratio\uff09<\/h2>\n<p>\u4e0d\u77e5\u9053\u6709\u6ca1\u6709\u7ec6\u5fc3\u7684\u540c\u5b66\u6ce8\u610f\u5230\u8fd9\u6837\u4e00\u884c\u65e5\u5fd7\uff1a<\/p>\n<pre>Cache Hit Ratio [com.yitiao.mapper.ArticleMapper]: 0.5<\/pre>\n<p>\u6700\u540e\u8fd9\u4e2a0.5\u5c31\u662f\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u4ee3\u8868\u4e00\u5171\u67e5\u8be2\u4e24\u6b21\uff0c\u547d\u4e2d\u4e00\u6b21\u7f13\u5b58\u4e00\u6b21\u3002<\/p>\n<h2>\u4e00\u7ea7\u7f13\u5b58\u548c\u4e8c\u7ea7\u7f13\u5b58<\/h2>\n<p><strong>\u4e00\u7ea7\u7f13\u5b58<\/strong><\/p>\n<p>\u4e00\u7ea7\u7f13\u5b58 Mybatis \u7684\u4e00\u7ea7\u7f13\u5b58\u662f\u6307 SQLSession\uff0c\u4e00\u7ea7\u7f13\u5b58\u7684\u4f5c\u7528\u57df\u662f SQlSession , Mabits \u9ed8\u8ba4\u5f00\u542f\u4e00\u7ea7\u7f13\u5b58\u3002 \u5728\u540c\u4e00\u4e2aSqlSession\u4e2d\uff0c\u6267\u884c\u76f8\u540c\u7684SQL\u67e5\u8be2\u65f6\uff1b\u7b2c\u4e00\u6b21\u4f1a\u53bb\u67e5\u8be2\u6570\u636e\u5e93\uff0c\u5e76\u5199\u5728\u7f13\u5b58\u4e2d\uff0c\u7b2c\u4e8c\u6b21\u4f1a\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u53d6\u3002 \u5f53\u6267\u884cSQL\u65f6\u5019\u4e24\u6b21\u67e5\u8be2\u4e2d\u95f4\u53d1\u751f\u4e86\u589e\u5220\u6539\u7684\u64cd\u4f5c\uff0c\u5219SQLSession\u7684\u7f13\u5b58\u4f1a\u88ab\u6e05\u7a7a\u3002<\/p>\n<p>\u6bcf\u6b21\u67e5\u8be2\u4f1a\u5148\u53bb\u7f13\u5b58\u4e2d\u627e\uff0c\u5982\u679c\u627e\u4e0d\u5230\uff0c\u518d\u53bb\u6570\u636e\u5e93\u67e5\u8be2\uff0c\u7136\u540e\u628a\u7ed3\u679c\u5199\u5230\u7f13\u5b58\u4e2d\u3002 Mybatis\u7684\u5185\u90e8\u7f13\u5b58\u4f7f\u7528\u4e00\u4e2aHashMap\uff0ckey\u4e3ahashcode+statementId+sql\u8bed\u53e5\u3002Value\u4e3a\u67e5\u8be2\u51fa\u6765\u7684\u7ed3\u679c\u96c6\u6620\u5c04\u6210\u7684java\u5bf9\u8c61\u3002 SqlSession\u6267\u884cinsert\u3001update\u3001delete\u7b49\u64cd\u4f5ccommit\u540e\u4f1a\u6e05\u7a7a\u8be5SQLSession\u7f13\u5b58\u3002<\/p>\n<p><strong>\u4e8c\u7ea7\u7f13\u5b58<\/strong><\/p>\n<p>\u4e8c\u7ea7\u7f13\u5b58 \u4e8c\u7ea7\u7f13\u5b58\u662f mapper \u7ea7\u522b\u7684\uff0cMybatis\u9ed8\u8ba4\u662f\u6ca1\u6709\u5f00\u542f\u4e8c\u7ea7\u7f13\u5b58\u7684\u3002 \u7b2c\u4e00\u6b21\u8c03\u7528mapper\u4e0b\u7684SQL\u53bb\u67e5\u8be2\u7528\u6237\u7684\u4fe1\u606f\uff0c\u67e5\u8be2\u5230\u7684\u4fe1\u606f\u4f1a\u5b58\u653e\u5230\u8be5 mapper \u5bf9\u5e94\u7684\u4e8c\u7ea7\u7f13\u5b58\u533a\u57df\u3002 \u7b2c\u4e8c\u6b21\u8c03\u7528 namespace \u4e0b\u7684 mapper \u6620\u5c04\u6587\u4ef6\u4e2d\uff0c\u76f8\u540c\u7684sql\u53bb\u67e5\u8be2\u7528\u6237\u4fe1\u606f\uff0c\u4f1a\u53bb\u5bf9\u5e94\u7684\u4e8c\u7ea7\u7f13\u5b58\u5185\u53d6\u7ed3\u679c\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167257267763b16f05cddc5.png\" class=\"aligncenter\"><\/p>\n<h2>\u4ec0\u4e48\u65f6\u5019\u8be5\u5f00\u542f\u4e8c\u7ea7\u7f13\u5b58<\/h2>\n<p>\u8bf4\u5b9e\u8bdd\uff0c\u6211\u9047\u5230\u5f00\u542f\u4e8c\u7ea7\u7f13\u5b58\u7684\u65f6\u5019\u5e76\u4e0d\u591a\uff0c\u56e0\u4e3a\u7f13\u5b58\u6709\u5229\u4e5f\u6709\u5f0a\u3002<\/p>\n<p>\u6211\u7684\u5efa\u8bae\u662f\u5982\u679c\u53d1\u73b0\u63a5\u53e3\u8017\u65f6\u4e25\u91cd\uff0c\u53ef\u4ee5\u5728\u7ebf\u4e0a\u5f00\u542f\u4e8c\u7ea7\u7f13\u5b58\uff0c\u5f00\u53d1\u73af\u5883\u5173\u6389\uff0c\u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u5c31\u62ff\u4eca\u5929\u6211\u9047\u5230\u7684\u4e8b\u6765\u8bf4\uff0c\u5f00\u53d1\u76f4\u63a5\u6539\u5e93\u4e0d\u80fd\u7acb\u5373\u751f\u6548\uff0c\u5c31\u5f88\u70e6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528Redis\u4f5c\u4e3aMybat&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201294","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201294"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201294\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}