1: Springboot整合Redis是在上一篇Springboot整合Mybatis的基础上进行的,链接
https://blog.csdn.net/tangmingxin0529/article/details/81106359
2:新增依赖
spring-boot-starter-data-redis 为redis依赖
com.alibaba.fastjson 是json依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
3:application.yml的配置
server:
port: 8080
context-path: /springbootMybatis
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8
username: root
password: mysql
redis:
database: 1
host: 127.0.0.1
port: 6379
password:
pool:
#连接池最大连接数[使用负数表示没有限制]
max-active: 1000
#连接池最大阻塞等待时间[使用负数表示没有限制]
max-wait: -1
#连接池最大空闲连接
max-idle: 10
#连接池最小空闲连接
min-idle: 2
#连接超时时间
timeout: 0
mybatis:
mapperLocations: classpath:/mapper/UserMapper.xml
typeAliasesPackage: com.springbmybatis.entity
4:Controller
import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.springbmybatis.entity.User;
import com.springbmybatis.service.UserService;
@RestController
@RequestMapping(value="/redisController")
public class RedisController {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource
private UserService userService;
@GetMapping(value="/redisTest")
public String redisTest() {
stringRedisTemplate.opsForValue().set("name", "我是redis");
String string = stringRedisTemplate.opsForValue().get("name");
return string;
}
//将数据库参数存入缓存再取出
@GetMapping(value="/redisGetUser/{id}")
public String redisGetUser(@PathVariable("id") Integer userId) {
User user = userService.findUserById(userId);
stringRedisTemplate.opsForValue().set("user",JSON.toJSONString(user));
String userInfo = stringRedisTemplate.opsForValue().get("user");
return userInfo;
}
//直接从缓存中获取值
@GetMapping(value="/redisGetUser1/{id}")
public String redisGetUser1() {
String userInfo = stringRedisTemplate.opsForValue().get("user");
return userInfo;
}
//放入一个集合
@GetMapping(value="/listUser")
public String listUser(){
List<User> findAllUser = userService.findAllUser();
stringRedisTemplate.opsForValue().set("userList", JSON.toJSONString(findAllUser));
String listUserInfo = stringRedisTemplate.opsForValue().get("userList");
return listUserInfo;
}
@GetMapping(value="/listUser1")
public String listUser1(){
String listUserInfo = stringRedisTemplate.opsForValue().get("userList");
return listUserInfo;
}
}
需要注意的是采用StringRedisTemplate只能存放String数据类型,所以这里采用了将对象[或者对象集合]转换为JSON字符串
5:启动demo
访问1: http://127.0.0.1:8080/springbootMybatis/redisController/redisTest
访问2:http://127.0.0.1:8080/springbootMybatis/redisController/redisGetUser/1
访问3: http://127.0.0.1:8080/springbootMybatis/redisController/listUser
以上这3个示例,都是将值先存放入缓存中,再取出.下面我们直接从缓存中取值
访问4: http://127.0.0.1:8080/springbootMybatis/redisController/listUser1