Spring整合Redis
1.引入redis jar包
2.配置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" />
<!-- redis 对象池 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${maxIdle}" />
<property name="maxActive" value="${maxActive}" />
<property name="maxWait" value="${maxWait}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
</bean>
<!-- 连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${host}" p:port="${port}" p:password="${pass}"
p:pool-config-ref="poolConfig" />
<!-- spring 对redis封装,相当于redis客户端 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="studentDao" class="impl.StudentDaoImpl">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
</beans>
3.redis设置
# Redis settings
#服务器ip,下面是本地
host=127.0.0.1
#端口,默认6379
port=6379
#redis连接密码,默认没有设
pass=
#最大能够保持idel状态的对象数
maxIdle=300
#最大分配的对象数
maxActive=600
#当池内没有返回对象时,最大等待时间
maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
testOnBorrow=true
4.BaseRedisDao
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
public abstract class BaseRedisDao<K, V> {
protected RedisTemplate<K, V> redisTemplate;
public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
this.redisTemplate = redisTemplate;
}
protected RedisSerializer<String> getRedisSerializer() {
return redisTemplate.getStringSerializer();
}
}
5.StudentDao
import java.util.List;
import entity.Student;
public interface StudentDao {
/**
* 新增
*
* @param user
* @return
*/
boolean add(Student student);
/**
* 批量新增 使用pipeline方式
*
* @param list
* @return
*/
boolean add(List<Student> list);
/**
* 删除
*
* @param key
*/
void delete(String key);
/**
* 删除多个
*
* @param keys
*/
void delete(List<String> keys);
/**
* 修改
*
* @param user
* @return
*/
boolean update(Student student);
/**
* 通过key获取
*
* @param keyId
* @return
*/
Student get(String keyId);
}
6.StudentDao实现,继承了BaseRedisDao抽象类,其中redisTemplate在spring配置文件中已经注入了值。实现方法中使用了匿名内部类,和回调函数,将要缓存的数据进行序列化处理。Java8之前要求匿名内部类中操作外部类成员变量或局部变量的时候要定义为final,Java8后就更加灵活,不要求是final。后面还有删除更新,查询的操作都是一样的,可以自己试着写写。
public class StudentDaoImpl extends BaseRedisDao<String, Student> implements StudentDao {
@Override
public boolean add(final Student student) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(student.getId());
byte[] name = serializer.serialize(student.getName());
return connection.setNX(key, name);
}
});
return result;
}
@Override
public boolean add(List<Student> list) {
// TODO Auto-generated method stub
return false;
}
@Override
public void delete(String key) {
// TODO Auto-generated method stub
}
@Override
public void delete(List<String> keys) {
// TODO Auto-generated method stub
}
@Override
public boolean update(Student student) {
// TODO Auto-generated method stub
return false;
}
@Override
public Student get(String keyId) {
// TODO Auto-generated method stub
return null;
}
}
7.测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.StudentDao;
import entity.Student;
public class Test {
public static void main(String[] args) {
ApplicationContext atc = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
StudentDao dao = (StudentDao)atc.getBean("studentDao");
Student student = new Student();
student.setId("1");
student.setName("zhangsan");
dao.add(student);
}
}
8.在redis客户端获得key对应的value值