转自:
下文笔者讲述SpringBoot中集成Redis的方法分享,如下所示
实现思路:
1.引入redis相关jar包
2.调整application.yml的相关配置
3.编写相应的controller
同时使用redisTemplate操作redis
例:
1、pom文件引入redis架包2、application.yml文件配置redis信息 spring: #redis配置 redis: host: 127.0.0.1 port: 6379 password: lettuce: pool: #最大连接数 max-active: 8 #最大阻塞等待时间 max-wait: 6000 #最大空闲连接 max-idle: 8 #最小空闲连接 min-idle: 0 timeout: 5000 database: 0 3、编写RedisController.java package com.java265.testRedis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value="/redis", produces = "application/json; charset=UTF-8") public class RedisController { @Autowired private RedisTemplate redisTemplate; @RequestMapping( "/set") public String setInfo( String name) { redisTemplate.opsForValue().set("1", name); return "succees"; } @RequestMapping( "/get") public String getInfo( String key) { return redisTemplate.opsForValue().get(key).toString(); } } org.springframework.boot spring-boot-starter-data-redis