`
liujiekasini0312
  • 浏览: 147157 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring 整合 Redis

 
阅读更多

pom构建:

  1. <modelVersion>4.0.0</modelVersion>
  2. <groupId>com.x.redis</groupId>
  3. <artifactId>springredis</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.springframework.data</groupId>
  8. <artifactId>spring-data-redis</artifactId>
  9. <version>1.0.2.RELEASE</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-test</artifactId>
  14. <version>3.1.2.RELEASE</version>
  15. <scope>test</scope>
  16. </dependency>
  17. <dependency>
  18. <groupId>redis.clients</groupId>
  19. <artifactId>jedis</artifactId>
  20. <version>2.1.0</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>junit</groupId>
  24. <artifactId>junit</artifactId>
  25. <version>4.8.2</version>
  26. <scope>test</scope>
  27. </dependency>
  28. </dependencies>

spring配置文件(applicationContext.xml):

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
  10. <context:property-placeholderlocation="classpath:redis.properties"/>
  11. <beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
  12. <propertyname="maxIdle"value="${redis.maxIdle}"/>
  13. <propertyname="maxActive"value="${redis.maxActive}"/>
  14. <propertyname="maxWait"value="${redis.maxWait}"/>
  15. <propertyname="testOnBorrow"value="${redis.testOnBorrow}"/>
  16. </bean>
  17. <beanid="connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  18. p:host-name="${redis.host}"p:port="${redis.port}"p:password="${redis.pass}"p:pool-config-ref="poolConfig"/>
  19. <beanid="redisTemplate"class="org.springframework.data.redis.core.StringRedisTemplate">
  20. <propertyname="connectionFactory"ref="connectionFactory"/>
  21. </bean>
  22. <beanid="userDao"class="com.x.dao.impl.UserDao"/>
  23. </beans>
redis.properties

  1. #Redissettings
  2. redis.host=localhost
  3. redis.port=6379
  4. redis.pass=java2000_wl
  5. redis.maxIdle=300
  6. redis.maxActive=600
  7. redis.maxWait=1000
  8. redis.testOnBorrow=true

Java代码:

  1. packagecom.x.entity;
  2. importjava.io.Serializable;
  3. /**
  4. *@authorhttp://blog.csdn.net/java2000_wl
  5. *@version<b>1.0</b>
  6. */
  7. publicclassUserimplementsSerializable{
  8. privatestaticfinallongserialVersionUID=-6011241820070393952L;
  9. privateStringid;
  10. privateStringname;
  11. privateStringpassword;
  12. /**
  13. *<br>------------------------------<br>
  14. */
  15. publicUser(){
  16. }
  17. /**
  18. *<br>------------------------------<br>
  19. */
  20. publicUser(Stringid,Stringname,Stringpassword){
  21. super();
  22. this.id=id;
  23. this.name=name;
  24. this.password=password;
  25. }
  26. /**
  27. *获得id
  28. *@returntheid
  29. */
  30. publicStringgetId(){
  31. returnid;
  32. }
  33. /**
  34. *设置id
  35. *@paramidtheidtoset
  36. */
  37. publicvoidsetId(Stringid){
  38. this.id=id;
  39. }
  40. /**
  41. *获得name
  42. *@returnthename
  43. */
  44. publicStringgetName(){
  45. returnname;
  46. }
  47. /**
  48. *设置name
  49. *@paramnamethenametoset
  50. */
  51. publicvoidsetName(Stringname){
  52. this.name=name;
  53. }
  54. /**
  55. *获得password
  56. *@returnthepassword
  57. */
  58. publicStringgetPassword(){
  59. returnpassword;
  60. }
  61. /**
  62. *设置password
  63. *@parampasswordthepasswordtoset
  64. */
  65. publicvoidsetPassword(Stringpassword){
  66. this.password=password;
  67. }
  68. }
  1. packagecom.x.dao;
  2. importorg.springframework.beans.factory.annotation.Autowired;
  3. importorg.springframework.data.redis.core.RedisTemplate;
  4. importorg.springframework.data.redis.serializer.RedisSerializer;
  5. /**
  6. *AbstractBaseRedisDao
  7. *@authorhttp://blog.csdn.net/java2000_wl
  8. *@version<b>1.0</b>
  9. */
  10. publicabstractclassAbstractBaseRedisDao<K,V>{
  11. @Autowired
  12. protectedRedisTemplate<K,V>redisTemplate;
  13. /**
  14. *设置redisTemplate
  15. *@paramredisTemplatetheredisTemplatetoset
  16. */
  17. publicvoidsetRedisTemplate(RedisTemplate<K,V>redisTemplate){
  18. this.redisTemplate=redisTemplate;
  19. }
  20. /**
  21. *获取RedisSerializer
  22. *<br>------------------------------<br>
  23. */
  24. protectedRedisSerializer<String>getRedisSerializer(){
  25. returnredisTemplate.getStringSerializer();
  26. }
  27. }
  1. packagecom.x.dao;
  2. importjava.util.List;
  3. importcom.x.entity.User;
  4. /**
  5. *@authorhttp://blog.csdn.net/java2000_wl
  6. *@version<b>1.0</b>
  7. */
  8. publicinterfaceIUserDao{
  9. /**
  10. *新增
  11. *<br>------------------------------<br>
  12. *@paramuser
  13. *@return
  14. */
  15. booleanadd(Useruser);
  16. /**
  17. *批量新增使用pipeline方式
  18. *<br>------------------------------<br>
  19. *@paramlist
  20. *@return
  21. */
  22. booleanadd(List<User>list);
  23. /**
  24. *删除
  25. *<br>------------------------------<br>
  26. *@paramkey
  27. */
  28. voiddelete(Stringkey);
  29. /**
  30. *删除多个
  31. *<br>------------------------------<br>
  32. *@paramkeys
  33. */
  34. voiddelete(List<String>keys);
  35. /**
  36. *修改
  37. *<br>------------------------------<br>
  38. *@paramuser
  39. *@return
  40. */
  41. booleanupdate(Useruser);
  42. /**
  43. *通过key获取
  44. *<br>------------------------------<br>
  45. *@paramkeyId
  46. *@return
  47. */
  48. Userget(StringkeyId);
  49. }
  1. packagecom.x.dao.impl;
  2. importjava.util.ArrayList;
  3. importjava.util.List;
  4. importorg.springframework.dao.DataAccessException;
  5. importorg.springframework.data.redis.connection.RedisConnection;
  6. importorg.springframework.data.redis.core.RedisCallback;
  7. importorg.springframework.data.redis.serializer.RedisSerializer;
  8. importorg.springframework.util.Assert;
  9. importcom.x.dao.AbstractBaseRedisDao;
  10. importcom.x.dao.IUserDao;
  11. importcom.x.entity.User;
  12. /**
  13. *Dao
  14. *@authorhttp://blog.csdn.net/java2000_wl
  15. *@version<b>1.0</b>
  16. */
  17. publicclassUserDaoextendsAbstractBaseRedisDao<String,User>implementsIUserDao{
  18. /**
  19. *新增
  20. *<br>------------------------------<br>
  21. *@paramuser
  22. *@return
  23. */
  24. publicbooleanadd(finalUseruser){
  25. booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
  26. publicBooleandoInRedis(RedisConnectionconnection)
  27. throwsDataAccessException{
  28. RedisSerializer<String>serializer=getRedisSerializer();
  29. byte[]key=serializer.serialize(user.getId());
  30. byte[]name=serializer.serialize(user.getName());
  31. returnconnection.setNX(key,name);
  32. }
  33. });
  34. returnresult;
  35. }
  36. /**
  37. *批量新增使用pipeline方式
  38. *<br>------------------------------<br>
  39. *@paramlist
  40. *@return
  41. */
  42. publicbooleanadd(finalList<User>list){
  43. Assert.notEmpty(list);
  44. booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
  45. publicBooleandoInRedis(RedisConnectionconnection)
  46. throwsDataAccessException{
  47. RedisSerializer<String>serializer=getRedisSerializer();
  48. for(Useruser:list){
  49. byte[]key=serializer.serialize(user.getId());
  50. byte[]name=serializer.serialize(user.getName());
  51. connection.setNX(key,name);
  52. }
  53. returntrue;
  54. }
  55. },false,true);
  56. returnresult;
  57. }
  58. /**
  59. *删除
  60. *<br>------------------------------<br>
  61. *@paramkey
  62. */
  63. publicvoiddelete(Stringkey){
  64. List<String>list=newArrayList<String>();
  65. list.add(key);
  66. delete(list);
  67. }
  68. /**
  69. *删除多个
  70. *<br>------------------------------<br>
  71. *@paramkeys
  72. */
  73. publicvoiddelete(List<String>keys){
  74. redisTemplate.delete(keys);
  75. }
  76. /**
  77. *修改
  78. *<br>------------------------------<br>
  79. *@paramuser
  80. *@return
  81. */
  82. publicbooleanupdate(finalUseruser){
  83. Stringkey=user.getId();
  84. if(get(key)==null){
  85. thrownewNullPointerException("数据行不存在,key="+key);
  86. }
  87. booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
  88. publicBooleandoInRedis(RedisConnectionconnection)
  89. throwsDataAccessException{
  90. RedisSerializer<String>serializer=getRedisSerializer();
  91. byte[]key=serializer.serialize(user.getId());
  92. byte[]name=serializer.serialize(user.getName());
  93. connection.set(key,name);
  94. returntrue;
  95. }
  96. });
  97. returnresult;
  98. }
  99. /**
  100. *通过key获取
  101. *<br>------------------------------<br>
  102. *@paramkeyId
  103. *@return
  104. */
  105. publicUserget(finalStringkeyId){
  106. Userresult=redisTemplate.execute(newRedisCallback<User>(){
  107. publicUserdoInRedis(RedisConnectionconnection)
  108. throwsDataAccessException{
  109. RedisSerializer<String>serializer=getRedisSerializer();
  110. byte[]key=serializer.serialize(keyId);
  111. byte[]value=connection.get(key);
  112. if(value==null){
  113. returnnull;
  114. }
  115. Stringname=serializer.deserialize(value);
  116. returnnewUser(keyId,name,null);
  117. }
  118. });
  119. returnresult;
  120. }
  121. }
  1. importjava.util.ArrayList;
  2. importjava.util.List;
  3. importjunit.framework.Assert;
  4. importorg.junit.Test;
  5. importorg.springframework.beans.factory.annotation.Autowired;
  6. importorg.springframework.test.context.ContextConfiguration;
  7. importorg.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
  8. importcom.x.dao.IUserDao;
  9. importcom.x.entity.User;
  10. /**
  11. *测试
  12. *@authorhttp://blog.csdn.net/java2000_wl
  13. *@version<b>1.0</b>
  14. */
  15. @ContextConfiguration(locations={"classpath*:applicationContext.xml"})
  16. publicclassRedisTestextendsAbstractJUnit4SpringContextTests{
  17. @Autowired
  18. privateIUserDaouserDao;
  19. /**
  20. *新增
  21. *<br>------------------------------<br>
  22. */
  23. @Test
  24. publicvoidtestAddUser(){
  25. Useruser=newUser();
  26. user.setId("user1");
  27. user.setName("java2000_wl");
  28. booleanresult=userDao.add(user);
  29. Assert.assertTrue(result);
  30. }
  31. /**
  32. *批量新增普通方式
  33. *<br>------------------------------<br>
  34. */
  35. @Test
  36. publicvoidtestAddUsers1(){
  37. List<User>list=newArrayList<User>();
  38. for(inti=10;i<50000;i++){
  39. Useruser=newUser();
  40. user.setId("user"+i);
  41. user.setName("java2000_wl"+i);
  42. list.add(user);
  43. }
  44. longbegin=System.currentTimeMillis();
  45. for(Useruser:list){
  46. userDao.add(user);
  47. }
  48. System.out.println(System.currentTimeMillis()-begin);
  49. }
  50. /**
  51. *批量新增pipeline方式
  52. *<br>------------------------------<br>
  53. */
  54. @Test
  55. publicvoidtestAddUsers2(){
  56. List<User>list=newArrayList<User>();
  57. for(inti=10;i<1500000;i++){
  58. Useruser=newUser();
  59. user.setId("user"+i);
  60. user.setName("java2000_wl"+i);
  61. list.add(user);
  62. }
  63. longbegin=System.currentTimeMillis();
  64. booleanresult=userDao.add(list);
  65. System.out.println(System.currentTimeMillis()-begin);
  66. Assert.assertTrue(result);
  67. }
  68. /**
  69. *修改
  70. *<br>------------------------------<br>
  71. */
  72. @Test
  73. publicvoidtestUpdate(){
  74. Useruser=newUser();
  75. user.setId("user1");
  76. user.setName("new_password");
  77. booleanresult=userDao.update(user);
  78. Assert.assertTrue(result);
  79. }
  80. /**
  81. *通过key删除单个
  82. *<br>------------------------------<br>
  83. */
  84. @Test
  85. publicvoidtestDelete(){
  86. Stringkey="user1";
  87. userDao.delete(key);
  88. }
  89. /**
  90. *批量删除
  91. *<br>------------------------------<br>
  92. */
  93. @Test
  94. publicvoidtestDeletes(){
  95. List<String>list=newArrayList<String>();
  96. for(inti=0;i<10;i++){
  97. list.add("user"+i);
  98. }
  99. userDao.delete(list);
  100. }
  101. /**
  102. *获取
  103. *<br>------------------------------<br>
  104. */
  105. @Test
  106. publicvoidtestGetUser(){
  107. Stringid="user1";
  108. Useruser=userDao.get(id);
  109. Assert.assertNotNull(user);
  110. Assert.assertEquals(user.getName(),"java2000_wl");
  111. }
  112. /**
  113. *设置userDao
  114. *@paramuserDaotheuserDaotoset
  115. */
  116. publicvoidsetUserDao(IUserDaouserDao){
  117. this.userDao=userDao;
  118. }
  119. }

pom构建:

  1. <modelVersion>4.0.0</modelVersion>
  2. <groupId>com.x.redis</groupId>
  3. <artifactId>springredis</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.springframework.data</groupId>
  8. <artifactId>spring-data-redis</artifactId>
  9. <version>1.0.2.RELEASE</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-test</artifactId>
  14. <version>3.1.2.RELEASE</version>
  15. <scope>test</scope>
  16. </dependency>
  17. <dependency>
  18. <groupId>redis.clients</groupId>
  19. <artifactId>jedis</artifactId>
  20. <version>2.1.0</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>junit</groupId>
  24. <artifactId>junit</artifactId>
  25. <version>4.8.2</version>
  26. <scope>test</scope>
  27. </dependency>
  28. </dependencies>

spring配置文件(applicationContext.xml):

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
  10. <context:property-placeholderlocation="classpath:redis.properties"/>
  11. <beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
  12. <propertyname="maxIdle"value="${redis.maxIdle}"/>
  13. <propertyname="maxActive"value="${redis.maxActive}"/>
  14. <propertyname="maxWait"value="${redis.maxWait}"/>
  15. <propertyname="testOnBorrow"value="${redis.testOnBorrow}"/>
  16. </bean>
  17. <beanid="connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  18. p:host-name="${redis.host}"p:port="${redis.port}"p:password="${redis.pass}"p:pool-config-ref="poolConfig"/>
  19. <beanid="redisTemplate"class="org.springframework.data.redis.core.StringRedisTemplate">
  20. <propertyname="connectionFactory"ref="connectionFactory"/>
  21. </bean>
  22. <beanid="userDao"class="com.x.dao.impl.UserDao"/>
  23. </beans>
redis.properties

  1. #Redissettings
  2. redis.host=localhost
  3. redis.port=6379
  4. redis.pass=java2000_wl
  5. redis.maxIdle=300
  6. redis.maxActive=600
  7. redis.maxWait=1000
  8. redis.testOnBorrow=true

Java代码:

  1. packagecom.x.entity;
  2. importjava.io.Serializable;
  3. /**
  4. *@authorhttp://blog.csdn.net/java2000_wl
  5. *@version<b>1.0</b>
  6. */
  7. publicclassUserimplementsSerializable{
  8. privatestaticfinallongserialVersionUID=-6011241820070393952L;
  9. privateStringid;
  10. privateStringname;
  11. privateStringpassword;
  12. /**
  13. *<br>------------------------------<br>
  14. */
  15. publicUser(){
  16. }
  17. /**
  18. *<br>------------------------------<br>
  19. */
  20. publicUser(Stringid,Stringname,Stringpassword){
  21. super();
  22. this.id=id;
  23. this.name=name;
  24. this.password=password;
  25. }
  26. /**
  27. *获得id
  28. *@returntheid
  29. */
  30. publicStringgetId(){
  31. returnid;
  32. }
  33. /**
  34. *设置id
  35. *@paramidtheidtoset
  36. */
  37. publicvoidsetId(Stringid){
  38. this.id=id;
  39. }
  40. /**
  41. *获得name
  42. *@returnthename
  43. */
  44. publicStringgetName(){
  45. returnname;
  46. }
  47. /**
  48. *设置name
  49. *@paramnamethenametoset
  50. */
  51. publicvoidsetName(Stringname){
  52. this.name=name;
  53. }
  54. /**
  55. *获得password
  56. *@returnthepassword
  57. */
  58. publicStringgetPassword(){
  59. returnpassword;
  60. }
  61. /**
  62. *设置password
  63. *@parampasswordthepasswordtoset
  64. */
  65. publicvoidsetPassword(Stringpassword){
  66. this.password=password;
  67. }
  68. }
  1. packagecom.x.dao;
  2. importorg.springframework.beans.factory.annotation.Autowired;
  3. importorg.springframework.data.redis.core.RedisTemplate;
  4. importorg.springframework.data.redis.serializer.RedisSerializer;
  5. /**
  6. *AbstractBaseRedisDao
  7. *@authorhttp://blog.csdn.net/java2000_wl
  8. *@version<b>1.0</b>
  9. */
  10. publicabstractclassAbstractBaseRedisDao<K,V>{
  11. @Autowired
  12. protectedRedisTemplate<K,V>redisTemplate;
  13. /**
  14. *设置redisTemplate
  15. *@paramredisTemplatetheredisTemplatetoset
  16. */
  17. publicvoidsetRedisTemplate(RedisTemplate<K,V>redisTemplate){
  18. this.redisTemplate=redisTemplate;
  19. }
  20. /**
  21. *获取RedisSerializer
  22. *<br>------------------------------<br>
  23. */
  24. protectedRedisSerializer<String>getRedisSerializer(){
  25. returnredisTemplate.getStringSerializer();
  26. }
  27. }
  1. packagecom.x.dao;
  2. importjava.util.List;
  3. importcom.x.entity.User;
  4. /**
  5. *@authorhttp://blog.csdn.net/java2000_wl
  6. *@version<b>1.0</b>
  7. */
  8. publicinterfaceIUserDao{
  9. /**
  10. *新增
  11. *<br>------------------------------<br>
  12. *@paramuser
  13. *@return
  14. */
  15. booleanadd(Useruser);
  16. /**
  17. *批量新增使用pipeline方式
  18. *<br>------------------------------<br>
  19. *@paramlist
  20. *@return
  21. */
  22. booleanadd(List<User>list);
  23. /**
  24. *删除
  25. *<br>------------------------------<br>
  26. *@paramkey
  27. */
  28. voiddelete(Stringkey);
  29. /**
  30. *删除多个
  31. *<br>------------------------------<br>
  32. *@paramkeys
  33. */
  34. voiddelete(List<String>keys);
  35. /**
  36. *修改
  37. *<br>------------------------------<br>
  38. *@paramuser
  39. *@return
  40. */
  41. booleanupdate(Useruser);
  42. /**
  43. *通过key获取
  44. *<br>------------------------------<br>
  45. *@paramkeyId
  46. *@return
  47. */
  48. Userget(StringkeyId);
  49. }
  1. packagecom.x.dao.impl;
  2. importjava.util.ArrayList;
  3. importjava.util.List;
  4. importorg.springframework.dao.DataAccessException;
  5. importorg.springframework.data.redis.connection.RedisConnection;
  6. importorg.springframework.data.redis.core.RedisCallback;
  7. importorg.springframework.data.redis.serializer.RedisSerializer;
  8. importorg.springframework.util.Assert;
  9. importcom.x.dao.AbstractBaseRedisDao;
  10. importcom.x.dao.IUserDao;
  11. importcom.x.entity.User;
  12. /**
  13. *Dao
  14. *@authorhttp://blog.csdn.net/java2000_wl
  15. *@version<b>1.0</b>
  16. */
  17. publicclassUserDaoextendsAbstractBaseRedisDao<String,User>implementsIUserDao{
  18. /**
  19. *新增
  20. *<br>------------------------------<br>
  21. *@paramuser
  22. *@return
  23. */
  24. publicbooleanadd(finalUseruser){
  25. booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
  26. publicBooleandoInRedis(RedisConnectionconnection)
  27. throwsDataAccessException{
  28. RedisSerializer<String>serializer=getRedisSerializer();
  29. byte[]key=serializer.serialize(user.getId());
  30. byte[]name=serializer.serialize(user.getName());
  31. returnconnection.setNX(key,name);
  32. }
  33. });
  34. returnresult;
  35. }
  36. /**
  37. *批量新增使用pipeline方式
  38. *<br>------------------------------<br>
  39. *@paramlist
  40. *@return
  41. */
  42. publicbooleanadd(finalList<User>list){
  43. Assert.notEmpty(list);
  44. booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
  45. publicBooleandoInRedis(RedisConnectionconnection)
  46. throwsDataAccessException{
  47. RedisSerializer<String>serializer=getRedisSerializer();
  48. for(Useruser:list){
  49. byte[]key=serializer.serialize(user.getId());
  50. byte[]name=serializer.serialize(user.getName());
  51. connection.setNX(key,name);
  52. }
  53. returntrue;
  54. }
  55. },false,true);
  56. returnresult;
  57. }
  58. /**
  59. *删除
  60. *<br>------------------------------<br>
  61. *@paramkey
  62. */
  63. publicvoiddelete(Stringkey){
  64. List<String>list=newArrayList<String>();
  65. list.add(key);
  66. delete(list);
  67. }
  68. /**
  69. *删除多个
  70. *<br>------------------------------<br>
  71. *@paramkeys
  72. */
  73. publicvoiddelete(List<String>keys){
  74. redisTemplate.delete(keys);
  75. }
  76. /**
  77. *修改
  78. *<br>------------------------------<br>
  79. *@paramuser
  80. *@return
  81. */
  82. publicbooleanupdate(finalUseruser){
  83. Stringkey=user.getId();
  84. if(get(key)==null){
  85. thrownewNullPointerException("数据行不存在,key="+key);
  86. }
  87. booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
  88. publicBooleandoInRedis(RedisConnectionconnection)
  89. throwsDataAccessException{
  90. RedisSerializer<String>serializer=getRedisSerializer();
  91. byte[]key=serializer.serialize(user.getId());
  92. byte[]name=serializer.serialize(user.getName());
  93. connection.set(key,name);
  94. returntrue;
  95. }
  96. });
  97. returnresult;
  98. }
  99. /**
  100. *通过key获取
  101. *<br>------------------------------<br>
  102. *@paramkeyId
  103. *@return
  104. */
  105. publicUserget(finalStringkeyId){
  106. Userresult=redisTemplate.execute(newRedisCallback<User>(){
  107. publicUserdoInRedis(RedisConnectionconnection)
  108. throwsDataAccessException{
  109. RedisSerializer<String>serializer=getRedisSerializer();
  110. byte[]key=serializer.serialize(keyId);
  111. byte[]value=connection.get(key);
  112. if(value==null){
  113. returnnull;
  114. }
  115. Stringname=serializer.deserialize(value);
  116. returnnewUser(keyId,name,null);
  117. }
  118. });
  119. returnresult;
  120. }
  121. }
  1. importjava.util.ArrayList;
  2. importjava.util.List;
  3. importjunit.framework.Assert;
  4. importorg.junit.Test;
  5. importorg.springframework.beans.factory.annotation.Autowired;
  6. importorg.springframework.test.context.ContextConfiguration;
  7. importorg.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
  8. importcom.x.dao.IUserDao;
  9. importcom.x.entity.User;
  10. /**
  11. *测试
  12. *@authorhttp://blog.csdn.net/java2000_wl
  13. *@version<b>1.0</b>
  14. */
  15. @ContextConfiguration(locations={"classpath*:applicationContext.xml"})
  16. publicclassRedisTestextendsAbstractJUnit4SpringContextTests{
  17. @Autowired
  18. privateIUserDaouserDao;
  19. /**
  20. *新增
  21. *<br>------------------------------<br>
  22. */
  23. @Test
  24. publicvoidtestAddUser(){
  25. Useruser=newUser();
  26. user.setId("user1");
  27. user.setName("java2000_wl");
  28. booleanresult=userDao.add(user);
  29. Assert.assertTrue(result);
  30. }
  31. /**
  32. *批量新增普通方式
  33. *<br>------------------------------<br>
  34. */
  35. @Test
  36. publicvoidtestAddUsers1(){
  37. List<User>list=newArrayList<User>();
  38. for(inti=10;i<50000;i++){
  39. Useruser=newUser();
  40. user.setId("user"+i);
  41. user.setName("java2000_wl"+i);
  42. list.add(user);
  43. }
  44. longbegin=System.currentTimeMillis();
  45. for(Useruser:list){
  46. userDao.add(user);
  47. }
  48. System.out.println(System.currentTimeMillis()-begin);
  49. }
  50. /**
  51. *批量新增pipeline方式
  52. *<br>------------------------------<br>
  53. */
  54. @Test
  55. publicvoidtestAddUsers2(){
  56. List<User>list=newArrayList<User>();
  57. for(inti=10;i<1500000;i++){
  58. Useruser=newUser();
  59. user.setId("user"+i);
  60. user.setName("java2000_wl"+i);
  61. list.add(user);
  62. }
  63. longbegin=System.currentTimeMillis();
  64. booleanresult=userDao.add(list);
  65. System.out.println(System.currentTimeMillis()-begin);
  66. Assert.assertTrue(result);
  67. }
  68. /**
  69. *修改
  70. *<br>------------------------------<br>
  71. */
  72. @Test
  73. publicvoidtestUpdate(){
  74. Useruser=newUser();
  75. user.setId("user1");
  76. user.setName("new_password");
  77. booleanresult=userDao.update(user);
  78. Assert.assertTrue(result);
  79. }
  80. /**
  81. *通过key删除单个
  82. *<br>------------------------------<br>
  83. */
  84. @Test
  85. publicvoidtestDelete(){
  86. Stringkey="user1";
  87. userDao.delete(key);
  88. }
  89. /**
  90. *批量删除
  91. *<br>------------------------------<br>
  92. */
  93. @Test
  94. publicvoidtestDeletes(){
  95. List<String>list=newArrayList<String>();
  96. for(inti=0;i<10;i++){
  97. list.add("user"+i);
  98. }
  99. userDao.delete(list);
  100. }
  101. /**
  102. *获取
  103. *<br>------------------------------<br>
  104. */
  105. @Test
  106. publicvoidtestGetUser(){
  107. Stringid="user1";
  108. Useruser=userDao.get(id);
  109. Assert.assertNotNull(user);
  110. Assert.assertEquals(user.getName(),"java2000_wl");
  111. }
  112. /**
  113. *设置userDao
  114. *@paramuserDaotheuserDaotoset
  115. */
  116. publicvoidsetUserDao(IUserDaouserDao){
  117. this.userDao=userDao;
  118. }
  119. }
分享到:
评论

相关推荐

    spring整合redis

    本文将深入探讨如何将Spring与Redis进行整合,为新手提供一个简单易懂的实践指南。 一、Spring与Redis整合的背景及优势 Spring框架提供了丰富的集成支持,使得与其他技术的整合变得非常方便。整合Spring和Redis...

    spring整合redis小demo

    总的来说,Spring整合Redis使得在Java应用中使用Redis变得更加便捷,无论是简单的字符串操作还是复杂的对象存储,都能通过Spring提供的工具轻松实现。通过上述步骤,你可以构建一个基本的Spring-Redis集成系统,...

    spring整合redis项目

    Spring整合Redis项目是一种常见的数据存储和缓存解决方案,特别是在高并发、大数据量的Web应用中。Redis是一款开源的、高性能的键值对数据库,而Spring是Java领域广泛使用的框架,提供了一整套的企业级应用开发工具...

    Spring整合Redis完整实例代码

    Spring整合Redis完整实例代码,下载经简单的修改配置文件,即可运行,代码中有详细的注释,关于代码的详情可参见博文:http://blog.csdn.net/l1028386804/article/details/52108758

    spring整合redis哨兵源码

    总结来说,Spring整合Redis哨兵模式涉及了Spring Boot的配置、哨兵系统的理解、连接工厂的创建、数据序列化、CRUD操作以及异常处理等多个知识点。通过这样的整合,我们可以构建出高可用、容错性强的Redis服务,为...

    spring整合redis3.2/redis2.1 jar包

    Spring 整合 Redis 是在 Java 开发中常用的一种技术,用于提升应用的缓存和数据处理性能。Redis 是一个开源的、基于键值对的数据存储系统,常被用作数据库、缓存和消息中间件。它支持多种数据结构,如字符串、哈希、...

    spring整合redis(spring模板+连接池+json序列化+cluster集群).zip

    以上就是关于Spring整合Redis的详细解析,涵盖了Spring模板、连接池、JSON序列化和Redis集群的配置与使用。在实际项目中,理解并熟练掌握这些知识点,能帮助我们构建出高效、稳定的Redis集成方案。

    Spring mvc整合redis实例(redis连接池)

    本文将详细介绍如何在Spring MVC中整合Redis,并利用连接池来优化性能。 首先,我们需要引入Redis的相关依赖。在Maven项目中,可以在pom.xml文件中添加`spring-data-redis`和`jedis`库,这两个库分别提供了Spring对...

    Spring整合Redis哨兵

    **Spring整合Redis哨兵** 在高可用性系统中,Redis Sentinel是至关重要的组件,它提供了主从监控、故障检测和自动故障转移的功能。Spring作为Java生态中的主流框架,提供了与Redis Sentinel集成的能力,使得我们...

    Spring整合Redis

    本文将深入探讨如何在Spring项目中整合Redis,以及在测试过程中如何使用JUnit进行单元测试。 1. **Spring Data Redis** Spring Data Redis是Spring Framework的一个模块,它提供了对Redis数据库的抽象层,使得...

    Spring整合Redis用作缓存-注解方式

    至此,你已经掌握了Spring整合Redis作为缓存的基本步骤。这种方式能够有效地提高应用程序的性能,减少对数据库的访问,同时利用Redis的高速读写能力。在实际开发中,还可以根据需求调整序列化策略、缓存策略等,以...

    spring整合redis案例(jedis)

    在本案例中,我们将深入探讨如何在Spring框架中整合Redis数据存储系统,使用Jedis作为客户端库。Redis是一个高性能的键值对存储系统,适用于缓存、消息队列等多种应用场景。而Spring是Java领域广泛使用的应用程序...

    期末向Spring整合Redis源码

    期末向Spring整合Redis源码

    spring整合redis(spring模板+连接池+json序列化+cluster集群).rar

    spring整合redis(spring模板+连接池+json序列化+cluster集群)

    spring整合redis(带spring模板和连接池json和jdk序列化,非集群版).rar

    总结来说,Spring整合Redis的目的是优化数据存取,提高应用性能。通过配置RedisTemplate、连接池和序列化策略,我们可以实现高效、灵活的Redis操作。在这个非集群环境中,我们专注于单实例的使用,但在生产环境中,...

    SpringCloud整合Redis

    SpringCloud整合Redis缓存;版本:SpringCloud :Dalston.SR4;SpringBoot:1.5.14.RELEASE;Redis:3.2.0;Maven :3.5.3.代码下载下来即可用

    Spring3.0整合redis相关jar

    "Spring3.0整合redis相关jar"这个主题主要涉及的是如何在Spring 3.0版本中集成Redis作为数据存储或缓存解决方案。Spring 3.0虽然相对较老,但在当时是广泛使用的版本,因此了解其与Redis的集成方式对维护旧项目或...

Global site tag (gtag.js) - Google Analytics