查了很多资料都是配置文件的 公司需要同时操作多个数据库 所以搞了半天我的处理方式如下也许有点笨,自己根绝实际情况封装,大神请绕道
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91package com.rayclear; import com.rayclear.common.RedisObjectSerializer; import com.rayclear.domain.Users; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; /** * * @author zhoushuai */ @RunWith(SpringRunner.class) @SpringBootTest public class RedisTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate<String, Object> redisTemplate; @Test public void test() throws Exception { //保存对象 springboot 默认是在配置文件里初始化数据库 // Users u = new Users(); // u.setActivities_count(656); // u.setNickname("对象名zhoushuai"); // redisTemplate.opsForValue().set("users", u); // u = (Users) redisTemplate.opsForValue().get("users"); // System.out.println(u.getNickname()); //手动改 // JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); // redisConnectionFactory.setHostName("127.0.0.1"); // redisConnectionFactory.setPort(6379); // redisConnectionFactory.setDatabase(0); // redisConnectionFactory.afterPropertiesSet(); // redisTemplate.setConnectionFactory(redisConnectionFactory); // redisTemplate.opsForValue().set("zhoushuai", "hahahahhahahahhahah"); redisTemplate = RedisDbInit.initRedis(0,redisTemplate); redisTemplate.opsForValue().set("new", "hahahahhahahahhahah"); System.out.println(redisTemplate.opsForValue().get("new")); Users u2 = new Users(); u2.setNickname("u2对象名zhoushuai"); redisTemplate.opsForValue().set("u2", u2); Users users2 = (Users)redisTemplate.opsForValue().get("u2"); System.out.println(users2.getNickname()); redisTemplate = RedisDbInit.initRedis(1,redisTemplate); redisTemplate.opsForValue().set("u3", u2); Users users3 = (Users)redisTemplate.opsForValue().get("u3"); System.out.println(users3.getNickname()); } } /** * redis 数据库切换 * @author zhoushuai */ public class RedisDbInit { public static RedisTemplate<String, Object> initRedis(Integer indexDb, RedisTemplate<String, Object> redisTemplate){ JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); redisConnectionFactory.setHostName("127.0.0.1"); redisConnectionFactory.setPort(6379); redisConnectionFactory.setDatabase(indexDb); redisConnectionFactory.afterPropertiesSet(); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } }
最后
以上就是冷傲招牌最近收集整理的关于spring boot 同时操作redis不同数据库的全部内容,更多相关spring内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复