package com.x.bean; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 1L; private String id; private String name; public User() { } public User(String id, String name) { super(); this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.x.dao; import java.util.List; import com.x.bean.User; public interface IUserDao { public boolean insert(User user); public boolean insert(List<User> userList); public boolean delete(String key); public boolean delete(List<String> keys); public boolean update(User user); public User find(String keyId); }
package com.x.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; public class RedisBaseDao<K, V> { @Autowired protected RedisTemplate<K, V> redisTemplate; public RedisTemplate<K, V> getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) { this.redisTemplate = redisTemplate; } protected RedisSerializer<String> getRedisSerializer() { return redisTemplate.getStringSerializer(); } }
package com.x.dao; import java.util.List; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.stereotype.Repository; import com.x.bean.User; @Repository public class UserDaoImpl extends RedisBaseDao<String, User> implements IUserDao { @Override public boolean insert(User user) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] key = serializer.serialize(user.getId()); byte[] value = serializer.serialize(user.getName()); return connection.setNX(key, value); } }); return result; } @Override public boolean insert(List<User> userList) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); for (User user : userList) { byte[] key = serializer.serialize(user.getId()); byte[] value = serializer.serialize(user.getName()); connection.setNX(key, value); } return true; } }, false, true); return result; } @Override public boolean delete(String key) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] k = serializer.serialize(key); return connection.del(k) > 0 ? true : false; } }); return result; } @Override public boolean delete(List<String> keys) { boolean result = false; for (String key : keys) { result = delete(key); } return result; } @Override public boolean update(User user) { String key = user.getId(); if (null == find(key)) { throw new NullPointerException("不存在此条数据 key = " + key); } boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] key = serializer.serialize(user.getId()); byte[] value = serializer.serialize(user.getName()); connection.set(key, value); return true; } }); return result; } @Override public User find(String keyId) { User user = redisTemplate.execute(new RedisCallback<User>() { @Override public User doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] key = serializer.serialize(keyId); byte[] value = connection.get(key); if (null == value) { return null; } String name = serializer.deserialize(value); return new User(keyId, name); } }); return user; } }
package com.x.test; import java.util.ArrayList; import java.util.List; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.x.bean.User; import com.x.dao.IUserDao; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "file:src/main/resources/redis.xml" }) public class RedisTest { @Autowired private IUserDao userDao; @Test public void testInsert() { User user = new User(); user.setId("0001"); user.setName("Jack"); boolean result = userDao.insert(user); Assert.assertTrue(result); } @Test public void testInsertList() { List<User> userList = new ArrayList<User>(); for (int i = 0; i < 5; i++) { User user = new User(); user.setId("user" + i); user.setName("userName" + i); userList.add(user); } boolean result = userDao.insert(userList); Assert.assertTrue(result); } @Test public void testDelete() { String key = "user2"; boolean result = userDao.delete(key); Assert.assertTrue(result); } @Test public void testDeleteList() { List<String> keyList = new ArrayList<String>(); keyList.add("user3"); keyList.add("user4"); boolean result = userDao.delete(keyList); Assert.assertTrue(result); } @Test public void testUpdate() { User user = new User("0001", "Tom"); boolean result = userDao.update(user); Assert.assertTrue(result); } @Test public void testFind() { String key = "0001"; User user = userDao.find(key); Assert.assertNotNull(user); Assert.assertEquals(user.getName(), "Tom"); } }
redis.host=127.0.0.1 redis.port=6379 redis.pass=123 redis.maxIdle=8 redis.maxTotal=8 redis.testOnBorrow=true
<?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" 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:component-scan base-package="com.x.dao" /> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:redis.properties</value> </list> </property> </bean> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}"></property> <property name="port" value="${redis.port}"></property> <property name="password" value="${redis.pass}"></property> <property name="poolConfig" ref="poolConfig"></property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> </beans>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>redis</groupId> <artifactId>reids</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.3.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> </dependencies> </project>