一、Spring提供的RedisTemplate
操作Redis,但是需要导入两个jar包:
(1)、jedis.jar
(2)、spring-data-redis.jar(注意版本兼容问题)
二、配置文件
在大部分情况下要使用到连接池,所以先要配置一个JedisPoolConfig
对象
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大空闲数 -->
<property name="maxIdle" value="50" />
<!--最大连接数 -->
<property name="maxTotal" value="100" />
<!--最大等待时间 -->
<property name="maxWaitMillis" value="20000" />
</bean>
在使用Spring提供的RedisTemplate
之前需要配置Spring提供的连接工厂,在Spring Data Redis方案中提供了4种工厂模型:
- JredisConnectionFactory
- JredisConnectionFactory
- LettuceConnectionFactory
- SrpConnectionFactory
它们都是RedisConnectionFactory接口的实现类,JedisConnectionFactory应用较广泛。下面配置一个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="paasword"/> -->
<property name="poolConfig" ref="poolConfig" />
</bean>
- hostName - 表示服务器,默认是
localhost
,如果是本机可以不配置它 - port - 表示端口,默认是
6379
,如果使用默认的Redis端口,也可以不配置它 - password - 表示密码,在需要密码连接Redis的场合需要配置它
- poolConfig - 连接池配置对象
普通的链接使用没有办法将java对象直接存入Redis,在Spring模板中提供了封装的方案,在它内部提供了RedisSerializer接口和一些实现类,使用它们可以将java对象通过序列化存储到Redis中,也可以把Redis存储的内容转化为Java对象。
StringRedisSerializer
使用字符串进行序列化JdkSerializationRedisSerializer
使用JDK的序列化器进行转化
Spring提供的RedisTemplate
有两个属性
keySerializer
- 键序列器valueSerializer
- 值序列器
了解了以上知识,就可配置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>
例子如下:
Pojo类
package com.dongtian.spring_redis.pojo;
import java.io.Serializable;
public class User implements Serializable{
//要序列化对象,需要实现Serializable接口
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String phone;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", phone=" + phone + ", age=" + age + "]";
}
}
测试类:
package com.dongtian.spring_redis;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import com.dongtian.spring_redis.pojo.User;
public class SpringRedisTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);
User user = new User();
user.setUsername("张三");
user.setPassword("123456");
user.setPhone("2525255");
user.setAge(20);
redisTemplate.opsForValue().set("user_1", user);
User user1 = (User) redisTemplate.opsForValue().get("user_1");
System.out.println(user1);
}
}
配置文件:applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大空闲数 -->
<property name="maxIdle" value="50" />
<!--最大连接数 -->
<property name="maxTotal" value="100" />
<!--最大等待时间 -->
<property name="maxWaitMillis" value="20000" />
</bean>
<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="aa123456"/>
<property name="poolConfig" ref="poolConfig" />
</bean>
<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>
pom.xml
<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>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<!-- 1.Spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- 2.Spring dao依赖 -->
<!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- 3.Spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- 4.Spring test依赖:方便做单元测试和集成测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
最后,总结流程:
1、导入jar包
2、配置redis连接池、配置连接RedisTemplate的工厂(4种,最常用的是JedisConnectionFactory)、配置RedisTemplate
3、建立POJO类,必须实现序列化Serializable接口
4、写测试类