springBoot 结合redis(windows环境)
redis 配置
- redis windows下载地址:https://hub.fastgit.org/tporadowski/redis/releases
- 运行redis: redis 目录下运行,redis-server.exe redis.windows.conf
springboot 配置
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
...
application.yml
spring:
redis:
database: 0
host: 127.0.0.1
port: 6379
password:
jedis:
pool:
#连接池最大的连接数(使用负数表示没有限制)
max-active: 20
#连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 1
#连接池最大空闲连接
max-idle: 10
#连接池最小空闲连接数
min-idle: 0
#连接超时时间(毫秒)
timeout: 1000
RedisService.java
@Component
public class RedisService {
@Resource
private StringRedisTemplate stringRedisTemplate;
// 注入stringRedisTemplate 获取ValueOpearations对象,通过对象
// 可读写redis数据库
public void setRedisKV(String key, String valueString) {
ValueOperations<String, String> fValueOperation = stringRedisTemplate.opsForValue();
fValueOperation.set(key, valueString);
}
public String getRedisKV(String keyString) {
String s = stringRedisTemplate.opsForValue().get(keyString);
return s;
}
// string累心存储对象,存储前要把对象转化为json数据类型
public void setRedisObj(User user) {
ValueOperations<String, String> sValueOperations = stringRedisTemplate.opsForValue();
sValueOperations.set("user", JSON.toJSONString(user));
}
public User getRedisObj() {
ValueOperations<String, String> sValueOperations = stringRedisTemplate.opsForValue();
String user = sValueOperations.get("user");
User user1 = JSONObject.parseObject(user, User.class);
return user1;
}
public void setRedisList() {
ListOperations<String, String> listOperations = stringRedisTemplate.opsForList();
listOperations.leftPush("list", "list1");
listOperations.leftPush("list", "list2");
listOperations.leftPush("list", "list3");
listOperations.leftPush("list", "list4");
}
public String getRedisList() {
ListOperations<String, String> sOperations = stringRedisTemplate.opsForList();
return sOperations.leftPop("list");
}
public void setRedisHash() {
HashOperations<String, Object, Object> hashOperations = stringRedisTemplate.opsForHash();
hashOperations.put("hash0", "name0", "ljz0");
hashOperations.put("hash1", "name1", "ljz1");
hashOperations.put("hash2", "name2", "ljz2");
}
public Object getRedisHash() {
HashOperations<String, Object, Object> hashOperations = stringRedisTemplate.opsForHash();
return hashOperations.get("hash0", "name0");
}
}
User.java
@Data
public class User {
private String name;
private Integer age;
private String sex;
}
RedisController.java
@RestController
@RequestMapping("redis")
public class RedisController {
@Autowired
private RedisService redisService;
@GetMapping("test")
public String test(){
return "test";
}
@GetMapping("setRedis/{key}/{name}")
public String setRedis(@PathVariable String key,@PathVariable String name){
redisService.setRedisKV(key, name);
return "success";
}
@GetMapping("getRedis/{key}")
public String getRedis(@PathVariable String key){
return redisService.getRedisKV(key);
}
@GetMapping("setRedisObj")
public String setRedisObj(){
User user=new User();
user.setAge(11);
user.setName("张三");
user.setSex("男");
redisService.setRedisObj(user);
return "success";
}
@GetMapping("getRedisObj")
public User getRedisObj(){
return redisService.getRedisObj();
}
}