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

Case Analysis of springboot Integration redis

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of springboot integration redis case analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this springboot integration redis case analysis article. Let's take a look.

Import redis pom file org.springframework.boot spring-boot-starter-data- redis to write redis configuration spring: redis: password: port: 6379 host: localhost database: 0 jedis: pool: # # maximum number of connections in connection pool (use negative values for unlimited) # spring.redis.pool.max-active=8 Max-active: 8 # # maximum blocking wait time of connection pool (using negative values for unlimited) # spring.redis.pool.max-wait=-1 max-wait:-1 # # maximum idle connection in connection pool # spring.redis.pool.max-idle=8 max-idle: 8 # # minimum idle connection in connection pool # spring.redis.pool.min-idle=0 min-idle: 0 # # connection timeout (milliseconds) lettuce: shutdown-timeout: 0 write springConfig file

Because the storage needs to be serialized, we need to configure the serialization method of redis. If not, both key and value use StringRedisSerializer by default, which can only be used to store data of String type, so we need to configure our commonly used types. At the same time, our Java entity class must inherit the Serializable interface.

@ Configurationpublic class RedisConfig {@ Bean public RedisTemplate redisTemplate (RedisConnectionFactory factory) {RedisTemplate template = new RedisTemplate (); template.setConnectionFactory (factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class); ObjectMapper om = new ObjectMapper (); om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL) / / om.activateDefaultTyping (LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); jackson2JsonRedisSerializer.setObjectMapper (om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer (); / / key uses String serialization method template.setKeySerializer (stringRedisSerializer); / / hash key also uses String serialization method template.setHashKeySerializer (stringRedisSerializer) / / jackson template.setValueSerializer (jackson2JsonRedisSerializer) is used for value serialization; / / jackson template.setHashValueSerializer (jackson2JsonRedisSerializer) is used for value serialization of hash; template.afterPropertiesSet (); return template;}} test redis

Before this step, we need to make sure that the connected redis service is enabled

@ Autowired private RedisTemplate redisTemplate;@Test public void testSelect () throws SQLException {redisTemplate.opsForValue () .set ("qqq", userMapper.findByUname ("zengkaitian")); System.out.println ("obtained in redis:" + redisTemplate.opsForValue () .get ("qqq"));}

Test result

This is the end of the article on "springboot Integration redis case Analysis". Thank you for reading! I believe you all have a certain understanding of the knowledge of "springboot Integration redis case Analysis". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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