本文章从头开始介绍spring集成redis的示例。
eclipse工程结构
如下图为我的示例工程的结构图,采用maven构建。其中需要集成spring,因此需要beans.xml文件配置spring的依赖注入,redis.properties配置连接服务器的配置信息。
其中工程中beans.xml和redis.properties文件直接放在了根目录,有需要的读者可以放到resource目录中。
pom依赖
如下为示例pom依赖,spring集成redis需要依赖的包为:jedis包,spring-context模块及依赖的包,spring-data-redis模块包,spring-test包用于junit测试,pom.xml文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<project xmlns= "https://maven.apache.org/pom/4.0.0" xmlns:xsi= "https://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "https://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.test</groupid> javatest</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>javatest</name> <url>https: //maven.apache.org</url> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> </properties> <dependencies> <dependency> <groupid>junit</groupid> junit</artifactid> <version> 4.12 </version> <scope>test</scope> </dependency> <dependency> <groupid>redis.clients</groupid> jedis</artifactid> <version> 2.5 . 1 </version> </dependency> <dependency> <groupid>org.springframework</groupid> spring-context</artifactid> <version> 4.2 . 6 .release</version> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework</groupid> spring-test</artifactid> <version> 4.2 . 6 .release</version> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework.data</groupid> spring-data-redis</artifactid> <version> 1.7 . 2 .release</version> </dependency> </dependencies> </project> |
spring配置
spring配置文件beans.xml的配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<!--?xml version= "1.0" encoding= "utf-8" ?--> <beans xmlns= "https://www.springframework.org/schema/beans" xmlns:aop= "https://www.springframework.org/schema/aop" xmlns:context= "https://www.springframework.org/schema/context" xmlns:jee= "https://www.springframework.org/schema/jee" xmlns:p= "https://www.springframework.org/schema/p" xmlns:tx= "https://www.springframework.org/schema/tx" xmlns:xsi= "https://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" https: //www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd https: //www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载classpath下的redis配置文件 --> <context:property-placeholder location= "classpath:redis.properties" > <bean class = "redis.clients.jedis.jedispoolconfig" id= "poolconfig" > <property name= "maxidle" value= "${redis.maxidle}" > <property name= "testonborrow" value= "${redis.testonborrow}" > </property></property></bean> <bean class = "org.springframework.data.redis.connection.jedis.jedisconnectionfactory" id= "connectionfactory" p:host-name= "${redis.host}" p:password= "${redis.pass}" p:pool-config-ref= "poolconfig" p:port= "${redis.port}" > <!-- spring提供的模板类 --> <bean class = "org.springframework.data.redis.core.stringredistemplate" id= "redistemplate" > <property name= "connectionfactory" ref= "connectionfactory" > </property></bean> <bean class = "com.redis.test.userdao" id= "userdao" > <property name= "redistemplate" ref= "redistemplate" > </property></bean> </bean></context:property-placeholder></beans> |
在beans.xml配置文件中,需要先加载redis.properties文件。
redis配置信息
redis的配置信息在redis.properties文件中配置:
1
2
3
4
5
6
7
|
# redis地址和端口和连接密码 redis.host=localhost redis.port= 6379 redis.pass= redis.maxidle= 300 redis.testonborrow= true |
此示例,连接redis服务器时没有设置连接密码,因此不用填值。
java代码
user.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package com.redis.test; import java.io.serializable; public class user implements serializable { private static final long serialversionuid = 3409768855488864675l; private string id; private string name; private string password; public user() { } public user(string id, string name, string password) { this .id = id; this .name = name; this .password = password; } 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; } public string getpassword() { return password; } public void setpassword(string password) { this .password = password; } public string tostring() { return "user [id=" + id + ", name=" + name + ", password=" + password + "]" ; } } |
abstractredisbasedao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.redis.test; import org.springframework.data.redis.core.redistemplate; import org.springframework.data.redis.serializer.redisserializer; public abstract class abstractredisbasedao<k, v= "" > { protected redistemplate<k, v= "" > redistemplate; public redistemplate<k, v= "" > getredistemplate() { return redistemplate; } public void setredistemplate(redistemplate<k, v= "" > redistemplate) { this .redistemplate = redistemplate; } /** * 获取 redisserializer */ protected redisserializer<string> getredisserializer() { return redistemplate.getstringserializer(); } } |
iuserdao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.redis.test; import java.util.list; public interface iuserdao { /** 新增 */ boolean add(user user); /** 批量新增,pipeline方式 */ boolean add(list<user> list); /** 删除 */ void delete(string key); /** 批量删除 */ void delete(list<string> keys); /** 更新 */ boolean update(user user); /** 读取 */ user get(string keyid); } |
userdao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package com.redis.test; 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 ; public class userdao extends abstractredisbasedao<string, user= "" > implements iuserdao { public boolean add( final user user) { boolean result = redistemplate.execute( new rediscallback< boolean >() { public boolean doinredis(redisconnection connection) throws dataaccessexception { redisserializer<string> serializer = getredisserializer(); byte [] key = serializer.serialize(user.getid()); // 将id序列化成key byte [] value = serializer.serialize(user.getname()); return connection.setnx(key, value); } } ); return result; } public boolean add( final list<user> list) { assert .notempty(list); boolean result = redistemplate.execute( new rediscallback< boolean >() { public boolean doinredis(redisconnection connection) throws dataaccessexception { redisserializer<string> serializer = getredisserializer(); for ( int i = 0 ; i < list.size(); i++) { user user = list.get(i); byte [] key = serializer.serialize(user.getid()); // 将id序列化成key byte [] value = serializer.serialize(user.getname()); connection.setnx(key, value); } return true ; } } , false , true ); return result; } public void delete(string key) { redistemplate.delete(key); } public void delete(list<string> keys) { redistemplate.delete(keys); } 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< boolean >() { public boolean doinredis(redisconnection connection) throws dataaccessexception { redisserializer<string> serializer = getredisserializer(); byte [] key = serializer.serialize(user.getid()); // 将id序列化成key byte [] value = serializer.serialize(user.getname()); connection.set(key, value); return true ; } } ); return result; } public user get( final string keyid) { user user = redistemplate.execute( new rediscallback<user>() { public user doinredis(redisconnection connection) throws dataaccessexception { redisserializer<string> 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 user; } } |
redistest.java(junit测试类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
package com.redis.test; import java.util.arraylist; import java.util.list; 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 org.springframework.util. assert ; /** * junit在spring context环境下测试 */ @contextconfiguration (locations={ "classpath*:beans.xml" }) public class redistest extends abstractjunit4springcontexttests { @autowired private iuserdao userdao; /** 增加单个用户 */ @test public void testadduser() { user user = new user( "user1" , "password1" , null ); boolean result = userdao.add(user); assert .istrue(result); system.out.println( "添加结果:" + result); } /** 批量新增普通方式,5286ms */ @test public void testaddusers1() { list<user> list = new arraylist<user>(); for ( int i = 10 ; i < 50000 ; i++) { user user = new user(); user.setid( "user" + i); user.setname( "password" + i); list.add(user); } long begin = system.currenttimemillis(); for (user user : list) { userdao.add(user); } system.out.println(system.currenttimemillis() - begin); } /** 批量新增pipeline方式,484ms */ @test public void testaddusers2() { list<user> list = new arraylist<user>(); for ( int i = 50000 ; i < 100000 ; i++) { user user = new user(); user.setid( "user" + i); user.setname( "password" + i); list.add(user); } long begin = system.currenttimemillis(); boolean result = userdao.add(list); assert .istrue(result); system.out.println(system.currenttimemillis() - begin); } /** 更新 */ @test public void testupdate() { user user = new user(); user.setid( "user1" ); user.setname( "new_password" ); boolean result = userdao.update(user); assert .istrue(result); } /** 删除 */ @test public void testdelete() { string key = "user1" ; userdao.delete(key); } /** 批量删除 */ @test public void testdeletes() { list<string> list = new arraylist<string>(); 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 .notnull(user); system.out.println(user); } } |
总结
以上就是本文关于spring集成redis详解代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:https://www.2cto.com/kf/201609/543944.html