一、前言
redis是一种特殊类型的数据库,称之为key-value存储。和HashMap有很大相似度。在大部分情况下,我们开发java项目通常会结合Spring进行整合操作。我们可以利用Spring Data面向模板的数据访问来使用Redis数据库。
Spring Data Redis提供四个Redis客户端实现的连接工厂
1、JedisConnnectionFactory
2、JredisConnnectionFactory
3、LettuceConnectionFactory
4、SrpConnectionFactory
具体使用哪一个,取决于你自己。从Spring Data Redis的角度来看,这些工厂在适用性上基本相同,通过连接工厂可以创建模板对Redis进行操作。Spring Data Redis提供了两个模板,分别为:
1、RedisTemplate
2、StringRedisTemplate
二、引入相关的jar包
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
三、配置文件
#访问地址
redis.host=127.0.0.1
#访问端口
redis.port=6379
#注意,如果没有password,此处不设置值,但这一项要保留
redis.password=
#最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。
redis.maxIdle=300
#连接池的最大数据库连接数。设为0表示无限制
redis.maxActive=600
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
redis.maxWait=1000
#在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;
redis.testOnBorrow=true
四、redis-context.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- scanner redis properties -->
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<!--(1)如果你有多个数据源需要通过<context:property-placeholder管理,且不愿意放在一个配置文件里,那么一定要加上ignore-unresolvable=“true"-->
<!--(2)注意新版的(具体从哪个版本开始不清楚,有兴趣可以查一下)JedisPoolConfig的property name,不是maxActive而是maxTotal,而且没有maxWait属性,建议看一下Jedis源码。-->
<!-- redis连接池 -->
<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxActive}"></property>
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="maxWaitMillis" value="${redis.maxWait}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
</bean>
<!-- redis连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.password}"></property>
<property name="poolConfig" ref="jedisConfig"></property>
</bean>
<!-- redis操作模板,这里采用尽量面向对象的模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
<!--开启事务-->
<property name="enableTransactionSupport" value="true"/>
</bean>
</beans>
需要注意的是,如果你使用的不是StringRedisTemplate,而是RedisTemplate那么你不得不适用序列化器用于实现String与byte数组之间的相互转换。
Spring Data Redis包含多种序列化器:
GenericToStringSerializer:使用Spring转换服务进行序列化
JacksonJsonRedisSerializer:使用Jackson1,将对象序列号为JSON
JacksonJson2RedisSerializer:使用Jackson2,将对象序列号为JSON
JdkSerializationRedisSerializer:使用Java序列化
OxmSerializer:使用Spring O/X映射的编排器和解排器(marshaler和unmarshaler)实现序列化,用于XML序列化。
StringRedisSerializer:序列化String类型的key 和 value.
五、测试
1、定义实体类User
public class User {
private String username;
private String password;
...
get/set
}
2、定义User操作接口
public interface IUserDao {
boolean add(User user);
boolean delete(String key);
boolean update(User user);
}
3、定义接口实现类
@Component
public class UserDaoImp implements IUserDao {
@Autowired
StringRedisTemplate redisTemplate;
@Override
public boolean add(User user) {
//使用最基本的方式
/*redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.set(redisTemplate.getStringSerializer().serialize(user.getUsername()),
redisTemplate.getStringSerializer().serialize(user.getPassword()));
return null;
}
});*/
//利用了StringRdisTemplate的特性
//redisTemplate.opsForValue().set(user.getUsername(), user.getPassword());
//利用了StringRdisTemplate的特性 通过绑定的方式
BoundValueOperations<String, String> bound = redisTemplate.boundValueOps(user.getUsername());
bound.set(user.getPassword());
//bound.append(user.getPassword());//追加,和StringBuilder的append一样功能
return true;
}
@Override
public boolean delete(String key) {
try {
redisTemplate.delete(key);
System.out.println(key+" 删除成功。");
} catch (Exception e) {
return false;
}
return true;
}
@Override
public boolean update(User user) {
ValueOperations<String, String> ops = redisTemplate.opsForValue();
if(ops.get(user.getUsername()) != null){
ops.set(user.getUsername(), user.getUsername());
return true;
}
System.out.println("没有此用户!" + user.getUsername());
return false;
}
}
4、编写测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:application-context.xml")
public class RedisTest {
@Autowired
UserDaoImp userDao;
@Test
public void redis(){
//添加
userDao.add(new User("hgs","123456"));
//更新
//userDao.update(new User("hgs","qwe"));
//查询
System.out.println(userDao.get("hgs"));
//删除
//userDao.delete("hgs");
}
}