springboot使用redis的詳細步驟
springboot使用redis
redis-service.exe : 服務端,啟動后不要關閉
redis-cli.exe : 客戶端,訪問redis中的數據
redisclient-win32.x86_64.2.0.jar : redis的圖形界面客戶端,執(zhí)行方式是在這個文件的目錄執(zhí)行
java -jar redisclient-win32.x86_64.2.0.jar
或者在這個jar包的目錄下點擊即可直接執(zhí)行
點擊server,點擊add,設置端口號就可以訪問redis了
springboot使用redis步驟
添加依賴
redis起步依賴,導入后可以直接使用RedisTemplate
RedisTemplate實際上使用的是lettuce客戶端庫
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
在application.properties中配置redis連接
#指定redis信息 (如 host, ip, password) spring.redis.host=localhost spring.redis.port=6379 #沒有密碼可以不用配置這個 #spring.redis.password=123456
使用redisTemplate來訪問redis服務器
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class RedisController { /** * 需要注入redis模板 * * 對于RedisTemplate的泛型情況, * 可以使用<String, String> * <Object, Object> * 或者不寫泛型 * * 注意,屬性的名稱必須為redisTemplate,因為按名稱注入,框架創(chuàng)建的對象就是這個名字的 */ @Resource private RedisTemplate redisTemplate; @Resource private StringRedisTemplate stringRedisTemplate; // 添加數據到redis @PostMapping("/redis/addstring") public String addToRedis(String name, String value) { // 操作Redis中的string類型的數據,先獲取ValueOperation ValueOperations valueOperations = redisTemplate.opsForValue(); // 添加數據到redis valueOperations.set(name, value); return "向redis添加string類型的數據"; } // 從redis獲取數據 @GetMapping("/redis/getk") public String getData(String key) { ValueOperations valueOperations = redisTemplate.opsForValue(); Object v = valueOperations.get(key); return "key是" + key + ",它的值是:" + v; } @PostMapping("/redis/{k}/{v}") public String addStringKV(@PathVariable String k, @PathVariable String v) { // 使用StringRedisTemplate對象 stringRedisTemplate.opsForValue().set(k,v); return "使用StringRedisTemplate對象添加"; } @GetMapping("/redis/{k}") public String getStringValue(@PathVariable String k) { // 獲取String類型的value String v = stringRedisTemplate.opsForValue().get(k); return "從redis中通過" + k + "獲取到string類型的v=" + v; } }
redisTemplate對象有好幾種,上面代碼中給了兩種
一種為RedisTemplate,這種是有泛型的,泛型類型為<String, String> 或者 <Object, Object> 或者不添加泛型,當用它來向redis服務器中存入String類型的數據時,會出現亂碼
使用postman
明明存入成功了,在redis中查詢的時候是帶有亂碼前綴的
直接通過"lisi"它的值是可以取出的
可見在RedisTemplate在存取中做了手腳.
當我們使用StringRedisTemplate對象存取String類型的數據時,是沒有亂碼的
存
取
redis服務器中的數據
數據正常展示
StringRedisTemplate和RedisTemplate
上面說到了這兩者在存取中的差異
- StringRedisTemplate : 這個類將key和value都做String處理,使用的是String的序列化,可讀性好
- RedisTemplate : 把key和value經過了序列化,key和value都是序列化的內容,不能直接識別,默認使用的是JDK的序列化,可以修改為其他的序列化
序列化作用 :
序列化是將對象轉換為可傳輸字節(jié)序列的過程,反序列化是將字節(jié)序列還原為原對象的過程.序列化最終的目的是為了對象可以跨平臺存儲和進行網絡的傳輸
序列化的方式 :
序列化只是一種拆裝對象的規(guī)則,那么這種規(guī)則也就多種多樣,常見的有JDK(不支持跨語言),json,xml,Hessian等
我們上面的RedisTemplate類的存儲就是JDK方式的
jdk方式的序列化
java的序列化 : 把java對象轉換為byte[],二進制數據
json序列化 : json序列化功能將對象轉換為json格式或者將其轉換回對象,如Student對象轉換為{“name”:“張三”,“age”:“20”}
序列化的方式可以改變
/** 設置RedisTemplate序列化機制 * 可以設置 key 的序列化,也可以設置 value 的序列化 * 也可以同時設置 */ @PostMapping("/redis/addstr") public String addString(String k, String v) { // 設置RedisTemplate的序列化機制 // 設置key為string類型的序列化 redisTemplate.setKeySerializer(new StringRedisSerializer()); // 設置value的序列化 redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.opsForValue().set(k, v); return "添加了k和v"; }
使用json方式的序列化
創(chuàng)建實體類,需要實現序列化接口,最好有序列化的UID
import java.io.Serializable; public class Student implements Serializable { private static final long serialVersionUID = -7839813688155519106L; private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
idea生成序列化UID的方式,需要先在setting中開啟,如下
然后將光標放在類名上,alt+enter
在方法中設置序列化方式
/** * 使用json序列化 */ @PostMapping("/redis/addjson") public String addJson() { Student student = new Student(); student.setName("zhangsan"); student.setAge(20); student.setId(1); // 設置key為string的序列化方式 redisTemplate.setKeySerializer(new StringRedisSerializer()); // 設置value為json的序列化方式,json為Student類型的方式組織,所以需要傳入Student.class redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class)); redisTemplate.opsForValue().set("myStudent", student); return "存入json類型的數據student"; }
測試訪問url
在redis服務端檢查數據
果然,數據的value已經以json的方式存入內存中了.
使用json的方式反序列化將數據取出
/** * 使用json序列化 */ @PostMapping("/redis/addjson") public String addJson() { Student student = new Student(); student.setName("zhangsan"); student.setAge(20); student.setId(1); // 設置key為string的序列化方式 redisTemplate.setKeySerializer(new StringRedisSerializer()); // 設置value為json的序列化方式,json為Student類型的方式組織,所以需要傳入Student.class redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class)); redisTemplate.opsForValue().set("myStudent", student); return "存入json類型的數據student"; }
反序列化的時候必須得指定序列化的方式,要不然不能取出數據
總結
到此這篇關于springboot使用redis的文章就介紹到這了,更多相關springboot使用redis內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Mybatis-Plus字段策略FieldStrategy的使用
本文主要介紹了Mybatis-Plus字段策略FieldStrategy的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08SpringBoot2使用JTA組件實現基于JdbcTemplate多數據源事務管理(親測好用)
這篇文章主要介紹了SpringBoot2使用JTA組件實現基于JdbcTemplate多數據源事務管理(親測好用),在Spring?Boot?2.x中,整合了這兩個JTA的實現分別是Atomikos和Bitronix,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07