整理一下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()); }
哦了,基本的整合也就完成了。。