springboot整合redis
第一步:创建一个springboot项目,导进redis需要的架包
pom.xml
<!--- redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
第二步:application.properties里面redis的配置信息
#reids相关配置
#redis服务器地址
spring.redis.host=localhost
#雷迪森服务器端口
spring.redis.port=6379
#redis密码,默认为空
spring.redis.password=
#redis数据库索引(默认为0)
spring.redis.database=0
#连接池对打阻塞等待时间(负表示没有限制)
spring.redis.jedis.pool.max-wait=10000
#连接池最大连接数(负表示没有限制)
spring.redis.jedis.pool.max-active=100
#连接池中的最大空闲链接
spring.redis.jedis.pool.max-idle=20
#连接池中的最小空闲链接
spring.redis.jedis.pool.min-idle=0
#链接超时时间
spring.redis.timeout=3000
第三步:配置实体类和操作数据库的接口连接数据库
第四步:添加测试,把数据放进redis或者拿数据出来
package com.codel.redis;
import com.codel.redis.pojo.User;
import com.codel.redis.pojo.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Iterator;
@SpringBootTest
class RedisApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Autowired
UserRepository userRepository;
@Test
void contextLoads() {
//向redis中添加数据
redisTemplate.opsForValue().set("keys", "value值");
//根据键值取出数据
System.out.println(redisTemplate.opsForValue().get("keys"));
}
// 添加数据进去redis数据库里面
@Test
void testRedis() {
User user = new User(1, "codel", "man");
User user2 = new User(2, "codel2", "man");
User user3 = new User(3, "codel3", "man");
User user4 = new User(4, "codel4", "man");
User user5 = new User(5, "codel5", "man");
User save = userRepository.save(user);
User save2 = userRepository.save(user2);
User save3 = userRepository.save(user3);
User save4 = userRepository.save(user4);
User save5 = userRepository.save(user5);
System.out.println(save);
}
// 输出redis里面的所有数据
@Test
public void testAll() {
Iterable<User> all = userRepository.findAll();
Iterator<User> iterator = all.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
// 测试成功:
//User(id=1, name=codel, sex=man)
//User(id=2, name=codel2, sex=man)
//User(id=3, name=codel3, sex=man)
//User(id=4, name=codel4, sex=man)
//User(id=5, name=codel5, sex=man)
}
进去redis的界面化的软件里面查看,数据存储成功
第五步:建立controller类,把数据从redis数据库拿到页面
package com.codel.redis.controller;
import com.codel.redis.pojo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired(required = false)
private RedisTemplate<String, Object> redisTemplate;
@Autowired
UserRepository userRepository;
@GetMapping("/get")
public Object getValue(String key) {
return redisTemplate.opsForValue().get(key);
}
@GetMapping("/getList")
public Object getlistValues() {
return userRepository.findAll();
//测试成功了,在页面输出redis里面user的值
}
}
测试结果: