Spring整合Redis
通过 jedis 客户端来和redis服务器通信,或者java直接通过 jni 调用redis服务不过很麻烦
为什么使用 jedis 作为客户端,是因为 jedis 支持集群
笔记代码参考 G:\学习资料\java\java学习文档\java技术文档\Spring文档\xufei-spring-word\xufei-projo-spring-redis
# 1、导包
<!--导入redis的包-->
<!--redis客户端 jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<!--spring-data-redis jedis-->
<!--Spring对Redis的支持 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.4.RELEASE</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 2、编写配置文件bean-redis.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd"
>
<!-- redis数据源 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大的空闲数量-->
<property name="maxIdle" value="100"></property>
<!--最小的空闲数-->
<property name="minIdle" value="50"></property>
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="2000"/>
<!-- 返回连接时,检测连接是否成功 -->
<property name="testOnBorrow" value="true"/>
</bean>
<!-- Spring-redis连接池管理工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- IP地址 -->
<property name="hostName" value="106.54.13.167"/>
<!-- 端口号 -->
<property name="port" value="6379"/>
<!-- 连接池配置引用 -->
<property name="poolConfig" ref="poolConfig"/>
<!-- usePool:是否使用连接池 -->
<property name="usePool" value="true"/>
</bean>
<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="com.xf.meeting.serilizer.MyRedisSerializer"/>
</property>
<!--开启事务 -->
<property name="enableTransactionSupport" value="true"></property>
</bean>
</beans>
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
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
# 3、使用RedisTemplate对象
@Component
public class RedisService implements IRedisService {
@Autowired
private RedisTemplate redisTemplate;
@Override
public void save(String key, User user) {
// redisTemplate.opsForValue(); //这个是操作String类型的
// redisTemplate.opsForHash(); //这个是操作hash的
// redisTemplate.opsForSet(); //这个是操作Set的
// redisTemplate.opsForZSet(); //这个是操作SortedSet的
// redisTemplate.opsForList(); //这个是操作List的
//操作的key的
// redisTemplate.
redisTemplate.opsForValue().set(key,user);
Object khg = redisTemplate.opsForValue().get("khg");
System.out.println("接受到 服务器返回的数据是:"+khg);
}
@Override
public User get(String key) {
return null;
}
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 4、Redis中的序列化器
Redis中序列化器的作用:
1:写入到Redis中的数据 使用什么格式写进去 你可以自定义
2:从Redis中将数据读取出来 这个数据 怎么解析
public class MyRedisSerializer implements RedisSerializer<User> {
/**
* 把原始的数据 转换成某一个格式放到数据库中
* @param user
* @return
* @throws SerializationException
*/
@Override
public byte[] serialize(User user) throws SerializationException {
//这里就是要放到Redis中的数据格式
//转换成json格式的字符串
String jsonString = JSON.toJSONString(user);
//接下来将字符串转换成byte数组
return jsonString.getBytes(Charset.forName("UTF-8"));
}
/**
* 这个表示的是从数据中 读取出来的数据 转换成我们想要的Java对象
* @param bytes
* @return
* @throws SerializationException
*/
@Override
public User deserialize(byte[] bytes) throws SerializationException {
String s=null;
User user=null;
try {
if(null!=bytes){
s = new String(bytes, "UTF-8");
//接下来:使用FastJSON直接转换成java对象
user=JSON.parseObject(s,User.class);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return user;
}
}
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
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
最近更新: 2025/07/30, 15:37:56