springboot使用redis的詳細(xì)步驟
springboot使用redis
redis-service.exe : 服務(wù)端,啟動后不要關(guān)閉
redis-cli.exe : 客戶端,訪問redis中的數(shù)據(jù)
redisclient-win32.x86_64.2.0.jar : redis的圖形界面客戶端,執(zhí)行方式是在這個文件的目錄執(zhí)行
java -jar redisclient-win32.x86_64.2.0.jar
或者在這個jar包的目錄下點(diǎn)擊即可直接執(zhí)行

點(diǎn)擊server,點(diǎn)擊add,設(shè)置端口號就可以訪問redis了
springboot使用redis步驟
添加依賴

redis起步依賴,導(dǎo)入后可以直接使用RedisTemplate
RedisTemplate實(shí)際上使用的是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服務(wù)器
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;
// 添加數(shù)據(jù)到redis
@PostMapping("/redis/addstring")
public String addToRedis(String name, String value) {
// 操作Redis中的string類型的數(shù)據(jù),先獲取ValueOperation
ValueOperations valueOperations = redisTemplate.opsForValue();
// 添加數(shù)據(jù)到redis
valueOperations.set(name, value);
return "向redis添加string類型的數(shù)據(jù)";
}
// 從redis獲取數(shù)據(jù)
@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> 或者不添加泛型,當(dāng)用它來向redis服務(wù)器中存入String類型的數(shù)據(jù)時,會出現(xiàn)亂碼
使用postman

明明存入成功了,在redis中查詢的時候是帶有亂碼前綴的


直接通過"lisi"它的值是可以取出的

可見在RedisTemplate在存取中做了手腳.
當(dāng)我們使用StringRedisTemplate對象存取String類型的數(shù)據(jù)時,是沒有亂碼的
存

取

redis服務(wù)器中的數(shù)據(jù)

數(shù)據(jù)正常展示

StringRedisTemplate和RedisTemplate
上面說到了這兩者在存取中的差異
- StringRedisTemplate : 這個類將key和value都做String處理,使用的是String的序列化,可讀性好
- RedisTemplate : 把key和value經(jīng)過了序列化,key和value都是序列化的內(nèi)容,不能直接識別,默認(rèn)使用的是JDK的序列化,可以修改為其他的序列化
序列化作用 :
序列化是將對象轉(zhuǎn)換為可傳輸字節(jié)序列的過程,反序列化是將字節(jié)序列還原為原對象的過程.序列化最終的目的是為了對象可以跨平臺存儲和進(jìn)行網(wǎng)絡(luò)的傳輸
序列化的方式 :
序列化只是一種拆裝對象的規(guī)則,那么這種規(guī)則也就多種多樣,常見的有JDK(不支持跨語言),json,xml,Hessian等
我們上面的RedisTemplate類的存儲就是JDK方式的
jdk方式的序列化

java的序列化 : 把java對象轉(zhuǎn)換為byte[],二進(jìn)制數(shù)據(jù)
json序列化 : json序列化功能將對象轉(zhuǎn)換為json格式或者將其轉(zhuǎn)換回對象,如Student對象轉(zhuǎn)換為{“name”:“張三”,“age”:“20”}
序列化的方式可以改變
/** 設(shè)置RedisTemplate序列化機(jī)制
* 可以設(shè)置 key 的序列化,也可以設(shè)置 value 的序列化
* 也可以同時設(shè)置
*/
@PostMapping("/redis/addstr")
public String addString(String k, String v) {
// 設(shè)置RedisTemplate的序列化機(jī)制
// 設(shè)置key為string類型的序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 設(shè)置value的序列化
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.opsForValue().set(k, v);
return "添加了k和v";
}
使用json方式的序列化
創(chuàng)建實(shí)體類,需要實(shí)現(xiàn)序列化接口,最好有序列化的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中開啟,如下

然后將光標(biāo)放在類名上,alt+enter

在方法中設(shè)置序列化方式
/**
* 使用json序列化
*/
@PostMapping("/redis/addjson")
public String addJson() {
Student student = new Student();
student.setName("zhangsan");
student.setAge(20);
student.setId(1);
// 設(shè)置key為string的序列化方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 設(shè)置value為json的序列化方式,json為Student類型的方式組織,所以需要傳入Student.class
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));
redisTemplate.opsForValue().set("myStudent", student);
return "存入json類型的數(shù)據(jù)student";
}
測試訪問url

在redis服務(wù)端檢查數(shù)據(jù)


果然,數(shù)據(jù)的value已經(jīng)以json的方式存入內(nèi)存中了.
使用json的方式反序列化將數(shù)據(jù)取出
/**
* 使用json序列化
*/
@PostMapping("/redis/addjson")
public String addJson() {
Student student = new Student();
student.setName("zhangsan");
student.setAge(20);
student.setId(1);
// 設(shè)置key為string的序列化方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 設(shè)置value為json的序列化方式,json為Student類型的方式組織,所以需要傳入Student.class
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));
redisTemplate.opsForValue().set("myStudent", student);
return "存入json類型的數(shù)據(jù)student";
}

反序列化的時候必須得指定序列化的方式,要不然不能取出數(shù)據(jù)

總結(jié)
到此這篇關(guān)于springboot使用redis的文章就介紹到這了,更多相關(guān)springboot使用redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis中執(zhí)行相關(guān)SQL語句的方法
本文主要介紹了MyBatis中執(zhí)行相關(guān)SQL語句的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
JavaEE實(shí)現(xiàn)前后臺交互的文件上傳與下載
這篇文章主要介紹了JavaEE實(shí)現(xiàn)前后臺交互的文件上傳與下載,分享相關(guān)技術(shù),實(shí)現(xiàn)文件上傳下載功能,需要的朋友可以參考下2015-11-11
Mybatis-Plus字段策略FieldStrategy的使用
本文主要介紹了Mybatis-Plus字段策略FieldStrategy的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
SpringBoot2使用JTA組件實(shí)現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測好用)
這篇文章主要介紹了SpringBoot2使用JTA組件實(shí)現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測好用),在Spring?Boot?2.x中,整合了這兩個JTA的實(shí)現(xiàn)分別是Atomikos和Bitronix,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Java實(shí)現(xiàn)創(chuàng)建Zip壓縮包并寫入文件
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)創(chuàng)建Zip壓縮包并寫入文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01

