jedis与spring整合及简单的使用RedisTemplate操作
整理一下redis与spring的整合。以及使用redisTemplate。首先是要导入spring所需要的jar。当然还有 jedis-2.1.0.jar,commons-pool-1.5.4.jar,spring-data-redis-1.0.0.RELEASE.jar (这是我使用的版本,应该不新)
1. 导入完这些jar,开始整理配置文件:
首先就是web.xml。这个还是老样子:贴一下吧
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
还有就是spring配置文件,大家也就一看就明白了。我还是叫做applicationContext.xml,
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-lazy-init="false"> <!-- 支持注解 --> <context:annotation-config /> <!-- 组件扫描 --> <context:component-scan base-package="com.demo" /> <context:property-placeholder location="classpath:redis.properties" /> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxActive" value="${redis.pool.maxActive}" /> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="maxWait" value="${redis.pool.maxWait}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.ip}" /> <property name="port" value="${redis.port}" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <bean id="template" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> </bean> <bean id="userDao" class="com.demo.spring.UserDaoImpl"> <property name="template" ref="template" /> </bean> </beans>
还有一个就是redis.properties了:
#最大分配的对象数 redis.pool.maxActive=1024 #最大能够保持idel状态的对象数 redis.pool.maxIdle=200 #当池内没有返回对象时,最大等待时间 redis.pool.maxWait=1000 #当调用borrow Object方法时,是否进行有效性检查 redis.pool.testOnBorrow=true #IP redis.ip=127.0.0.1 #Port redis.port=6379
好了,配置文件就是这些,也没什么特别之处。
2.编写一个User类:
public class User implements Serializable { /** * */ private static final long serialVersionUID = -1530813282496676263L; private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
3.编写一个userDao和UserDaoImpl
public interface UserDao { void save(User user); User read(User user); }
public class UserDaoImpl implements UserDao { private RedisTemplate<Serializable, Serializable> template; public RedisTemplate<Serializable, Serializable> getTemplate() { return template; } public void setTemplate(RedisTemplate<Serializable, Serializable> template) { this.template = template; } public User read(final User user) { // User user2 = (User) template.execute(new RedisCallback<Object>() { // @Override // public User doInRedis(RedisConnection connection)throws DataAccessException { // byte[] key = template.getStringSerializer().serialize("id"+user.getId()); // if(connection.exists(key)){ // byte[] value = connection.get(key); // String name = template.getStringSerializer().deserialize(value); // User user1 = new User(); // user1.setName(name); // return user1; // } // return null; // } // }); ValueOperations<Serializable, Serializable> opsForValue = template.opsForValue(); User user2 = (User) opsForValue.get(user.getId()); return user2; } public void save(final User user) { // template.execute(new RedisCallback<Object>() { // public Object doInRedis(RedisConnection connection) throws DataAccessException { // connection.set(template.getStringSerializer().serialize("id"+user.getId()), template.getStringSerializer().serialize(user.getName())); // return null; // } // }); ValueOperations<Serializable, Serializable> opsForValue = template.opsForValue(); opsForValue.set(user.getId(), user); } }
这里注释部分是另一种方法,也是可以的,自定义对象需要序列化 template.getStringSerializer().serialize("xxx");
注释中connection方法很多例如:
connection.mGet(keys);//byte[]... keys
connection.mSet(tuple);//Map<byte[], byte[]> tuple
connection.lSet(key, index, value);//byte[] key, long index, byte[] value
connection.lRange(key, begin, end);//byte[] key, long begin, long end)等等
4.最后是测试类
private ApplicationContext app; private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Before public void before() throws Exception { app = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到userDao 对象 userDao = (UserDao) app.getBean("userDao"); } @Test public void test1() { String name = "fu"; User user = new User(); user.setName(name); user.setId(1); userDao.save(user); System.out.println("============添加完成"); User u = userDao.read(user); System.out.println("============获取:" + u.getName()); }
哦了,基本的整合也就完成了。。