javascript
Springboot整合redis(lettuce)
springboot 整合redis(lettuce)
首先確保電腦上裝了redis。最好能用redisDesktop查看一下數(shù)據(jù)情況
- 對于StringRedisTemplate,繼承RedisTemplate,只能存String類型的數(shù)據(jù)。不方面存對象,一般用的不是特別多(特定場景除外)。
比較麻煩,存入的key和value都是字符型。 - 對于RedisTemplate,默認是類型。如果不做配置,他會對key和value默認序列化成二進制文件看不懂。不利于觀察。一般重新配置redisTeamplate的一個bean。設置這個bean的key,value, 以及hashkey等等的序列化方式。**一般是key序列化為string,value序列化為json,**這樣,你就可以在存入對象時候它自動幫你序列化成字符或者json。方便觀察。
賬號密碼之類。在重寫RedisTemplate這個bean。配置他的序列化規(guī)則。注意就是注入bean的時候名稱要和重寫的名一樣。因為這個對象是 spring幫你生成好的給你使用。而如果你隨便瞎起名字的話spring會根據(jù)原始重新生成對象。相當于引用未序列化的redisTemplate。
springboot官方的整合redis這么說的
10. You can also register an arbitrary number of beans that implement LettuceClientConfigurationBuilderCustomizer for more advanced customizations. If you use Jedis, JedisClientConfigurationBuilderCustomizer is also available.
11. If you add your own @Bean of any of the auto-configured types, it replaces the default (except in the case of RedisTemplate, when the exclusion is based on the bean name, redisTemplate, not its type). By default, if commons-pool2 is on the classpath, you get a pooled connection factory.
項目的目錄為
12. 首先,在創(chuàng)建Springboot項目時候勾選redis選項或者在pom.xml中添加:
user類:
controller
import com.redis.pojo.user; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class redisController {private final static Logger log= LoggerFactory.getLogger(redisController.class);/*注入的名稱要和RedisConfiguration的名稱一致否則注入的默認的redisTemplate如果不喜歡序列化可以直接給config文件夾刪除,哈哈*/@Autowired(required = false)RedisTemplate redisCacheTemplate;//如果RedisTemplate redisTemplate就相當沒配置序列化等@Autowired(required = false)StringRedisTemplate stringRedisTemplate;@Autowired(required = false)RedisTemplate redisTemplate;@GetMapping("test1")//StringRedistemplate 存Stringpublic String test1(){stringRedisTemplate.opsForValue().set("test1","hello賽賽");//再desttop中可以看到為中文串stringRedisTemplate.opsForValue().set("redis:test1","hello賽賽");//再desttop中可以看到為中文串redisTemplate.opsForValue().set("test1","hello賽賽");//再desttop中可以看到為二進制數(shù)字亂起八糟String val1=stringRedisTemplate.opsForValue().get("test1");//String va2=(String) redisCacheTemplate.opsForValue().get("test1");//不注釋會報異常,因為Stingxxtemplate就是String沒有序列化過。反序列化會報異常log.info(val1+" ");return val1+" ";}@GetMapping("test2")//Redistemplate 存Stringpublic String test2(){redisCacheTemplate.opsForValue().set("bigsai222","hello賽賽2");String val1=stringRedisTemplate.opsForValue().get("bigsai222");//為 "hello賽賽2" 多個引號String va2=(String) redisCacheTemplate.opsForValue().get("bigsai222");//為 hello賽賽2 沒有引號因為json反序列化log.info(val1+" "+va2);return val1+" "+va2;}@GetMapping("test3")public user test3(){user user=new user("大佬","點個star謝謝!","man");redisCacheTemplate.opsForValue().set("user",user);user value=(user) redisCacheTemplate.opsForValue().get("user");log.info(redisCacheTemplate.hasKey("user")+"");//判斷是否有鍵值對return value;} }test類
package com.redis;import com.redis.pojo.user; 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.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner;import java.io.Serializable;@RunWith(SpringRunner.class) @SpringBootTest public class RedisApplicationTests {private final static Logger log= LoggerFactory.getLogger(RedisApplicationTests.class);@Autowired(required = false)RedisTemplate redisCacheTemplate;@Autowired(required = false)RedisTemplate redisTemplate;@Testpublic void contextLoads() {}@Testpublic void test1(){redisCacheTemplate.opsForValue().set("hello","heoolo");log.warn((String) redisCacheTemplate.opsForValue().get("hello"));}@Testpublic void test2(){user usr=new user("66","賽格","man");redisCacheTemplate.opsForValue().set("bigsai",usr);user user2=(user) redisCacheTemplate.opsForValue().get("bigsai");log.warn(user2.toString());}@Testpublic void test3(){user usr=new user("66","賽格","man");redisTemplate.opsForValue().set("bigsai33",usr);user user2=(user) redisTemplate.opsForValue().get("bigsai33");log.warn(user2.toString());}}測試情況:
- 還有其他的test和controller可以自行訪問體驗其中差異。最好用redis的客戶端工具觀察。
- 如果不想配置序列化直接使用即可,不需要redis配置類(不利于觀察不推薦)!
- 你可以看到已經(jīng)成功了,但是你可能會發(fā)現(xiàn)redis存儲對象的方式是json格式。其實redis本身不支持object類型對象,我們通過中間json達到存儲object效果。
- 你可能還會有疑問,為啥redis模板不是而是。這就涉及到java傳輸信息時候的序列化和接收的反序列化。他可以完美的序列化,反序列化。關于序列化的意義和內(nèi)容更多可以百度,在聲明User類的時候,它繼承了序列化接口,那么可以直接這樣寫。這個思想可以了解下java面向對象繼承的向上繼承和向下繼承。
- 另外,redis還可以操作一些其他類型
-
下列的就是Redis其它類型所對應的操作方式:
opsForValue: 對應 String(字符串)
opsForZSet: 對應 ZSet(有序集合)
opsForHash: 對應 Hash(哈希)
opsForList: 對應 List(列表)
opsForSet: 對應 Set(集合)
opsForGeo: 對應 GEO(地理位置)
那么springboot和mybatis,redis已經(jīng)整合完畢了。你可以在項目中存取一些適合key-value的數(shù)值,并且使用起來相比mysql也要方便一些。
項目github地址
如果描述錯誤還請大佬指正?!
- 如果對后端、爬蟲、數(shù)據(jù)結構算法等感性趣歡迎關注我的個人公眾號交流:bigsai
總結
以上是生活随笔為你收集整理的Springboot整合redis(lettuce)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两种欧拉函数模板
- 下一篇: Springboot整合shiro基于u