建立一个springboot项目,什么也不用设置,建立一个最简单的就可以的,首先还是加入我们的Maven驱动包
<!-- 加入redis连接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
加入我们的redis的配置信息,放置在application.yml文件下即可。
spring:
redis:
host: 192.168.31.101
port: 6379
password: 123456
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 500
min-idle: 0
lettuce:
shutdown-timeout: 0
我们直接来上测试类吧
package com.abbey.myblog.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.redis.core.StringRedisTemplate;
/**
* @author lianying
* @create 2020-11-22 3:43 下午
**/
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping(value = "getUserByRedis")
public String getIndex(){
stringRedisTemplate.opsForValue().set("xiaocai", "888");
String res = stringRedisTemplate.opsForValue().get("xiaocai");
System.out.println(res);
return res;
}
}
浏览器输入地址验证
直接自动装配一下StringRedisTemplate即可,剩下的就是redis操作了,五种数据的基本操作如下。
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
版权声明:本文为sanmi8276原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。