溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring Data Redis的用法介紹

發(fā)布時間:2021-08-24 18:16:36 來源:億速云 閱讀:190 作者:chen 欄目:大數(shù)據(jù)

本篇內容介紹了“Spring Data Redis的用法介紹”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

Spring Data Redis介紹

Spring Data Redis是Spring官方推出,可以算是Spring框架集成Redis操作的一個子框架,封裝了Redis的很多命令,可以很方便的使用Spring操作Redis數(shù)據(jù)庫,Spring對很多工具都提供了類似的集成,如Spring Data MongDB、Spring Data JPA等,Spring Data Redis只是其中一種。

環(huán)境搭建

要使用SDR,首先需要搭建Spring+SpringMVC環(huán)境,由于這個不是本文的重點,因此這一步我直接略過,Spring+SpringMVC環(huán)境搭建成功后,接下來我們要整合SDR,首先需要添加如下依賴:

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>RELEASE</version>
</dependency>

然后創(chuàng)建在resources目錄下創(chuàng)建redis.properties文件作為redis的配置文件,如下:

redis.host=192.168.248.128
redis.port=6379
redis.maxIdle=300
redis.maxTotal=600
redis.maxWait=1000
redis.testOnBorrow=true

在spring的配置文件中,添加如下bean:

<!--引入redis.properties文件-->
<context:property-placeholder location="classpath:redis.properties"/>
<!--配置連接池信息-->
<bean class="redis.clients.jedis.JedisPoolConfig" id="poolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!--配置基本連接信息-->
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" id="connectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!--配置RedisTemplate-->
<bean class="org.springframework.data.redis.core.RedisTemplate" id="redisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!--key和value要進行序列化,否則存儲對象時會出錯-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>

好了,在Spring中配置了redisTemplate之后,接下來我們就可以在Dao層注入redisTemplate進而使用了。

接下來我們首先創(chuàng)建實體類User,注意User一定要可序列化:

public class User implements Serializable{
private String username;
private String password;
private String id;
//get/set省略
}

然后在Dao層實現(xiàn)數(shù)據(jù)的添加和獲取,如下:

@Repository
public class HelloDao {
@Autowired
RedisTemplate redisTemplate;
public void set(String key, String value) {
ValueOperations ops = redisTemplate.opsForValue();
ops.set(key, value);
}
public String get(String key) {
ValueOperations ops = redisTemplate.opsForValue();
return ops.get(key).toString();
}
public void setuser(User user) {
ValueOperations ops = redisTemplate.opsForValue();
ops.set(user.getId(), user);
}
public User getuser(String id) {
ValueOperations<String, User> ops = redisTemplate.opsForValue();
User user = ops.get(id);
System.out.println(user);
return user;
}
}

SDR官方文檔中對Redistemplate的介紹,通過Redistemplate可以調用ValueOperations和ListOperations等等方法,分別是對Redis命令的高級封裝。但是ValueOperations等等這些命令最終是要轉化成為RedisCallback來執(zhí)行的。也就是說通過使用RedisCallback可以實現(xiàn)更強的功能。

最后,給大家展示下我的Service和Controller,如下:

@Service
public class HelloService {
@Autowired
HelloDao helloDao;
public void set(String key, String value) {
helloDao.set(key,value);
}

public String get(String key) {
return helloDao.get(key);
}

public void setuser(User user) {
helloDao.setuser(user);
}

public String getuser(String id) {
String s = helloDao.getuser(id).toString();
return s;
}
}
Controller:
@Controller
public class HelloController {
@Autowired
HelloService helloService;

@RequestMapping("/set")
@ResponseBody
public void set(String key, String value) {
helloService.set(key, value);
}

@RequestMapping("/get")
@ResponseBody
public String get(String key) {
return helloService.get(key);
}

@RequestMapping("/setuser")
@ResponseBody
public void setUser() {
User user = new User();
user.setId("1");
user.setUsername("深圳");
user.setPassword("sang");
helloService.setuser(user);
}

@RequestMapping(value = "/getuser",produces = "text/html;charset=UTF-8")
@ResponseBody
public String getUser() {
return helloService.getuser("1");
}
}

“Spring Data Redis的用法介紹”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI