spring与redis整合
1、jar包
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、redis.properties
#redis.properties
#2017/04/15
redis.host=192.168.255.133
redis.port=6379
redis.pass=123456
redis.maxIdle=300
redis.maxActive.value=600
redis.maxWait=1000
redis.testOnBorrow=true
3、spring-redis.xml
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexmlns: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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<!-- —注意此处注入的是JedisPoolConfig,说明SDR还依赖与Jedis -->
<bean id="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle"value="${redis.maxIdle}" />
<propertyname="maxTotal" value="${redis.maxActive.value}" />
<propertyname="maxWaitMillis" value="${redis.maxWait}" />
<propertyname="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="connectionsFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}"p:port="${redis.port}" p:password="${redis.pass}"
p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate"class="org.springframework.data.redis.core.StringRedisTemplate">
<propertyname="connectionFactory" ref="connectionsFactory" />
<!-- 如果不配置Serializer,那么存储的时候智能使用String -->
<propertyname="keySerializer">
<beanclass="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<propertyname="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
<bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
4、applicationContent.xml
<importresource="spring-redis.xml"/>
5、AbstractBaseRedisService抽象类
importjavax.annotation.Resource;
importorg.springframework.data.redis.core.RedisTemplate;
importorg.springframework.data.redis.serializer.RedisSerializer;
publicabstractclass AbstractBaseRedisService<K,V>{
@Resource(name="redisTemplate")
protectedRedisTemplate<K, V> redisTemplate;
/**
* 设置redisTemplate
* @paramredisTemplate the redisTemplate to set
*/
publicvoidsetRedisTemplate(RedisTemplate<K, V> redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 获取RedisSerializer
* <br>------------------------------<br>
*/
protectedRedisSerializer<String> getRedisSerializer() {
returnredisTemplate.getStringSerializer();
}
}
6、接口以及实现类
importjava.util.List;
importcom.workorder.entity.Person;
publicinterfacePersonService {
/**
* 新增
* <br>------------------------------<br>
* @param Person
* @return
*/
booleanadd(Person Person);
/**
* 批量新增使用pipeline方式
* <br>------------------------------<br>
* @param list
* @return
*/
boolean add(List<Person> list);
/**
* 删除
* <br>------------------------------<br>
* @param key
*/
void delete(String key);
/**
* 删除多个
* <br>------------------------------<br>
* @param keys
*/
void delete(List<String> keys);
/**
* 修改
* <br>------------------------------<br>
* @param Person
* @return
*/
boolean update(Person Person);
/**
* 通过key获取
* <br>------------------------------<br>
* @param keyId
* @return
*/
Person get(String keyId);
}
importjava.util.ArrayList;
importjava.util.List;
importorg.springframework.dao.DataAccessException;
importorg.springframework.data.redis.connection.RedisConnection;
importorg.springframework.data.redis.core.RedisCallback;
importorg.springframework.data.redis.serializer.RedisSerializer;
importorg.springframework.stereotype.Service;
importorg.springframework.util.Assert;
importcom.workorder.entity.Person;
importcom.workorder.service.PersonService;
@Service
publicclass PersonServiceImplextends AbstractBaseRedisService<String,Person> implements PersonService {
@Override
publicboolean add( final Person person) {
booleanresult = redisTemplate.execute(newRedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throwsDataAccessException {
RedisSerializer<String> serializer =getRedisSerializer();
byte[] key = serializer.serialize(person.getId());
byte[] name = serializer.serialize(person.getName());
returnconnection.setNX(key, name);
}
});
returnresult;
}
@Override
publicboolean add(finalList<Person> list) {
Assert.notEmpty(list);
booleanresult = redisTemplate.execute(newRedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throwsDataAccessException {
RedisSerializer<String> serializer =getRedisSerializer();
for (Person person : list) {
byte[] key = serializer.serialize(person.getId());
byte[] name = serializer.serialize(person.getName());
connection.setNX(key, name);
}
returntrue;
}
}, false, true);
returnresult;
}
@Override
publicvoiddelete(String key) {
List<String> list = newArrayList<String>();
list.add(key);
delete(list);
}
@Override
publicvoiddelete(List<String> keys) {
redisTemplate.delete(keys);
}
@Override
publicboolean update(final Person person) {
String key = person.getId();
if (get(key) == null) {
thrownewNullPointerException("数据行不存在, key =" + key);
}
booleanresult = redisTemplate.execute(newRedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throwsDataAccessException {
RedisSerializer<String> serializer =getRedisSerializer();
byte[] key = serializer.serialize(person.getId());
byte[] name = serializer.serialize(person.getName());
connection.set(key, name);
returntrue;
}
});
returnresult;
}
@Override
public Person get(final String keyId) {
Person result = redisTemplate.execute(newRedisCallback<Person>() {
public PersondoInRedis(RedisConnection connection)
throwsDataAccessException {
RedisSerializer<String> serializer =getRedisSerializer();
byte[] key = serializer.serialize(keyId);
byte[] value = connection.get(key);
if (value == null) {
returnnull;
}
String name = serializer.deserialize(value);
returnnew Person(keyId, name, null);
}
});
returnresult;
}
}