爲什麼要學習在spring中使用redis,一開始我們是用jedis去操作redis的,但是redis只提供基於字符串的操作,而在java中使用的卻是以類對象爲主,所以需要redis存儲的字符串和java對象相互轉化。如果自己編寫規則,很麻煩,而spring中封裝了這些東西,還提供了工具類,所以學習在spring中使用redis,很有必要。
1.準備的東西,使用的是maven(pom.xml)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.taylor</groupId>
<artifactId>Spring-redis</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>
2.完善applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<!--spring的配置代碼 -->
<!--首先配置JedisPoolConfig對象 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空閒數 -->
<property name="maxIdle" value="100"/>
<!-- 最大連接數 -->
<property name="maxTotal" value="200"/>
<!-- 最大等待時間 -->
<property name="maxWaitMillis" value="20000"/>
</bean>
<!--配置JedisConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
<!-- <property name="password" value=""/> -->
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!-- 配置spring RedisTemplate -->
<!--鍵序列器 -->
<bean id="jdkSerializationRedisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<!-- 值序列器-->
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
</beans>
3.測試代碼和運行截圖
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import com.taylor.pojo.Role;
public class TestJedis {
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate =applicationContext.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1L);
role.setRoleName("taylor");
role.setNote("hello");
redisTemplate.opsForValue().set("role_1", role);
Role role1 = (Role) redisTemplate.opsForValue().get("role_1");
System.out.println(role1.getRoleName());
//以上代碼存在問題,不能保證set操作和get操作來自於同一個連接
/*
* 爲了使得所有的操作來自於同一個連接,可以使用SessionCallback或者是RedisCallback
* 這兩個接口,更多的時候還是會用到SessionCallback
*/
}
@Test
public void fun() {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate =applicationContext.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1L);
role.setRoleName("swift");
role.setNote("hello");
//前面的操作都是一樣,只是把set和get方法放到了接口中
SessionCallback callback = new SessionCallback<Role>() {
@Override
public Role execute(RedisOperations operations) throws DataAccessException {
operations.boundValueOps("role_1").set(role);
return (Role) operations.boundValueOps("role_1").get();
}
};
//使用了SessionCallback,這樣可以保證在同一個連接池的同一個Redis連接進行操作。
Role role1 = (Role) redisTemplate.execute(callback);
System.out.println(role1.getRoleName());
}
}
4.問題
(1)如果發現連接不上,檢測是否開啓redis服務。
(2)如果是spring內部對象創建異常,檢測配置文件的正確性。
5.源碼文件
鏈接:https://pan.baidu.com/s/1uoHTKyRaqPuwUzsjoN970Q 密碼:l2mk