{"id":1546,"date":"2024-01-17T14:38:23","date_gmt":"2024-01-17T06:38:23","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1546"},"modified":"2024-01-17T14:38:44","modified_gmt":"2024-01-17T06:38:44","slug":"mybatis%e4%ba%8c%e7%ba%a7%e7%bc%93%e5%ad%98%e4%bb%a5%e5%8f%8a%e4%bd%bf%e7%94%a8redis%e4%bf%9d%e5%ad%98","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2024\/01\/17\/mybatis%e4%ba%8c%e7%ba%a7%e7%bc%93%e5%ad%98%e4%bb%a5%e5%8f%8a%e4%bd%bf%e7%94%a8redis%e4%bf%9d%e5%ad%98\/","title":{"rendered":"Mybatis\u4e8c\u7ea7\u7f13\u5b58\u4ee5\u53ca\u4f7f\u7528Redis\u4fdd\u5b58"},"content":{"rendered":"\n<p>MyBatis \u5185\u7f6e\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u4e8b\u52a1\u6027\u67e5\u8be2\u7f13\u5b58\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u914d\u7f6e\u548c\u5b9a\u5236\u3002\u5176\u5185\u7f6e\u4e86\u4e00\u4e2a\u7f13\u5b58\u673a\u5236\uff0c\u6211\u4eec\u67e5\u8be2\u65f6\uff0c\u5982\u679c\u7f13\u5b58\u4e2d\u5b58\u5728\u6570\u636e\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\uff0c\u800c\u4e0d\u662f\u518d\u53bb\u5411\u6570\u636e\u5e93\u8fdb\u884c\u8bf7\u6c42\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Mybatis\u5b58\u5728\u4e00\u7ea7\u7f13\u5b58\u548c\u4e8c\u7ea7\u7f13\u5b58\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"751\" height=\"259\" src=\"https:\/\/i0.wp.com\/fireinsect.top\/wp-content\/uploads\/2024\/01\/image-28.png?resize=751%2C259&#038;ssl=1\" alt=\"\" class=\"wp-image-1549\" srcset=\"https:\/\/i0.wp.com\/fireinsect.top\/wp-content\/uploads\/2024\/01\/image-28.png?w=751&amp;ssl=1 751w, https:\/\/i0.wp.com\/fireinsect.top\/wp-content\/uploads\/2024\/01\/image-28.png?resize=300%2C103&amp;ssl=1 300w\" sizes=\"(max-width: 751px) 100vw, 751px\" data-recalc-dims=\"1\" \/><\/figure>\n\n\n\n<p>\u6211\u4eec\u9996\u5148\u6765\u770b\u4e00\u4e0b\u4e00\u7ea7\u7f13\u5b58\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u53ea\u542f\u7528\u4e86\u672c\u5730\u7684\u4f1a\u8bdd\u7f13\u5b58\uff0c\u5b83\u4ec5\u4ec5\u5bf9\u4e00\u4e2a\u4f1a\u8bdd\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u7f13\u5b58\uff08\u4e00\u7ea7\u7f13\u5b58\u65e0\u6cd5\u5173\u95ed\uff0c\u53ea\u80fd\u8c03\u6574\uff09\uff0c\u8ba9\u6211\u4eec\u5982\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">public static void main(String[] args) throws InterruptedException {\r\n    try (SqlSession sqlSession = MybatisUtil.getSession(true)){\r\n        TestMapper testMapper = sqlSession.getMapper(TestMapper.class);\r\n        Student student1 = testMapper.getStudentBySid(1);\r\n        Student student2 = testMapper.getStudentBySid(1);\r\n        System.out.println(student1 == student2);\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>\u4f1a\u53d1\u73b0\u7ed3\u679c\u4e3atrue\uff0c\u56e0\u4e3a\u5904\u4e8e\u4e00\u4e2a\u4f1a\u8bdd\u4e2d\uff0cmybatis\u52a8\u7528\u4e86\u7f13\u5b58\uff0c\u5c31\u4e0d\u7528\u53bb\u91cd\u65b0\u6784\u9020\u5bf9\u8c61\u4e86\u3002\u800c\u4e00\u7ea7\u7f13\u5b58\u5728\u6211\u4eec\u8fdb\u884c\u589e\u5220\u6539\u64cd\u4f5c\u540e\u5c31\u4f1a\u5931\u6548\uff0c\u4fdd\u8bc1\u6570\u636e\u4e3a\u6700\u65b0\u5185\u5bb9\u4e14\u5728\u4f1a\u8bdd\u7ed3\u675f\u540e\uff0c\u4f1a\u6e05\u9664\u6240\u6709\u7f13\u5b58\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u7ea7\u7f13\u5b58\u53ea\u9488\u5bf9\u5355\u4e2a\u4f1a\u8bdd\uff0c\u591a\u4e2a\u4f1a\u8bdd\u4e4b\u95f4\u4e0d\u76f8\u901a\u7684\u3002<\/p>\n\n\n\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e2a\u4f1a\u8bddDML\u64cd\u4f5c\u53ea\u4f1a\u91cd\u7f6e\u5f53\u524d\u4f1a\u8bdd\u7684\u7f13\u5b58\uff0c\u4e0d\u4f1a\u91cd\u7f6e\u5176\u4ed6\u4f1a\u8bdd\u7684\u7f13\u5b58\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5176\u4ed6\u4f1a\u8bdd\u7f13\u5b58\u662f\u4e0d\u4f1a\u66f4\u65b0\u7684\uff01<\/p>\n\n\n\n<p>\u4e00\u7ea7\u7f13\u5b58\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u5f88\u9ad8\u901f\u7684\u8bbf\u95ee\u6548\u7387\uff0c\u4f46\u662f\u5b83\u7684\u4f5c\u7528\u8303\u56f4\u5b9e\u5728\u662f\u6709\u9650\uff0c\u5982\u679c\u4e00\u4e2a\u4f1a\u8bdd\u7ed3\u675f\uff0c\u90a3\u4e48\u4e4b\u524d\u7684\u7f13\u5b58\u5c31\u5168\u90e8\u5931\u6548\u4e86\uff0c\u4f46\u662f\u6211\u4eec\u5e0c\u671b\u7f13\u5b58\u80fd\u591f\u6269\u5c55\u5230\u6240\u6709\u4f1a\u8bdd\u90fd\u80fd\u4f7f\u7528\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e8c\u7ea7\u7f13\u5b58\u6765\u5b9e\u73b0\uff0c\u4e8c\u7ea7\u7f13\u5b58\u9ed8\u8ba4\u662f\u5173\u95ed\u72b6\u6001\uff0c\u8981\u5f00\u542f\u4e8c\u7ea7\u7f13\u5b58\uff0c\u6211\u4eec\u9700\u8981\u5728\u6620\u5c04\u5668\uff08mapper\uff09XML\u6587\u4ef6\u4e2d\u6dfb\u52a0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;cache\/><\/code><\/pre>\n\n\n\n<p>\u53ef\u89c1\u4e8c\u7ea7\u7f13\u5b58\u662fMapper\u7ea7\u522b\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u4e00\u4e2a\u4f1a\u8bdd\u5931\u6548\u65f6\uff0c\u5b83\u7684\u7f13\u5b58\u4f9d\u7136\u4f1a\u5b58\u5728\u4e8e\u4e8c\u7ea7\u7f13\u5b58\u4e2d\uff0c\u56e0\u6b64\u5982\u679c\u6211\u4eec\u518d\u6b21\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4f1a\u8bdd\u4f1a\u76f4\u63a5\u4f7f\u7528\u4e4b\u524d\u7684\u7f13\u5b58\uff0c\u6211\u4eec\u9996\u5148\u6839\u636e\u5b98\u65b9\u6587\u6863\u8fdb\u884c\u4e00\u4e9b\u914d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;cache\r\n  eviction=\"FIFO\"\r\n  flushInterval=\"60000\"\r\n  size=\"512\"\r\n  readOnly=\"true\"\/><\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u80fd\u5b9e\u73b0\u8de8\u4f1a\u8bdd\u7684\u7f13\u5b58\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u4e0d\u5e0c\u671b\u67d0\u4e2a\u65b9\u6cd5\u5f00\u542f\u7f13\u5b58\uff0c\u53ef\u4ee5\u6dfb\u52a0useCache\u5c5e\u6027\u6765\u5173\u95ed\u7f13\u5b58\uff1a<\/p>\n\n\n\n<pre title=\"\" class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;select id=\"getStudentBySid\" resultType=\"Student\" useCache=\"false\">\r\n    select * from student where sid = #{sid}\r\n&lt;\/select><\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528flushCache=&#8221;false&#8221;\u5728\u6bcf\u6b21\u6267\u884c\u540e\u90fd\u6e05\u7a7a\u7f13\u5b58\uff0c\u901a\u8fc7\u8fd9\u8fd9\u4e2a\u6211\u4eec\u8fd8\u53ef\u4ee5\u63a7\u5236DML\u64cd\u4f5c\u5b8c\u6210\u4e4b\u540e\u4e0d\u6e05\u7a7a\u7f13\u5b58\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;select id=\"getStudentBySid\" resultType=\"Student\" flushCache=\"true\">\r\n    select * from student where sid = #{sid}\r\n&lt;\/select><\/code><\/pre>\n\n\n\n<p>\u6dfb\u52a0\u4e86\u4e8c\u7ea7\u7f13\u5b58\u4e4b\u540e\uff0c\u4f1a\u5148\u4ece\u4e8c\u7ea7\u7f13\u5b58\u4e2d\u67e5\u627e\u6570\u636e\uff0c\u5f53\u4e8c\u7ea7\u7f13\u5b58\u4e2d\u6ca1\u6709\u65f6\uff0c\u624d\u4f1a\u4ece\u4e00\u7ea7\u7f13\u5b58\u4e2d\u83b7\u53d6\uff0c\u5f53\u4e00\u7ea7\u7f13\u5b58\u4e2d\u90fd\u8fd8\u6ca1\u6709\u6570\u636e\u65f6\uff0c\u624d\u4f1a\u8bf7\u6c42\u6570\u636e\u5e93\u3002<\/p>\n\n\n\n<div style=\"height:17px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Redis\u4fdd\u5b58\u4e8c\u7ea7\u7f13\u5b58<\/h2>\n\n\n\n<p>\u524d\u9762\u4e2d\uff0c\u6211\u4eec\u7406\u89e3\u7684Mybatis\u4e8c\u7ea7\u7f13\u5b58\u7684\u4f7f\u7528\uff0c\u4f46\u5b9e\u9645\u4e0a\uff0cMybatis\u539f\u751f\u7684\u4e8c\u7ea7\u7f13\u5b58\u662f\u5b58\u5728\u5728\u5355\u4e2a\u865a\u62df\u673a\u4e0a\u7684\u3002\u5982\u679c\u591a\u4e2a\u670d\u52a1\u5668\u8bbf\u95ee\u540c\u4e00\u4e2a\u6570\u636e\u5e93\uff0c\u4e8c\u7ea7\u7f13\u5b58\u53ea\u4f1a\u5728\u5404\u81ea\u7684\u670d\u52a1\u5668\u4e0a\u751f\u6548\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u670d\u52a1\u5171\u7528\u7f13\u5b58\uff0c\u5c31\u9700\u8981\u4f7f\u7528\u5206\u5e03\u5f0f\u7f13\u5b58\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528Redis\u4f5c\u4e3a\u7f13\u5b58\u6570\u636e\u5e93\uff0c\u9996\u5148\u9700\u8981\u624b\u52a8\u5b9e\u73b0Mybatis\u63d0\u4f9b\u7684Cache\u63a5\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">\/\/\u5b9e\u73b0Mybatis\u7684Cache\u63a5\u53e3\r\npublic class RedisMybatisCache implements Cache {\r\n\r\n    private final String id;\r\n    private static RedisTemplate&lt;Object, Object> template;\r\n\r\n   \t\/\/\u6ce8\u610f\u6784\u9020\u65b9\u6cd5\u5fc5\u987b\u5e26\u4e00\u4e2aString\u7c7b\u578b\u7684\u53c2\u6570\u63a5\u6536id\r\n    public RedisMybatisCache(String id){\r\n        this.id = id;\r\n    }\r\n\r\n  \t\/\/\u521d\u59cb\u5316\u65f6\u901a\u8fc7\u914d\u7f6e\u7c7b\u5c06RedisTemplate\u7ed9\u8fc7\u6765\r\n    public static void setTemplate(RedisTemplate&lt;Object, Object> template) {\r\n        RedisMybatisCache.template = template;\r\n    }\r\n\r\n    @Override\r\n    public String getId() {\r\n        return id;\r\n    }\r\n\r\n    @Override\r\n    public void putObject(Object o, Object o1) {\r\n      \t\/\/\u8fd9\u91cc\u76f4\u63a5\u5411Redis\u6570\u636e\u5e93\u4e2d\u4e22\u6570\u636e\u5373\u53ef\uff0co\u5c31\u662fKey\uff0co1\u5c31\u662fValue\uff0c60\u79d2\u4e3a\u8fc7\u671f\u65f6\u95f4\r\n        template.opsForValue().set(o, o1, 60, TimeUnit.SECONDS);\r\n    }\r\n\r\n    @Override\r\n    public Object getObject(Object o) {\r\n      \t\/\/\u8fd9\u91cc\u6839\u636eKey\u76f4\u63a5\u4eceRedis\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u503c\u5373\u53ef\r\n        return template.opsForValue().get(o);\r\n    }\r\n\r\n    @Override\r\n    public Object removeObject(Object o) {\r\n      \t\/\/\u6839\u636eKey\u5220\u9664\r\n        return template.delete(o);\r\n    }\r\n\r\n    @Override\r\n    public void clear() {\r\n      \t\/\/\u7531\u4e8etemplate\u4e2d\u6ca1\u5c01\u88c5\u6e05\u9664\u64cd\u4f5c\uff0c\u53ea\u80fd\u901a\u8fc7connection\u6765\u6267\u884c\r\n\t\t\t\ttemplate.execute((RedisCallback&lt;Void>) connection -> {\r\n          \t\/\/\u901a\u8fc7connection\u5bf9\u8c61\u6267\u884c\u6e05\u7a7a\u64cd\u4f5c\r\n            connection.flushDb();\r\n            return null;\r\n        });\r\n    }\r\n\r\n    @Override\r\n    public int getSize() {\r\n      \t\/\/\u8fd9\u91cc\u4e5f\u662f\u4f7f\u7528connection\u5bf9\u8c61\u6765\u83b7\u53d6\u5f53\u524d\u7684Key\u6570\u91cf\r\n        return template.execute(RedisServerCommands::dbSize).intValue();\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>\u4e4b\u540e\u6211\u4eec\u9700\u8981\u53bb\u7f16\u5199\u914d\u7f6e\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">@Configuration\r\npublic class MainConfiguration {\r\n    @Resource\r\n    RedisTemplate&lt;Object, Object> template;\r\n\r\n    @PostConstruct\r\n    public void init(){\r\n      \t\/\/\u628aRedisTemplate\u7ed9\u5230RedisMybatisCache\r\n        RedisMybatisCache.setTemplate(template);\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>\u6700\u540e\u6211\u4eec\u5728Mapper\u4e0a\u542f\u7528\u6b64\u7f13\u5b58\u5373\u53ef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">\/\/\u53ea\u9700\u8981\u4fee\u6539\u7f13\u5b58\u5b9e\u73b0\u7c7bimplementation\u4e3a\u6211\u4eec\u7684RedisMybatisCache\u5373\u53ef\r\n@CacheNamespace(implementation = RedisMybatisCache.class)\r\n@Mapper\r\npublic interface MainMapper {\r\n\r\n    @Select(\"select name from student where sid = 1\")\r\n    String getSid();\r\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u5b9e\u73b0\u4e86Mybatis\u4e8c\u7ea7\u7f13\u5b58\u7684\u5206\u5e03\u5f0f\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MyBatis \u5185\u7f6e\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u4e8b\u52a1\u6027\u67e5\u8be2\u7f13\u5b58\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u914d\u7f6e\u548c\u5b9a\u5236\u3002\u5176\u5185\u7f6e\u4e86\u4e00\u4e2a\u7f13\u5b58\u673a\u5236\uff0c\u6211\u4eec\u67e5\u8be2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-1546","post","type-post","status-publish","format-standard","hentry","category-4"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1546"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1546"}],"version-history":[{"count":3,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1546\/revisions"}],"predecessor-version":[{"id":1550,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1546\/revisions\/1550"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}