最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
springboot连接redis并动态切换database从db0到db15
时间:2026-06-19 08:51:54 编辑:袖梨 来源:一聚教程网
redis
redis db0到db15springboot连接redis添加配置文件application.properties测试是否连接成功redis动态切换databaseredis db0到db15
可以理解为数据库表这是redis默认提供的16个表
我们可以把不同的数据存在不同的db上
取得时候可以在不同的db拿到不同类型数据
springboot连接redis
pom.xml文件中引入spring-boot-starter-redis,
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId><version>1.3.8.RELEASE</version></dependency>
添加配置文件application.properties
# Redis数据库索引(默认为0)spring.redis.database=0# Redis服务器地址spring.redis.host=localhost# Redis服务器连接端口spring.redis.port=6379# Redis服务器连接密码(默认为空)spring.redis.password=# 连接池最大连接数(使用负值表示没有限制) 默认 8spring.redis.lettuce.pool.max-active=8# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1spring.redis.lettuce.pool.max-wait=-1# 连接池中的最大空闲连接 默认 8spring.redis.lettuce.pool.max-idle=8# 连接池中的最小空闲连接 默认 0spring.redis.lettuce.pool.min-idle=0
测试是否连接成功
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = Application.class)public class TestCRedis{protected static Logger LOGGER = LoggerFactory.getLogger(TestCRedis.class);@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Testpublic void t1(){ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();stringStringValueOperations.set("test","大湿胸");String testkey = stringStringValueOperations.get("test");LOGGER.info(testkey);}}
运行TestCRedis.t1(),控制台打印“大湿胸”redis连接成功
redis动态切换database
package com.niceteam.web.bm;import com.niceteam.common.excel.ExcelDownloadUtil;import org.junit.Test;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;@RunWith(SpringRunner.class)@SpringBootTestpublic class NiceteamWebBasemanagerApplicationTests {private static Logger log = LoggerFactory.getLogger(ExcelDownloadUtil.class);@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Testpublic void contextLoads() {for (int i = 0; i <= 2; i++) {LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory) stringRedisTemplate.getConnectionFactory();lettuceConnectionFactory.setDatabase(i);stringRedisTemplate.setConnectionFactory(lettuceConnectionFactory);lettuceConnectionFactory.resetConnection();ValueOperations valueOperations = stringRedisTemplate.opsForValue();valueOperations.set(i+"",i+"");String test = (String) valueOperations.get(i+"");log.info(test);System.out.println("拿到的数据"+test);}}}
相关文章
- PCL2启动器如何改壁纸 08-21
- 小米ax6000路由器怎么设置穿墙(小米ax6000路由器设置穿墙方法) 08-21
- excel工具栏如何还原 08-21
- OpenClaw请求超时llm request timed out的三种解决方案及完整排查流程实用指南 08-21
- 蜘蛛纸牌如何更改游戏难度 08-21
- 蜘蛛纸牌有什么初级玩法 08-21