Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to switch redis Library in Springboot2.X

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Use the Redis native method to use 16 databases at the same time, and continue to use the above pom file dependency

9. Redis tool class

Remember: the next version for Springboot2.1.X, including 2.0.X, 2.2.X, currently does not switch databases through the following.

Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Component;@Componentpublic class RedisUtil {@ Autowired private StringRedisTemplate redisTemplate; public void setRedisTemplate (StringRedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;} public void setDataBase (int num) {LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory () If (connectionFactory! = null & & num! = connectionFactory.getDatabase ()) {connectionFactory.setDatabase (num); this.redisTemplate.setConnectionFactory (connectionFactory); connectionFactory.resetConnection ();}} public StringRedisTemplate getRedisTemplate () {return this.redisTemplate;} public void set (String key, String value) {redisTemplate.opsForValue () .set (key, value) } / / get the value of the specified key public String get (String key) {return redisTemplate.opsForValue (). Get (key);}} 10, Controller test @ AutowiredRedisUtil redisUtil;@GetMapping ("/ test") public String test (String key) {for (int I = 1; I < 3; iSue +) {redisUtil.setDataBase (I); redisUtil.set (key, "" + I + key) } / / the development environment uses the following code, which simply tests to different redis databases / / redisUtil.setDataBase (RedisPartition.EIGHT.getOrdinal ()); return redisUtil.get (key);}

11. Summary

There are two ways to switch databases between jedis and StringRedisTemplate. I prefer to use jedis. With SringRedisTemplate, I don't copy all the redis methods too much.

12. Attach

The following is the configuration of ssm framework switching to use redis database at the same time, as for redis.xml and redis.properties to copy from the Internet.

@ Resource (name= "stringRedisTemplate") private StringRedisTemplate stringRedisTemplate;private StringRedisTemplate choseConnection (RedisConnectionEnum redisEnum) {JedisConnectionFactory factory = (JedisConnectionFactory) stringRedisTemplate.getConnectionFactory (); factory.setDatabase (redisEnum.getDbNum ()); stringRedisTemplate.setConnectionFactory (factory); return stringRedisTemplate;} is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report