- 浏览: 147157 次
- 性别:
- 来自: 北京
文章分类
最新评论
Spring 整合 Redis
pom构建:
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.x.redis</groupId>
- <artifactId>springredis</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <dependencies>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.0.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>3.1.2.RELEASE</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.1.0</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.2</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
spring配置文件(applicationContext.xml):
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
- <context:property-placeholderlocation="classpath:redis.properties"/>
- <beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
- <propertyname="maxIdle"value="${redis.maxIdle}"/>
- <propertyname="maxActive"value="${redis.maxActive}"/>
- <propertyname="maxWait"value="${redis.maxWait}"/>
- <propertyname="testOnBorrow"value="${redis.testOnBorrow}"/>
- </bean>
- <beanid="connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="${redis.host}"p:port="${redis.port}"p:password="${redis.pass}"p:pool-config-ref="poolConfig"/>
- <beanid="redisTemplate"class="org.springframework.data.redis.core.StringRedisTemplate">
- <propertyname="connectionFactory"ref="connectionFactory"/>
- </bean>
- <beanid="userDao"class="com.x.dao.impl.UserDao"/>
- </beans>
- #Redissettings
- redis.host=localhost
- redis.port=6379
- redis.pass=java2000_wl
- redis.maxIdle=300
- redis.maxActive=600
- redis.maxWait=1000
- redis.testOnBorrow=true
Java代码:
- packagecom.x.entity;
- importjava.io.Serializable;
- /**
- *@authorhttp://blog.csdn.net/java2000_wl
- *@version<b>1.0</b>
- */
- publicclassUserimplementsSerializable{
- privatestaticfinallongserialVersionUID=-6011241820070393952L;
- privateStringid;
- privateStringname;
- privateStringpassword;
- /**
- *<br>------------------------------<br>
- */
- publicUser(){
- }
- /**
- *<br>------------------------------<br>
- */
- publicUser(Stringid,Stringname,Stringpassword){
- super();
- this.id=id;
- this.name=name;
- this.password=password;
- }
- /**
- *获得id
- *@returntheid
- */
- publicStringgetId(){
- returnid;
- }
- /**
- *设置id
- *@paramidtheidtoset
- */
- publicvoidsetId(Stringid){
- this.id=id;
- }
- /**
- *获得name
- *@returnthename
- */
- publicStringgetName(){
- returnname;
- }
- /**
- *设置name
- *@paramnamethenametoset
- */
- publicvoidsetName(Stringname){
- this.name=name;
- }
- /**
- *获得password
- *@returnthepassword
- */
- publicStringgetPassword(){
- returnpassword;
- }
- /**
- *设置password
- *@parampasswordthepasswordtoset
- */
- publicvoidsetPassword(Stringpassword){
- this.password=password;
- }
- }
- packagecom.x.dao;
- importorg.springframework.beans.factory.annotation.Autowired;
- importorg.springframework.data.redis.core.RedisTemplate;
- importorg.springframework.data.redis.serializer.RedisSerializer;
- /**
- *AbstractBaseRedisDao
- *@authorhttp://blog.csdn.net/java2000_wl
- *@version<b>1.0</b>
- */
- publicabstractclassAbstractBaseRedisDao<K,V>{
- @Autowired
- protectedRedisTemplate<K,V>redisTemplate;
- /**
- *设置redisTemplate
- *@paramredisTemplatetheredisTemplatetoset
- */
- publicvoidsetRedisTemplate(RedisTemplate<K,V>redisTemplate){
- this.redisTemplate=redisTemplate;
- }
- /**
- *获取RedisSerializer
- *<br>------------------------------<br>
- */
- protectedRedisSerializer<String>getRedisSerializer(){
- returnredisTemplate.getStringSerializer();
- }
- }
- packagecom.x.dao;
- importjava.util.List;
- importcom.x.entity.User;
- /**
- *@authorhttp://blog.csdn.net/java2000_wl
- *@version<b>1.0</b>
- */
- publicinterfaceIUserDao{
- /**
- *新增
- *<br>------------------------------<br>
- *@paramuser
- *@return
- */
- booleanadd(Useruser);
- /**
- *批量新增使用pipeline方式
- *<br>------------------------------<br>
- *@paramlist
- *@return
- */
- booleanadd(List<User>list);
- /**
- *删除
- *<br>------------------------------<br>
- *@paramkey
- */
- voiddelete(Stringkey);
- /**
- *删除多个
- *<br>------------------------------<br>
- *@paramkeys
- */
- voiddelete(List<String>keys);
- /**
- *修改
- *<br>------------------------------<br>
- *@paramuser
- *@return
- */
- booleanupdate(Useruser);
- /**
- *通过key获取
- *<br>------------------------------<br>
- *@paramkeyId
- *@return
- */
- Userget(StringkeyId);
- }
- packagecom.x.dao.impl;
- importjava.util.ArrayList;
- importjava.util.List;
- importorg.springframework.dao.DataAccessException;
- importorg.springframework.data.redis.connection.RedisConnection;
- importorg.springframework.data.redis.core.RedisCallback;
- importorg.springframework.data.redis.serializer.RedisSerializer;
- importorg.springframework.util.Assert;
- importcom.x.dao.AbstractBaseRedisDao;
- importcom.x.dao.IUserDao;
- importcom.x.entity.User;
- /**
- *Dao
- *@authorhttp://blog.csdn.net/java2000_wl
- *@version<b>1.0</b>
- */
- publicclassUserDaoextendsAbstractBaseRedisDao<String,User>implementsIUserDao{
- /**
- *新增
- *<br>------------------------------<br>
- *@paramuser
- *@return
- */
- publicbooleanadd(finalUseruser){
- booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
- publicBooleandoInRedis(RedisConnectionconnection)
- throwsDataAccessException{
- RedisSerializer<String>serializer=getRedisSerializer();
- byte[]key=serializer.serialize(user.getId());
- byte[]name=serializer.serialize(user.getName());
- returnconnection.setNX(key,name);
- }
- });
- returnresult;
- }
- /**
- *批量新增使用pipeline方式
- *<br>------------------------------<br>
- *@paramlist
- *@return
- */
- publicbooleanadd(finalList<User>list){
- Assert.notEmpty(list);
- booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
- publicBooleandoInRedis(RedisConnectionconnection)
- throwsDataAccessException{
- RedisSerializer<String>serializer=getRedisSerializer();
- for(Useruser:list){
- byte[]key=serializer.serialize(user.getId());
- byte[]name=serializer.serialize(user.getName());
- connection.setNX(key,name);
- }
- returntrue;
- }
- },false,true);
- returnresult;
- }
- /**
- *删除
- *<br>------------------------------<br>
- *@paramkey
- */
- publicvoiddelete(Stringkey){
- List<String>list=newArrayList<String>();
- list.add(key);
- delete(list);
- }
- /**
- *删除多个
- *<br>------------------------------<br>
- *@paramkeys
- */
- publicvoiddelete(List<String>keys){
- redisTemplate.delete(keys);
- }
- /**
- *修改
- *<br>------------------------------<br>
- *@paramuser
- *@return
- */
- publicbooleanupdate(finalUseruser){
- Stringkey=user.getId();
- if(get(key)==null){
- thrownewNullPointerException("数据行不存在,key="+key);
- }
- booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
- publicBooleandoInRedis(RedisConnectionconnection)
- throwsDataAccessException{
- RedisSerializer<String>serializer=getRedisSerializer();
- byte[]key=serializer.serialize(user.getId());
- byte[]name=serializer.serialize(user.getName());
- connection.set(key,name);
- returntrue;
- }
- });
- returnresult;
- }
- /**
- *通过key获取
- *<br>------------------------------<br>
- *@paramkeyId
- *@return
- */
- publicUserget(finalStringkeyId){
- Userresult=redisTemplate.execute(newRedisCallback<User>(){
- publicUserdoInRedis(RedisConnectionconnection)
- throwsDataAccessException{
- RedisSerializer<String>serializer=getRedisSerializer();
- byte[]key=serializer.serialize(keyId);
- byte[]value=connection.get(key);
- if(value==null){
- returnnull;
- }
- Stringname=serializer.deserialize(value);
- returnnewUser(keyId,name,null);
- }
- });
- returnresult;
- }
- }
- importjava.util.ArrayList;
- importjava.util.List;
- importjunit.framework.Assert;
- importorg.junit.Test;
- importorg.springframework.beans.factory.annotation.Autowired;
- importorg.springframework.test.context.ContextConfiguration;
- importorg.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
- importcom.x.dao.IUserDao;
- importcom.x.entity.User;
- /**
- *测试
- *@authorhttp://blog.csdn.net/java2000_wl
- *@version<b>1.0</b>
- */
- @ContextConfiguration(locations={"classpath*:applicationContext.xml"})
- publicclassRedisTestextendsAbstractJUnit4SpringContextTests{
- @Autowired
- privateIUserDaouserDao;
- /**
- *新增
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestAddUser(){
- Useruser=newUser();
- user.setId("user1");
- user.setName("java2000_wl");
- booleanresult=userDao.add(user);
- Assert.assertTrue(result);
- }
- /**
- *批量新增普通方式
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestAddUsers1(){
- List<User>list=newArrayList<User>();
- for(inti=10;i<50000;i++){
- Useruser=newUser();
- user.setId("user"+i);
- user.setName("java2000_wl"+i);
- list.add(user);
- }
- longbegin=System.currentTimeMillis();
- for(Useruser:list){
- userDao.add(user);
- }
- System.out.println(System.currentTimeMillis()-begin);
- }
- /**
- *批量新增pipeline方式
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestAddUsers2(){
- List<User>list=newArrayList<User>();
- for(inti=10;i<1500000;i++){
- Useruser=newUser();
- user.setId("user"+i);
- user.setName("java2000_wl"+i);
- list.add(user);
- }
- longbegin=System.currentTimeMillis();
- booleanresult=userDao.add(list);
- System.out.println(System.currentTimeMillis()-begin);
- Assert.assertTrue(result);
- }
- /**
- *修改
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestUpdate(){
- Useruser=newUser();
- user.setId("user1");
- user.setName("new_password");
- booleanresult=userDao.update(user);
- Assert.assertTrue(result);
- }
- /**
- *通过key删除单个
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestDelete(){
- Stringkey="user1";
- userDao.delete(key);
- }
- /**
- *批量删除
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestDeletes(){
- List<String>list=newArrayList<String>();
- for(inti=0;i<10;i++){
- list.add("user"+i);
- }
- userDao.delete(list);
- }
- /**
- *获取
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestGetUser(){
- Stringid="user1";
- Useruser=userDao.get(id);
- Assert.assertNotNull(user);
- Assert.assertEquals(user.getName(),"java2000_wl");
- }
- /**
- *设置userDao
- *@paramuserDaotheuserDaotoset
- */
- publicvoidsetUserDao(IUserDaouserDao){
- this.userDao=userDao;
- }
- }
pom构建:
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.x.redis</groupId>
- <artifactId>springredis</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <dependencies>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.0.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>3.1.2.RELEASE</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.1.0</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.2</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
spring配置文件(applicationContext.xml):
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
- <context:property-placeholderlocation="classpath:redis.properties"/>
- <beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
- <propertyname="maxIdle"value="${redis.maxIdle}"/>
- <propertyname="maxActive"value="${redis.maxActive}"/>
- <propertyname="maxWait"value="${redis.maxWait}"/>
- <propertyname="testOnBorrow"value="${redis.testOnBorrow}"/>
- </bean>
- <beanid="connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="${redis.host}"p:port="${redis.port}"p:password="${redis.pass}"p:pool-config-ref="poolConfig"/>
- <beanid="redisTemplate"class="org.springframework.data.redis.core.StringRedisTemplate">
- <propertyname="connectionFactory"ref="connectionFactory"/>
- </bean>
- <beanid="userDao"class="com.x.dao.impl.UserDao"/>
- </beans>
- #Redissettings
- redis.host=localhost
- redis.port=6379
- redis.pass=java2000_wl
- redis.maxIdle=300
- redis.maxActive=600
- redis.maxWait=1000
- redis.testOnBorrow=true
Java代码:
- packagecom.x.entity;
- importjava.io.Serializable;
- /**
- *@authorhttp://blog.csdn.net/java2000_wl
- *@version<b>1.0</b>
- */
- publicclassUserimplementsSerializable{
- privatestaticfinallongserialVersionUID=-6011241820070393952L;
- privateStringid;
- privateStringname;
- privateStringpassword;
- /**
- *<br>------------------------------<br>
- */
- publicUser(){
- }
- /**
- *<br>------------------------------<br>
- */
- publicUser(Stringid,Stringname,Stringpassword){
- super();
- this.id=id;
- this.name=name;
- this.password=password;
- }
- /**
- *获得id
- *@returntheid
- */
- publicStringgetId(){
- returnid;
- }
- /**
- *设置id
- *@paramidtheidtoset
- */
- publicvoidsetId(Stringid){
- this.id=id;
- }
- /**
- *获得name
- *@returnthename
- */
- publicStringgetName(){
- returnname;
- }
- /**
- *设置name
- *@paramnamethenametoset
- */
- publicvoidsetName(Stringname){
- this.name=name;
- }
- /**
- *获得password
- *@returnthepassword
- */
- publicStringgetPassword(){
- returnpassword;
- }
- /**
- *设置password
- *@parampasswordthepasswordtoset
- */
- publicvoidsetPassword(Stringpassword){
- this.password=password;
- }
- }
- packagecom.x.dao;
- importorg.springframework.beans.factory.annotation.Autowired;
- importorg.springframework.data.redis.core.RedisTemplate;
- importorg.springframework.data.redis.serializer.RedisSerializer;
- /**
- *AbstractBaseRedisDao
- *@authorhttp://blog.csdn.net/java2000_wl
- *@version<b>1.0</b>
- */
- publicabstractclassAbstractBaseRedisDao<K,V>{
- @Autowired
- protectedRedisTemplate<K,V>redisTemplate;
- /**
- *设置redisTemplate
- *@paramredisTemplatetheredisTemplatetoset
- */
- publicvoidsetRedisTemplate(RedisTemplate<K,V>redisTemplate){
- this.redisTemplate=redisTemplate;
- }
- /**
- *获取RedisSerializer
- *<br>------------------------------<br>
- */
- protectedRedisSerializer<String>getRedisSerializer(){
- returnredisTemplate.getStringSerializer();
- }
- }
- packagecom.x.dao;
- importjava.util.List;
- importcom.x.entity.User;
- /**
- *@authorhttp://blog.csdn.net/java2000_wl
- *@version<b>1.0</b>
- */
- publicinterfaceIUserDao{
- /**
- *新增
- *<br>------------------------------<br>
- *@paramuser
- *@return
- */
- booleanadd(Useruser);
- /**
- *批量新增使用pipeline方式
- *<br>------------------------------<br>
- *@paramlist
- *@return
- */
- booleanadd(List<User>list);
- /**
- *删除
- *<br>------------------------------<br>
- *@paramkey
- */
- voiddelete(Stringkey);
- /**
- *删除多个
- *<br>------------------------------<br>
- *@paramkeys
- */
- voiddelete(List<String>keys);
- /**
- *修改
- *<br>------------------------------<br>
- *@paramuser
- *@return
- */
- booleanupdate(Useruser);
- /**
- *通过key获取
- *<br>------------------------------<br>
- *@paramkeyId
- *@return
- */
- Userget(StringkeyId);
- }
- packagecom.x.dao.impl;
- importjava.util.ArrayList;
- importjava.util.List;
- importorg.springframework.dao.DataAccessException;
- importorg.springframework.data.redis.connection.RedisConnection;
- importorg.springframework.data.redis.core.RedisCallback;
- importorg.springframework.data.redis.serializer.RedisSerializer;
- importorg.springframework.util.Assert;
- importcom.x.dao.AbstractBaseRedisDao;
- importcom.x.dao.IUserDao;
- importcom.x.entity.User;
- /**
- *Dao
- *@authorhttp://blog.csdn.net/java2000_wl
- *@version<b>1.0</b>
- */
- publicclassUserDaoextendsAbstractBaseRedisDao<String,User>implementsIUserDao{
- /**
- *新增
- *<br>------------------------------<br>
- *@paramuser
- *@return
- */
- publicbooleanadd(finalUseruser){
- booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
- publicBooleandoInRedis(RedisConnectionconnection)
- throwsDataAccessException{
- RedisSerializer<String>serializer=getRedisSerializer();
- byte[]key=serializer.serialize(user.getId());
- byte[]name=serializer.serialize(user.getName());
- returnconnection.setNX(key,name);
- }
- });
- returnresult;
- }
- /**
- *批量新增使用pipeline方式
- *<br>------------------------------<br>
- *@paramlist
- *@return
- */
- publicbooleanadd(finalList<User>list){
- Assert.notEmpty(list);
- booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
- publicBooleandoInRedis(RedisConnectionconnection)
- throwsDataAccessException{
- RedisSerializer<String>serializer=getRedisSerializer();
- for(Useruser:list){
- byte[]key=serializer.serialize(user.getId());
- byte[]name=serializer.serialize(user.getName());
- connection.setNX(key,name);
- }
- returntrue;
- }
- },false,true);
- returnresult;
- }
- /**
- *删除
- *<br>------------------------------<br>
- *@paramkey
- */
- publicvoiddelete(Stringkey){
- List<String>list=newArrayList<String>();
- list.add(key);
- delete(list);
- }
- /**
- *删除多个
- *<br>------------------------------<br>
- *@paramkeys
- */
- publicvoiddelete(List<String>keys){
- redisTemplate.delete(keys);
- }
- /**
- *修改
- *<br>------------------------------<br>
- *@paramuser
- *@return
- */
- publicbooleanupdate(finalUseruser){
- Stringkey=user.getId();
- if(get(key)==null){
- thrownewNullPointerException("数据行不存在,key="+key);
- }
- booleanresult=redisTemplate.execute(newRedisCallback<Boolean>(){
- publicBooleandoInRedis(RedisConnectionconnection)
- throwsDataAccessException{
- RedisSerializer<String>serializer=getRedisSerializer();
- byte[]key=serializer.serialize(user.getId());
- byte[]name=serializer.serialize(user.getName());
- connection.set(key,name);
- returntrue;
- }
- });
- returnresult;
- }
- /**
- *通过key获取
- *<br>------------------------------<br>
- *@paramkeyId
- *@return
- */
- publicUserget(finalStringkeyId){
- Userresult=redisTemplate.execute(newRedisCallback<User>(){
- publicUserdoInRedis(RedisConnectionconnection)
- throwsDataAccessException{
- RedisSerializer<String>serializer=getRedisSerializer();
- byte[]key=serializer.serialize(keyId);
- byte[]value=connection.get(key);
- if(value==null){
- returnnull;
- }
- Stringname=serializer.deserialize(value);
- returnnewUser(keyId,name,null);
- }
- });
- returnresult;
- }
- }
- importjava.util.ArrayList;
- importjava.util.List;
- importjunit.framework.Assert;
- importorg.junit.Test;
- importorg.springframework.beans.factory.annotation.Autowired;
- importorg.springframework.test.context.ContextConfiguration;
- importorg.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
- importcom.x.dao.IUserDao;
- importcom.x.entity.User;
- /**
- *测试
- *@authorhttp://blog.csdn.net/java2000_wl
- *@version<b>1.0</b>
- */
- @ContextConfiguration(locations={"classpath*:applicationContext.xml"})
- publicclassRedisTestextendsAbstractJUnit4SpringContextTests{
- @Autowired
- privateIUserDaouserDao;
- /**
- *新增
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestAddUser(){
- Useruser=newUser();
- user.setId("user1");
- user.setName("java2000_wl");
- booleanresult=userDao.add(user);
- Assert.assertTrue(result);
- }
- /**
- *批量新增普通方式
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestAddUsers1(){
- List<User>list=newArrayList<User>();
- for(inti=10;i<50000;i++){
- Useruser=newUser();
- user.setId("user"+i);
- user.setName("java2000_wl"+i);
- list.add(user);
- }
- longbegin=System.currentTimeMillis();
- for(Useruser:list){
- userDao.add(user);
- }
- System.out.println(System.currentTimeMillis()-begin);
- }
- /**
- *批量新增pipeline方式
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestAddUsers2(){
- List<User>list=newArrayList<User>();
- for(inti=10;i<1500000;i++){
- Useruser=newUser();
- user.setId("user"+i);
- user.setName("java2000_wl"+i);
- list.add(user);
- }
- longbegin=System.currentTimeMillis();
- booleanresult=userDao.add(list);
- System.out.println(System.currentTimeMillis()-begin);
- Assert.assertTrue(result);
- }
- /**
- *修改
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestUpdate(){
- Useruser=newUser();
- user.setId("user1");
- user.setName("new_password");
- booleanresult=userDao.update(user);
- Assert.assertTrue(result);
- }
- /**
- *通过key删除单个
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestDelete(){
- Stringkey="user1";
- userDao.delete(key);
- }
- /**
- *批量删除
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestDeletes(){
- List<String>list=newArrayList<String>();
- for(inti=0;i<10;i++){
- list.add("user"+i);
- }
- userDao.delete(list);
- }
- /**
- *获取
- *<br>------------------------------<br>
- */
- @Test
- publicvoidtestGetUser(){
- Stringid="user1";
- Useruser=userDao.get(id);
- Assert.assertNotNull(user);
- Assert.assertEquals(user.getName(),"java2000_wl");
- }
- /**
- *设置userDao
- *@paramuserDaotheuserDaotoset
- */
- publicvoidsetUserDao(IUserDaouserDao){
- this.userDao=userDao;
- }
- }
相关推荐
本文将深入探讨如何将Spring与Redis进行整合,为新手提供一个简单易懂的实践指南。 一、Spring与Redis整合的背景及优势 Spring框架提供了丰富的集成支持,使得与其他技术的整合变得非常方便。整合Spring和Redis...
总的来说,Spring整合Redis使得在Java应用中使用Redis变得更加便捷,无论是简单的字符串操作还是复杂的对象存储,都能通过Spring提供的工具轻松实现。通过上述步骤,你可以构建一个基本的Spring-Redis集成系统,...
Spring整合Redis项目是一种常见的数据存储和缓存解决方案,特别是在高并发、大数据量的Web应用中。Redis是一款开源的、高性能的键值对数据库,而Spring是Java领域广泛使用的框架,提供了一整套的企业级应用开发工具...
Spring整合Redis完整实例代码,下载经简单的修改配置文件,即可运行,代码中有详细的注释,关于代码的详情可参见博文:http://blog.csdn.net/l1028386804/article/details/52108758
总结来说,Spring整合Redis哨兵模式涉及了Spring Boot的配置、哨兵系统的理解、连接工厂的创建、数据序列化、CRUD操作以及异常处理等多个知识点。通过这样的整合,我们可以构建出高可用、容错性强的Redis服务,为...
Spring 整合 Redis 是在 Java 开发中常用的一种技术,用于提升应用的缓存和数据处理性能。Redis 是一个开源的、基于键值对的数据存储系统,常被用作数据库、缓存和消息中间件。它支持多种数据结构,如字符串、哈希、...
以上就是关于Spring整合Redis的详细解析,涵盖了Spring模板、连接池、JSON序列化和Redis集群的配置与使用。在实际项目中,理解并熟练掌握这些知识点,能帮助我们构建出高效、稳定的Redis集成方案。
本文将详细介绍如何在Spring MVC中整合Redis,并利用连接池来优化性能。 首先,我们需要引入Redis的相关依赖。在Maven项目中,可以在pom.xml文件中添加`spring-data-redis`和`jedis`库,这两个库分别提供了Spring对...
**Spring整合Redis哨兵** 在高可用性系统中,Redis Sentinel是至关重要的组件,它提供了主从监控、故障检测和自动故障转移的功能。Spring作为Java生态中的主流框架,提供了与Redis Sentinel集成的能力,使得我们...
本文将深入探讨如何在Spring项目中整合Redis,以及在测试过程中如何使用JUnit进行单元测试。 1. **Spring Data Redis** Spring Data Redis是Spring Framework的一个模块,它提供了对Redis数据库的抽象层,使得...
至此,你已经掌握了Spring整合Redis作为缓存的基本步骤。这种方式能够有效地提高应用程序的性能,减少对数据库的访问,同时利用Redis的高速读写能力。在实际开发中,还可以根据需求调整序列化策略、缓存策略等,以...
在本案例中,我们将深入探讨如何在Spring框架中整合Redis数据存储系统,使用Jedis作为客户端库。Redis是一个高性能的键值对存储系统,适用于缓存、消息队列等多种应用场景。而Spring是Java领域广泛使用的应用程序...
期末向Spring整合Redis源码
spring整合redis(spring模板+连接池+json序列化+cluster集群)
总结来说,Spring整合Redis的目的是优化数据存取,提高应用性能。通过配置RedisTemplate、连接池和序列化策略,我们可以实现高效、灵活的Redis操作。在这个非集群环境中,我们专注于单实例的使用,但在生产环境中,...
SpringCloud整合Redis缓存;版本:SpringCloud :Dalston.SR4;SpringBoot:1.5.14.RELEASE;Redis:3.2.0;Maven :3.5.3.代码下载下来即可用
"Spring3.0整合redis相关jar"这个主题主要涉及的是如何在Spring 3.0版本中集成Redis作为数据存储或缓存解决方案。Spring 3.0虽然相对较老,但在当时是广泛使用的版本,因此了解其与Redis的集成方式对维护旧项目或...