spring redis整合(一)
2013-06-28 13:32
330 查看
spring-data-redis下载地址:
http://www.springsource.org/download/community?project=Spring%2520Data%2520Redis&version=1.0.1.RELEASE
//spring-redis.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:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.abin.lee.spring.redis"></context:component-scan>
<context:property-placeholder location="classpath:com/abin/lee/spring/redis/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
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory" />
</beans>
//User.java
package com.abin.lee.spring.redis.pojo;
import java.io.Serializable;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2668307865623183776L;
private String uid;
private String address;
public User() {
super();
}
public User(String uid, String address) {
super();
this.uid = uid;
this.address = address;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [uid=" + uid + ", address=" + address + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((uid == null) ? 0 : uid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (uid == null) {
if (other.uid != null)
return false;
} else if (!uid.equals(other.uid))
return false;
return true;
}
}
//UserDao.java
package com.abin.lee.spring.redis.dao;
import com.abin.lee.spring.redis.pojo.User;
public interface UserDao {
/**
* @param uid
* @param address
*/
void save(User user);
/**
* @param uid
* @return
*/
User read(String uid);
/**
* @param uid
*/
void delete(String uid);
}
//UserDaoImpl.java
package com.abin.lee.spring.redis.dao.impl;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import com.abin.lee.spring.redis.dao.UserDao;
import com.abin.lee.spring.redis.pojo.User;
@Repository("userDao")
public class UserDaoImpl implements UserDao{
@Autowired
private RedisTemplate<Serializable, Serializable> redisTemplate;
@Override
public void save(final User user) {
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
connection.set(
redisTemplate.getStringSerializer().serialize(
"user.uid." + user.getUid()),
redisTemplate.getStringSerializer().serialize(
user.getAddress()));
return null;
}
});
}
@Override
public User read(final String uid) {
return redisTemplate.execute(new RedisCallback<User>() {
@Override
public User doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] key = redisTemplate.getStringSerializer().serialize(
"user.uid." + uid);
if (connection.exists(key)) {
byte[] value = connection.get(key);
String address = redisTemplate.getStringSerializer()
.deserialize(value);
User user = new User();
user.setAddress(address);
user.setUid(uid);
return user;
}
return null;
}
});
}
@Override
public void delete(final String uid) {
redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) {
connection.del(redisTemplate.getStringSerializer().serialize(
"user.uid." + uid));
return null;
}
});
}
}
//UserDaoTest.java
package com.abin.lee.spring.redis.dao.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.abin.lee.spring.redis.dao.UserDao;
import com.abin.lee.spring.redis.pojo.User;
public class UserDaoTest {
private ApplicationContext app;
private UserDao userDao;
@Before
public void before() throws Exception {
app = new ClassPathXmlApplicationContext("com/abin/lee/spring/redis/spring-redis.xml");
userDao = (UserDao) app.getBean("userDao");
}
@Test
public void crud() {
// -------------- Create ---------------
String uid = "u123456";
String address1 = "上海";
User user = new User();
user.setAddress(address1);
user.setUid(uid);
userDao.save(user);
// ---------------Read ---------------
user = userDao.read(uid);
System.out.println("address1="+user.getAddress());
assertEquals(address1, user.getAddress());
// --------------Update ------------
String address2 = "北京";
user.setAddress(address2);
userDao.save(user);
user = userDao.read(uid);
System.out.println("address2Save="+user.getAddress());
assertEquals(address2, user.getAddress());
// --------------Delete ------------
userDao.delete(uid);
user = userDao.read(uid);
System.out.println("addressdel="+user.getAddress());
assertNull(user);
}
}
http://www.springsource.org/download/community?project=Spring%2520Data%2520Redis&version=1.0.1.RELEASE
//spring-redis.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:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.abin.lee.spring.redis"></context:component-scan>
<context:property-placeholder location="classpath:com/abin/lee/spring/redis/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
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory" />
</beans>
//User.java
package com.abin.lee.spring.redis.pojo;
import java.io.Serializable;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2668307865623183776L;
private String uid;
private String address;
public User() {
super();
}
public User(String uid, String address) {
super();
this.uid = uid;
this.address = address;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [uid=" + uid + ", address=" + address + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((uid == null) ? 0 : uid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (uid == null) {
if (other.uid != null)
return false;
} else if (!uid.equals(other.uid))
return false;
return true;
}
}
//UserDao.java
package com.abin.lee.spring.redis.dao;
import com.abin.lee.spring.redis.pojo.User;
public interface UserDao {
/**
* @param uid
* @param address
*/
void save(User user);
/**
* @param uid
* @return
*/
User read(String uid);
/**
* @param uid
*/
void delete(String uid);
}
//UserDaoImpl.java
package com.abin.lee.spring.redis.dao.impl;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import com.abin.lee.spring.redis.dao.UserDao;
import com.abin.lee.spring.redis.pojo.User;
@Repository("userDao")
public class UserDaoImpl implements UserDao{
@Autowired
private RedisTemplate<Serializable, Serializable> redisTemplate;
@Override
public void save(final User user) {
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
connection.set(
redisTemplate.getStringSerializer().serialize(
"user.uid." + user.getUid()),
redisTemplate.getStringSerializer().serialize(
user.getAddress()));
return null;
}
});
}
@Override
public User read(final String uid) {
return redisTemplate.execute(new RedisCallback<User>() {
@Override
public User doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] key = redisTemplate.getStringSerializer().serialize(
"user.uid." + uid);
if (connection.exists(key)) {
byte[] value = connection.get(key);
String address = redisTemplate.getStringSerializer()
.deserialize(value);
User user = new User();
user.setAddress(address);
user.setUid(uid);
return user;
}
return null;
}
});
}
@Override
public void delete(final String uid) {
redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) {
connection.del(redisTemplate.getStringSerializer().serialize(
"user.uid." + uid));
return null;
}
});
}
}
//UserDaoTest.java
package com.abin.lee.spring.redis.dao.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.abin.lee.spring.redis.dao.UserDao;
import com.abin.lee.spring.redis.pojo.User;
public class UserDaoTest {
private ApplicationContext app;
private UserDao userDao;
@Before
public void before() throws Exception {
app = new ClassPathXmlApplicationContext("com/abin/lee/spring/redis/spring-redis.xml");
userDao = (UserDao) app.getBean("userDao");
}
@Test
public void crud() {
// -------------- Create ---------------
String uid = "u123456";
String address1 = "上海";
User user = new User();
user.setAddress(address1);
user.setUid(uid);
userDao.save(user);
// ---------------Read ---------------
user = userDao.read(uid);
System.out.println("address1="+user.getAddress());
assertEquals(address1, user.getAddress());
// --------------Update ------------
String address2 = "北京";
user.setAddress(address2);
userDao.save(user);
user = userDao.read(uid);
System.out.println("address2Save="+user.getAddress());
assertEquals(address2, user.getAddress());
// --------------Delete ------------
userDao.delete(uid);
user = userDao.read(uid);
System.out.println("addressdel="+user.getAddress());
assertNull(user);
}
}
相关文章推荐
- jedis与spring整合及简单的使用RedisTemplate操作
- spring简单整合Redis配置
- spring 整合redis
- redis入门——Spring整合篇
- SpringBoot 整合redis
- 5. SpringMVC+redis整合-下篇
- redis单机之spring整合操作
- spring5整合spring-data-redis2
- spring_redis整合
- redis mybatis spring整合
- springboot整合redis进行数据操作(推荐)
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- springboot+shiro+redis(单机redis版)整合教程
- Spring整合Redis-Cluster集群实现04
- springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)
- Spring 整合 Redis 分类:
- 基于SpringBoot整合redis及shiro
- spring-boot 整合redis作为数据缓存
- spring整合redis
- SpringBoot整合Redis(附带序列化方式对比)