{"id":201226,"date":"2025-05-10T10:32:51","date_gmt":"2025-05-10T02:32:51","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201226\/"},"modified":"2025-05-10T10:32:51","modified_gmt":"2025-05-10T02:32:51","slug":"springboot%e6%95%b4%e5%90%88redis%e5%85%a5%e9%97%a8%e4%b9%8b%e7%bc%93%e5%ad%98%e6%95%b0%e6%8d%ae%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201226\/","title":{"rendered":"SpringBoot\u6574\u5408Redis\u5165\u95e8\u4e4b\u7f13\u5b58\u6570\u636e\u7684\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>SpringBoot\u6574\u5408Redis\u5165\u95e8\u4e4b\u7f13\u5b58\u6570\u636e\u7684\u65b9\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0cgolang\u5b66\u4e60\u7f51\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aSpringBoot\u6574\u5408Redis\u5165\u95e8\u4e4b\u7f13\u5b58\u6570\u636e\u7684\u65b9\u6cd5\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86SpringBootRedis\u3001\u7f13\u5b58\u6570\u636e\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528Redis\u5462\uff1f<\/h3>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u5982\u7cfb\u7edf\u4e2d\u67092\u5343\u4e07\u7528\u6237\u4fe1\u606f\uff0c\u7528\u6237\u4fe1\u606f\u57fa\u672c\u56fa\u5b9a\uff0c\u4e00\u65e6\u5f55\u5165\u5f88\u5c11\u53d8\u52a8\uff0c\u90a3\u4e48\u4f60\u6bcf\u6b21\u52a0\u8f7d\u6240\u6709\u7528\u6237\u4fe1\u606f\u65f6\uff0c\u5982\u679c\u90fd\u8981\u8bf7\u6c42\u6570\u636e\u5e93\uff0c\u6570\u636e\u5e93\u7f16\u8bd1\u5e76\u6267\u884c\u4f60\u7684\u67e5\u8be2\u8bed\u53e5\uff0c\u8fd9\u6837\u6548\u7387\u5c31\u4f1a\u4f4e\u4e0b\u5f88\u591a\uff0c\u9488\u5bf9\u8fd9\u79cd\u4fe1\u606f\u4e0d\u7ecf\u5e38\u53d8\u52a8\u5e76\u4e14\u6570\u636e\u91cf\u3002<\/p>\n<p>\u8f83\u5927\u7684\u60c5\u51b5\uff0c\u901a\u5e38\u505a\u6cd5\uff0c\u5c31\u662f\u628a\u4ed6\u52a0\u5165\u7f13\u5b58\uff0c\u6bcf\u6b21\u53d6\u6570\u524d\u5148\u53bb\u5224\u65ad\uff0c\u5982\u679c\u7f13\u5b58\u4e0d\u4e3a\u7a7a\uff0c\u90a3\u4e48\u5c31\u4ece\u7f13\u5b58\u53d6\u503c\uff0c\u5982\u679c\u4e3a\u7a7a\uff0c\u518d\u53bb\u8bf7\u6c42\u6570\u636e\u5e93\uff0c\u5e76\u5c06\u6570\u636e\u52a0\u5165\u7f13\u5b58\uff0c\u8fd9\u6837\u5927\u5927\u63d0\u9ad8\u7cfb\u7edf\u8bbf\u95ee\u6548\u7387\u3002<\/p>\n<hr>\n<h3>\u76f8\u5173\u4f9d\u8d56<\/h3>\n<pre>\n\n   \n\n   \n    \n     org.springframework.boot\n    \n    \n     spring-boot-starter-parent\n    \n    \n     2.2.7.RELEASE\n    \n   \n   \n   \n    \n    \n     \n      org.springframework.boot\n     \n     \n      spring-boot-starter-data-redis\n     \n    \n    \n    \n     \n      org.apache.commons\n     \n     \n      commons-pool2\n     \n    \n    \n    \n     \n      mysql\n     \n     \n      mysql-connector-java\n     \n    \n    \n    \n     \n      org.mybatis.spring.boot\n     \n     \n      mybatis-spring-boot-starter\n     \n     \n      2.1.1\n     \n    \n    \n    \n     \n      tk.mybatis\n     \n     \n      mapper-spring-boot-starter\n     \n     \n      2.1.5\n     \n    \n    \n    \n     \n      org.projectlombok\n     \n     \n      lombok\n     \n    \n    \n    \n     \n      org.springframework.boot\n     \n     \n      spring-boot-starter-test\n     \n    \n   <\/pre>\n<h2>\u914d\u7f6e<\/h2>\n<pre>\n# \u7aef\u53e3\nserver:\n  port: 9998\n# mysql\u6570\u636e\u6e90\nspring:\n  datasource:\n    username: root\n    password: root\n    url: jdbc:mysql:\/\/127.0.0.1:3306\/dbtest?serverTimezone=GMT%2B8\n# redis\n  redis:\n    host: localhost\n    port: 6379\n    timeout: 1000\n    jedis:\n      pool:\n        min-idle: 5\n        max-idle: 10\n        max-wait: -1\n# mybatis\nmybatis:\n  mapper-locations: classpath:\/mybatis\/mapper\/*.xml\n  type-aliases-package: cn.kgc.entities\n#  \u5f00\u542f\u9a7c\u5cf0\u547d\u540d\n  configuration:\n    map-underscore-to-camel-case: true\n# log\nlogging:\n  level:\n    cn.kgc: debug\n<\/pre>\n<h3>\u6570\u636e\u5e93<\/h3>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167248263163b00f47deae5.png\" class=\"aligncenter\"><\/p>\n<pre>\n#\u5efa\u8868\nCREATE TABLE `emp`  (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,\n  `age` int(11) NULL DEFAULT NULL,\n  PRIMARY KEY (`id`) USING BTREE\n) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;\n#\u63d2\u5165\u6570\u636e\nINSERT INTO `emp` VALUES (1, '\u5f20\u4e09', 18);\nINSERT INTO `emp` VALUES (2, '\u674e\u56db', 20);\nINSERT INTO `emp` VALUES (3, '\u738b\u4e94', 22);\n<\/pre>\n<h3>\u5b9e\u4f53\u7c7b<\/h3>\n<p><strong>Emp<\/strong><\/p>\n<pre>\n@Data\n@Table(name = \"emp\")\npublic class Emp implements Serializable {\n    @Id\n    @GeneratedValue(strategy = GenerationType.IDENTITY)\n    private Integer id;\n    private String name;\n    private Integer age;\n}\n<\/pre>\n<h2>RedisConfig<\/h2>\n<p>\u6307\u5b9aRedis\u5e8f\u5217\u5316\u65b9\u5f0f<\/p>\n<pre>\n@Configuration\npublic class RedisConfig {\n\n    @Bean\n    public RedisTemplate\n   \n     redisTemplate(RedisConnectionFactory factory){\n        RedisTemplate\n    \n      redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(factory); \/\/ \u6307\u5b9akv\u7684\u5e8f\u5217\u5316\u65b9\u5f0f Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setKeySerializer(new StringRedisSerializer()); return redisTemplate; } } \n    \n   <\/pre>\n<h3>Mapper<\/h3>\n<p>Emp\u7684Mapper\u63a5\u53e3\u7ee7\u627ftk\u7684Mapper\u7c7b\uff0c\u6cdb\u578b\u4e3a\u5b9e\u4f53\u7c7bEmp<\/p>\n<pre>\npublic interface EmpMapper extends Mapper\n   \n     {\n\n}\n\n   <\/pre>\n<h3>Service\u63a5\u53e3<\/h3>\n<p>\u4e1a\u52a1\u63a5\u53e3\u5b9a\u4e49add\u6dfb\u52a0\u548cgetEmpById\u6839\u636eid\u67e5\u8be2\u7684\u65b9\u6cd5<\/p>\n<pre>\npublic interface EmpService {\n    public void add(Emp emp);\n    public Object getEmpById(Integer id);\n}\n<\/pre>\n<h3>Service\u5b9e\u73b0\u7c7b<\/h3>\n<p>\u5148\u67e5Redis\uff0cRedis\u6ca1\u6709\u6570\u636e\u518d\u4ece\u6570\u636e\u5e93\u4e2d\u62ff\u6570\u636e\uff0c\u540c\u65f6\u7f13\u5b58\u5230Redis\u4e2d\u3002<\/p>\n<pre>\n@Service\n@Slf4j\npublic class EmpServiceImpl implements EmpService {\n\n    @Autowired\n    public RedisTemplate redisTemplate;\n\n    @Resource\n    private EmpMapper empMapper;\n\n    @Override\n    public void add(Emp emp) {\n        empMapper.insert(emp);\n    }\n\n    @Override\n    public  Object getEmpById(Integer id) {\n        \/\/ \u5148\u4ece\u7f13\u5b58\u83b7\u53d6\u6570\u636e\uff0c\u5982\u679c\u6709\u5219\u76f4\u63a5\u8fd4\u56de\n        \/\/                   \u5982\u679c\u65e0\uff0c\u5219\u67e5\u8be2mysql,\u5e76\u5c06\u6570\u636e\u8bbe\u7f6e\u5230\u7f13\u5b58\n        String key = \"user:\" + id;\n        Object userObj = redisTemplate.opsForValue().get(key);\n        if(userObj == null){\n            synchronized (this.getClass()){\n                userObj = redisTemplate.opsForValue().get(key);\n                if(userObj == null ){\n                    log.debug(\"----&gt; \u67e5\u8be2\u6570\u636e\u5e93..............\");\n                    \/\/ \u67e5\u6570\u636e\u5e93\n                    Emp emp = empMapper.selectByPrimaryKey(id);\n                    redisTemplate.opsForValue().set(key,emp);\n                    return emp;\n                }else{\n                    log.debug(\"----&gt; \u67e5\u8be2\u7f13\u5b58(\u540c\u6b65\u4ee3\u7801\u5757)&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\");\n                    return userObj;\n                }\n            }\n\n        }else{\n            log.debug(\"----&gt; \u67e5\u8be2\u7f13\u5b58&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\");\n        }\n        return userObj;\n    }\n}\n<\/pre>\n<h2>\u6d4b\u8bd5Redis<\/h2>\n<p><strong>Redis-Controller<\/strong><\/p>\n<pre>\n@RestController\npublic class RedisContoller {\n    @Autowired\n    private RedisTemplate redisTemplate;\n\n    @GetMapping(\"\/redis\/get\/{key}\")\n    public Object get(@PathVariable(\"key\") String key){\n       return  redisTemplate.opsForValue().get(key);\n    }\n\n    @PostMapping(\"\/redis\/set\/{key}\/{value}\")\n    public Object set(@PathVariable(\"key\") String key,\n                      @PathVariable(\"value\") String value){\n        redisTemplate.opsForValue().set(key,value);\n        return \"set success\";\n    }\n}\n<\/pre>\n<h2>Controller<\/h2>\n<p><strong>Redis+MySQL<\/strong><\/p>\n<pre>\n@RestController\npublic class EmpController {\n    @Autowired\n    private EmpService empService;\n\n    @PostMapping(\"\/emp\")\n    public String addEmp(Emp emp){\n        empService.add(emp);\n        return \"add ok\";\n    }\n\n    @GetMapping(\"\/emp\/{id}\")\n    public Object getEmpById(@PathVariable(\"id\") Integer id){\n        ExecutorService es = Executors.newFixedThreadPool(200);\n        for(int i=0 ;i\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SpringBoot\u6574\u5408Redi&#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-201226","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201226","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=201226"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201226\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}