您的位置:首页 > 数据库 > Redis

Redis系列-JAVA与redis整合-spring-redis

2013-11-03 12:00 711 查看
http://projects.spring.io/spring-data-redis/

直接上代码 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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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" />
<context:component-scan base-package="org.zlex.redis.dao" />
<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" />
<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>
</beans>


redis.properties

redis.pool.maxActive=1024
redis.pool.maxIdle=200
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true
#IP
redis.ip=192.168.1.234
#Port
redis.port=6379


实体类

/**
* Aug 29, 2012
*/
package org.zlex.redis.domain;

import java.io.Serializable;

/**
*
* @author
* @version 1.0
* @since 1.0
*/
public class User implements Serializable {

/**
*
*/
private static final long serialVersionUID = -1267719235225203410L;

private String uid;

private String address;

/**
* @return the uid
*/
public String getUid() {
return uid;
}

/**
* @param uid
*            the uid to set
*/
public void setUid(String uid) {
this.uid = uid;
}

/**
* @return the address
*/
public String getAddress() {
return address;
}

/**
* @param address
*            the address to set
*/
public void setAddress(String address) {
this.address = address;
}

}


Dao接口

/**
* Aug 29, 2012
*/
package org.zlex.redis.dao;

import org.zlex.redis.domain.User;

/**
*
* @author
* @version 1.0
* @since 1.0
*/
public interface UserDao {
/**
* @param uid
* @param address
*/
void save(User user);

/**
* @param uid
* @return
*/
User read(String uid);

/**
* @param uid
*/
void delete(String uid);
}


实现类

/**
* Aug 29, 2012
*/
package org.zlex.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 org.zlex.redis.dao.UserDao;
import org.zlex.redis.domain.User;

/**
*
* @author
* @version 1.0
* @since 1.0
*/
@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;
}
});
}

/*
* (non-Javadoc)
*
* @see org.zlex.redis.dao.UserDao#read(java.lang.String)
*/
@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;
}
});
}

/*
* (non-Javadoc)
*
* @see org.zlex.redis.dao.UserDao#delete(java.lang.String)
*/
@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;
}
});
}
}


TestCase

package org.zlex.redis.dao.impl;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.zlex.redis.dao.UserDao;
import org.zlex.redis.domain.User;

public class UserDaoTest {
private ApplicationContext app;
private UserDao userDao;

@Before
public void before() throws Exception {
app = new ClassPathXmlApplicationContext("applicationContext.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);

assertEquals(address1, user.getAddress());

// --------------Update ------------
String address2 = "北京";
user.setAddress(address2);
userDao.save(user);

user = userDao.read(uid);

assertEquals(address2, user.getAddress());

// --------------Delete ------------
userDao.delete(uid);
user = userDao.read(uid);
assertNull(user);
}
}


项目打包下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: