tyxiayu 发表于 2018-11-5 09:53:44

Spring4 整合 Redis-11546194

  pom构建:
   view plain copy print?

[*]  4.0.0
[*]  com.x.redis
[*]  springredis
[*]  0.0.1-SNAPSHOT
[*]
[*]  
[*]  
[*]  org.springframework.data
[*]  spring-data-redis
[*]  1.0.2.RELEASE
[*]  
[*]  
[*]  org.springframework
[*]  spring-test
[*]  3.1.2.RELEASE
[*]  test
[*]  
[*]
[*]  
[*]  redis.clients
[*]  jedis
[*]  2.1.0
[*]  
[*]
[*]  
[*]  junit
[*]  junit
[*]  4.8.2
[*]  test
[*]  
[*]  
  spring配置文件(applicationContext.xml):
   view plain copy print?

[*]  
[*]  
[*]
[*]  
[*]
[*]  
[*]  
[*]  
[*]  
[*]  
[*]  
[*]
[*]  
[*]
[*]  
[*]  
[*]  
[*]
[*]  
[*]  
  redis.properties
   view plain copy print?

[*]  # Redis settings
[*]  redis.host=localhost
[*]  redis.port=6379
[*]  redis.pass=java2000_wl
[*]
[*]
[*]  redis.maxIdle=300
[*]  redis.maxActive=600
[*]  redis.maxWait=1000
[*]  redis.testOnBorrow=true
  Java代码获取【下载地址】   :
   view plain copy print?

[*]  package com.x.entity;
[*]
[*]  import java.io.Serializable;
[*]
[*]  /**
[*]  * @author http://blog.csdn.net/java2000_wl
[*]  * @version 1.0
[*]  */
[*]  public class User implements Serializable {
[*]
[*]  private static final long serialVersionUID = -6011241820070393952L;
[*]
[*]  private String id;
[*]
[*]  private String name;
[*]
[*]  private String password;
[*]
[*]  /**
[*]  * ------------------------------
[*]  */
[*]  public User() {
[*]
[*]  }
[*]
[*]  /**
[*]  * ------------------------------
[*]  */
[*]  public User(String id, String name, String password) {
[*]  super();
[*]  this.id = id;
[*]  this.name = name;
[*]  this.password = password;
[*]  }
[*]
[*]  /**
[*]  * 获得id
[*]  * @return the id
[*]  */
[*]  public String getId() {
[*]  return id;
[*]  }
[*]
[*]  /**
[*]  * 设置id
[*]  * @param id the id to set
[*]  */
[*]  public void setId(String id) {
[*]  this.id = id;
[*]  }
[*]
[*]  /**
[*]  * 获得name
[*]  * @return the name
[*]  */
[*]  public String getName() {
[*]  return name;
[*]  }
[*]
[*]  /**
[*]  * 设置name
[*]  * @param name the name to set
[*]  */
[*]  public void setName(String name) {
[*]  this.name = name;
[*]  }
[*]
[*]  /**
[*]  * 获得password
[*]  * @return the password
[*]  */
[*]  public String getPassword() {
[*]  return password;
[*]  }
[*]
[*]  /**
[*]  * 设置password
[*]  * @param password the password to set
[*]  */
[*]  public void setPassword(String password) {
[*]  this.password = password;
[*]  }
[*]  }
   view plain copy print?

[*]  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;
[*]
[*]  /**
[*]  * AbstractBaseRedisDao
[*]  * @author http://blog.csdn.net/java2000_wl
[*]  * @version 1.0
[*]  */
[*]  public abstract class AbstractBaseRedisDao {
[*]
[*]  @Autowired
[*]  protected RedisTemplate redisTemplate;
[*]
[*]  /**
[*]  * 设置redisTemplate
[*]  * @param redisTemplate the redisTemplate to set
[*]  */
[*]  public void setRedisTemplate(RedisTemplate redisTemplate) {
[*]  this.redisTemplate = redisTemplate;
[*]  }
[*]
[*]  /**
[*]  * 获取 RedisSerializer
[*]  * ------------------------------
[*]  */
[*]  protected RedisSerializer getRedisSerializer() {
[*]  return redisTemplate.getStringSerializer();
[*]  }
[*]  }
   view plain copy print?

[*]  package com.x.dao;
[*]
[*]  import java.util.List;
[*]
[*]  import com.x.entity.User;
[*]
[*]  /**
[*]  * @author http://blog.csdn.net/java2000_wl
[*]  * @version 1.0
[*]  */
[*]  public interface IUserDao {
[*]
[*]  /**
[*]  * 新增
[*]  * ------------------------------
[*]  * @param user
[*]  * @return
[*]  */
[*]  boolean add(User user);
[*]
[*]  /**
[*]  * 批量新增 使用pipeline方式
[*]  * ------------------------------
[*]  * @param list
[*]  * @return
[*]  */
[*]  boolean add(List list);
[*]
[*]  /**
[*]  * 删除
[*]  * ------------------------------
[*]  * @param key
[*]  */
[*]  void delete(String key);
[*]
[*]  /**
[*]  * 删除多个
[*]  * ------------------------------
[*]  * @param keys
[*]  */
[*]  void delete(List keys);
[*]
[*]  /**
[*]  * 修改
[*]  * ------------------------------
[*]  * @param user
[*]  * @return
[*]  */
[*]  boolean update(User user);
[*]
[*]  /**
[*]  * 通过key获取
[*]  * ------------------------------
[*]  * @param keyId
[*]  * @return
[*]  */
[*]  User get(String keyId);
[*]  }
   view plain copy print?

[*]  package com.x.dao.impl;
[*]
[*]  import java.util.ArrayList;
[*]  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.util.Assert;
[*]
[*]  import com.x.dao.AbstractBaseRedisDao;
[*]  import com.x.dao.IUserDao;
[*]  import com.x.entity.User;
[*]
[*]  /**
[*]  * Dao
[*]  * @author http://blog.csdn.net/java2000_wl
[*]  * @version 1.0
[*]  */
[*]  public class UserDao extends AbstractBaseRedisDao implements IUserDao {
[*]
[*]  /**
[*]  * 新增
[*]  *------------------------------
[*]  * @param user
[*]  * @return
[*]  */
[*]  public boolean add(final User user) {
[*]  boolean result = redisTemplate.execute(new RedisCallback() {
[*]  public Boolean doInRedis(RedisConnection connection)
[*]  throws DataAccessException {
[*]  RedisSerializer serializer = getRedisSerializer();
[*]  byte[] key= serializer.serialize(user.getId());
[*]  byte[] name = serializer.serialize(user.getName());
[*]  return connection.setNX(key, name);
[*]  }
[*]  });
[*]  return result;
[*]  }
[*]
[*]  /**
[*]  * 批量新增 使用pipeline方式
[*]  *------------------------------
[*]  *@param list
[*]  *@return
[*]  */
[*]  public boolean add(final List list) {
[*]  Assert.notEmpty(list);
[*]  boolean result = redisTemplate.execute(new RedisCallback() {
[*]  public Boolean doInRedis(RedisConnection connection)
[*]  throws DataAccessException {
[*]  RedisSerializer serializer = getRedisSerializer();
[*]  for (User user : list) {
[*]  byte[] key= serializer.serialize(user.getId());
[*]  byte[] name = serializer.serialize(user.getName());
[*]  connection.setNX(key, name);
[*]  }
[*]  return true;
[*]  }
[*]  }, false, true);
[*]  return result;
[*]  }
[*]
[*]  /**
[*]  * 删除
[*]  * ------------------------------
[*]  * @param key
[*]  */
[*]  public void delete(String key) {
[*]  List list = new ArrayList();
[*]  list.add(key);
[*]  delete(list);
[*]  }
[*]
[*]  /**
[*]  * 删除多个
[*]  * ------------------------------
[*]  * @param keys
[*]  */
[*]  public void delete(List keys) {
[*]  redisTemplate.delete(keys);
[*]  }
[*]
[*]  /**
[*]  * 修改
[*]  * ------------------------------
[*]  * @param user
[*]  * @return
[*]  */
[*]  public boolean update(final User user) {
[*]  String key = user.getId();
[*]  if (get(key) == null) {
[*]  throw new NullPointerException("数据行不存在, key = " + key);
[*]  }
[*]  boolean result = redisTemplate.execute(new RedisCallback() {
[*]  public Boolean doInRedis(RedisConnection connection)
[*]  throws DataAccessException {
[*]  RedisSerializer serializer = getRedisSerializer();
[*]  byte[] key= serializer.serialize(user.getId());
[*]  byte[] name = serializer.serialize(user.getName());
[*]  connection.set(key, name);
[*]  return true;
[*]  }
[*]  });
[*]  return result;
[*]  }
[*]
[*]  /**
[*]  * 通过key获取
[*]  * ------------------------------
[*]  * @param keyId
[*]  * @return
[*]  */
[*]  public User get(final String keyId) {
[*]  User result = redisTemplate.execute(new RedisCallback() {
[*]  public User doInRedis(RedisConnection connection)
[*]  throws DataAccessException {
[*]  RedisSerializer serializer = getRedisSerializer();
[*]  byte[] key = serializer.serialize(keyId);
[*]  byte[] value = connection.get(key);
[*]  if (value == null) {
[*]  return null;
[*]  }
[*]  String name = serializer.deserialize(value);
[*]  return new User(keyId, name, null);
[*]  }
[*]  });
[*]  return result;
[*]  }
[*]  }
   view plain copy print?

[*]  import java.util.ArrayList;
[*]  import java.util.List;
[*]
[*]  import junit.framework.Assert;
[*]
[*]  import org.junit.Test;
[*]  import org.springframework.beans.factory.annotation.Autowired;
[*]  import org.springframework.test.context.ContextConfiguration;
[*]  import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
[*]
[*]  import com.x.dao.IUserDao;
[*]  import com.x.entity.User;
[*]
[*]  /**
[*]  * 测试
[*]  * @author http://blog.csdn.net/java2000_wl
[*]  * @version 1.0
[*]  */
[*]  @ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
[*]  public class RedisTest extends AbstractJUnit4SpringContextTests {
[*]
[*]  @Autowired
[*]  private IUserDao userDao;
[*]
[*]  /**
[*]  * 新增
[*]  * ------------------------------
[*]  */
[*]  @Test
[*]  public void testAddUser() {
[*]  User user = new User();
[*]  user.setId("user1");
[*]  user.setName("java2000_wl");
[*]  boolean result = userDao.add(user);
[*]  Assert.assertTrue(result);
[*]  }
[*]
[*]  /**
[*]  * 批量新增 普通方式
[*]  * ------------------------------
[*]  */
[*]  @Test
[*]  public void testAddUsers1() {
[*]  List list = new ArrayList();
[*]  for (int i = 10; i < 50000; i++) {
[*]  User user = new User();
[*]  user.setId("user" + i);
[*]  user.setName("java2000_wl" + i);
[*]  list.add(user);
[*]  }
[*]  long begin = System.currentTimeMillis();
[*]  for (User user : list) {
[*]  userDao.add(user);
[*]  }
[*]  System.out.println(System.currentTimeMillis() -begin);
[*]  }
[*]
[*]  /**
[*]  * 批量新增 pipeline方式
[*]  * ------------------------------
[*]  */
[*]  @Test
[*]  public void testAddUsers2() {
[*]  List list = new ArrayList();
[*]  for (int i = 10; i < 1500000; i++) {
[*]  User user = new User();
[*]  user.setId("user" + i);
[*]  user.setName("java2000_wl" + i);
[*]  list.add(user);
[*]  }
[*]  long begin = System.currentTimeMillis();
[*]  boolean result = userDao.add(list);
[*]  System.out.println(System.currentTimeMillis() - begin);
[*]  Assert.assertTrue(result);
[*]  }
[*]
[*]  /**
[*]  * 修改
[*]  * ------------------------------
[*]  */
[*]  @Test
[*]  public void testUpdate() {
[*]  User user = new User();
[*]  user.setId("user1");
[*]  user.setName("new_password");
[*]  boolean result = userDao.update(user);
[*]  Assert.assertTrue(result);
[*]  }
[*]
[*]  /**
[*]  * 通过key删除单个
[*]  * ------------------------------
[*]  */
[*]  @Test
[*]  public void testDelete() {
[*]  String key = "user1";
[*]  userDao.delete(key);
[*]  }
[*]
[*]  /**
[*]  * 批量删除
[*]  * ------------------------------
[*]  */
[*]  @Test
[*]  public void testDeletes() {
[*]  List list = new ArrayList();
[*]  for (int i = 0; i < 10; i++) {
[*]  list.add("user" + i);
[*]  }
[*]  userDao.delete(list);
[*]  }
[*]
[*]  /**
[*]  * 获取
[*]  * ------------------------------
[*]  */
[*]  @Test
[*]  public void testGetUser() {
[*]  String id = "user1";
[*]  User user = userDao.get(id);
[*]  Assert.assertNotNull(user);
[*]  Assert.assertEquals(user.getName(), "java2000_wl");
[*]  }
[*]
[*]  /**
[*]  * 设置userDao
[*]  * @param userDao the userDao to set
[*]  */
[*]  public void setUserDao(IUserDao userDao) {
[*]  this.userDao = userDao;
[*]  }
[*]  }

页: [1]
查看完整版本: Spring4 整合 Redis-11546194