pom构建:
<dependency>
<groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
spring配置文件(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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:redis.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="127.0.0.1" p:port="6379" p:password="admin" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="xxxxx" class="com.x.dao.impl.xxxxxx" />
</beans>
AbstractBaseRedisDao:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* AbstractBaseRedisDao
* @author http://blog.csdn.net/java2000_wl
* @version <b>1.0</b>
*/
public abstract class AbstractBaseRedisDao<K, V> {
@Autowired
protected RedisTemplate<K, V> redisTemplate;
/**
* 设置redisTemplate
* @param redisTemplate the redisTemplate to set
*/
public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 获取 RedisSerializer
* <br>------------------------------<br>
*/
protected RedisSerializer<String> getRedisSerializer() {
return redisTemplate.getStringSerializer();
}
}
daoImpl:
public class xxxDao extends AbstractBaseRedisDao<String, XXX> implements IxxxDao {
/**
* 新增
*<br>------------------------------<br>
* @param xxx
* @return
*/
public boolean add(final XXX xxx) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(xxx.getXXX());
byte[] name = serializer.serialize(xxx.getXXX());
return connection.setNX(key, name);
}
});
return result;
}
/**
* 批量新增 使用pipeline方式
*<br>------------------------------<br>
*@param list
*@return
*/
public boolean add(final List<XXX> list) {
Assert.notEmpty(list);
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
for (XXX XXXX: list) {
byte[] key = serializer.serialize(xxx.getXXX());
byte[] name = serializer.serialize(xxx.getXXX());
connection.setNX(key, name);
}
return true;
}
}, false, true);
return result;
}
/**
* 删除
* <br>------------------------------<br>
* @param key
*/
public void delete(String key) {
List<String> list = new ArrayList<String>();
list.add(key);
delete(list);
}
/**
* 删除多个
* <br>------------------------------<br>
* @param keys
*/
public void delete(List<String> keys) {
redisTemplate.delete(keys);
}
/**
* 修改
* <br>------------------------------<br>
* @param xxx
* @return
*/
public boolean update(final XXX xxx) {
String key = xxx.getXXX();
if (get(key) == null) {
throw new NullPointerException("数据行不存在, key = " + key);
}
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(xxx.getXXX());
byte[] name = serializer.serialize(xxx.getXXX());
connection.set(key, name);
return true;
}
});
return result;
}
/**
* 通过key获取
* <br>------------------------------<br>
* @param keyId
* @return
*/
public User get(final String keyId) {
User result = redisTemplate.execute(new RedisCallback<XXX>() {
public User doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(keyId);
byte[] value = connection.get(key);
if (value == null) {
return null;
}
String name = serializer.deserialize(value);
return new User(keyId, name, null);
}
});
return result;
}
}