日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Spring Boot整合Spring Data Redis-存取JSON格式Java对象

發布時間:2024/4/13 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot整合Spring Data Redis-存取JSON格式Java对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如何通過Spring Data Redis,以JSON格式來存儲我們的實體對象,其實上節課我們講了基于JDK的序列化器,來幫助我們做一個序列化,做一個對象的存儲,那么這種方式需要注意一點的是,如果我們用的是JDK的序列化器,來對我們的對象做序列化,存到Redis當中的時候,對于序列化以后的結果,相比較JSON格式,起碼要比JSON格式大5倍以上,就是在Redis當中存的內容,要比我們以JSON格式轉換完的內容,要大5倍以上,所以這樣就會對Redis空間造成一個浪費的一個問題,所以這個轉換的方式,根據自己的開發情況來做選擇,他有這樣的一個缺陷,我們怎么以JSON的格式來存儲我們的對象,首先我們在測試類當中,直接去寫一個測試方法,基于JSON格式存/*** 基于JSON格式存Users對象*/ @Test public void testSetUsersUseJSON(){Users users = new Users();users.setAge(20);users.setName("李四豐");users.setId(1);this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));this.redisTemplate.opsForValue().set("users_json", users); }你想我們要把對象做存儲,之前是需要這個對象,所以我先把這個對象給拿過來,我們給改個名,我們這里肯定還要換一個序列化器,還是打開這個包org.springframework.data.redis.serializer看我們的序列化器,這里其實提供了一個JacksonJsonRedisSerializer,有兩個,一個是Jackson,一個是Jackson2,這里我們需要注意的,不帶2的已經過時了/*** {@link RedisSerializer} that can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson's</a>* {@link ObjectMapper}.* <p>* This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances.* <b>Note:</b>Null objects are serialized as empty arrays and vice versa.* * @author Costin Leau* @author Thomas Darimont* @author Christoph Strobl* @deprecated ince 1.7. Will be removed in subsequent version.*/ @Deprecated public class JacksonJsonRedisSerializer<T> implements RedisSerializer<T> {我們可以使用Jackson2JsonRedisSerializer,還有GenericJackson2JsonRedisSerializer,我們用哪個都可以,但是千萬不要用JacksonJsonRedisSerializer,用這個org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer<T>來做,我們肯定要切換序列化器this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));你得告訴我類的信息,這樣就可以對我們的User對象進行JSON轉換了,添加到redis當中,比如我們的這個key叫user_json,然后value就是我們要做匹配的對象,看我們是否可以以JSON的格式存到redis當中,測試通過,我們打開可視化工具,我們可以看到value是沒有問題的,以JSON格式來存儲的,這是存放,我們再來看取出@Test public void testGetUseJSON(){this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));Users users = (Users)this.redisTemplate.opsForValue().get("users_json");System.out.println(users); }我們還是先設置一個序列化器,現在不僅僅是序列化器相同的問題了,還有后面的模板也必須相同,返回來的時候也是User.class,然后去取里面的內容,沒有問題的,現在這個對象反序列化回來,他是可以正常輸出的,以上就是通過redisTemplate,設置一個新的序列話化器,來對我們User對象做JSON處理,以及JSON格式的反序列化處理,所以這個操作還是比較簡單的,只是最關鍵的一點是,序列化器的一個使用而已 spring.redis.pool.max-idle=10 spring.redis.pool.min-idle=5 spring.redis.pool.max-total=20 # #aaa.bbb.max-idle=10 #aaa.bbb.min-idle=5 #aaa.bbb.max-total=20spring.redis.hostName=10.40.8.152 spring.redis.port=6379 spring.redis.password=1234 package com.learn.config;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer;import redis.clients.jedis.JedisPoolConfig;/*** 完成對Redis的整合的一些配置***/ @Configuration public class RedisConfig {/*** 1.創建JedisPoolConfig對象。在該對象中完成一些鏈接池配置* @ConfigurationProperties:會將前綴相同的內容創建一個實體。*/@Bean@ConfigurationProperties(prefix="spring.redis.pool") // @ConfigurationProperties(prefix="aaa.bbb")public JedisPoolConfig jedisPoolConfig(){JedisPoolConfig config = new JedisPoolConfig();/*//最大空閑數config.setMaxIdle(10);//最小空閑數config.setMinIdle(5);//最大鏈接數config.setMaxTotal(20);*/System.out.println("默認值:"+config.getMaxIdle());System.out.println("默認值:"+config.getMinIdle());System.out.println("默認值:"+config.getMaxTotal());return config;}/*** 2.創建JedisConnectionFactory:配置redis鏈接信息*/@Bean@ConfigurationProperties(prefix="spring.redis")public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){System.out.println("配置完畢:"+config.getMaxIdle());System.out.println("配置完畢:"+config.getMinIdle());System.out.println("配置完畢:"+config.getMaxTotal());JedisConnectionFactory factory = new JedisConnectionFactory();//關聯鏈接池的配置對象factory.setPoolConfig(config);//配置鏈接Redis的信息//主機地址/*factory.setHostName("10.40.8.152");//端口factory.setPort(6379);*/return factory;}/*** 3.創建RedisTemplate:用于執行Redis操作的方法*/@Beanpublic RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){RedisTemplate<String, Object> template = new RedisTemplate<>();//關聯template.setConnectionFactory(factory);//為key設置序列化器template.setKeySerializer(new StringRedisSerializer());//為value設置序列化器template.setValueSerializer(new StringRedisSerializer());return template;} } package com.learn.test;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.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.learn.RedisApp; import com.learn.pojo.Users;@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes=RedisApp.class) public class RedisJSONSerializationTest {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;/*** 基于JSON格式存Users對象*/@Testpublic void testSetUsersUseJSON(){Users users = new Users();users.setAge(20);users.setName("李四豐");users.setId(1);this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));this.redisTemplate.opsForValue().set("users_json", users);}/*** 基于JSON格式取Users對象*/@Testpublic void testGetUseJSON(){this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));Users users = (Users)this.redisTemplate.opsForValue().get("users_json");System.out.println(users);} }

?

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的Spring Boot整合Spring Data Redis-存取JSON格式Java对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。