欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

012_Spring Data Redis

发布时间:2025/5/22 javascript 31 豆豆
生活随笔 收集整理的这篇文章主要介绍了 012_Spring Data Redis 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1. Spring Data Redis简介

2. Spring Data Redis相关jar包

2.1. Spring相关jar包

 

2.2. Spring Data Redis相关jar包 

2.3. Json相关jar包 

3. Spring Data Redis案例

3.1. 新建一个名为spring-data-redis的Java项目, 同时添加相关jar包。

 

3.2. 添加Junit

3.2.1. 项目上右键——>Build Path——>Configure Build Path...

 

3.2.2. 点击Add Library... 

3.2.3. 选择JUnit——>Next——>Apply and Close 

3.2.4. JUnit能力 

3.3. 在src下新建application.properties 

spring.redis.pool.max-idle=10 spring.redis.pool.min-idle=5 spring.redis.pool.max-total=20spring.redis.hostName=192.168.25.138 spring.redis.port=6379 spring.redis.password=lyw123456

3.4. 在src下新建applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置读取properties文件的工具类 --><context:property-placeholder location="classpath:application.properties"/><!-- Jedis连接池 --><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxTotal" value="${spring.redis.pool.max-total}"/><property name="maxIdle" value="${spring.redis.pool.max-idle}"/><property name="minIdle" value="${spring.redis.pool.min-idle}"/></bean><!-- Jedis连接工厂: 创建Jedis对象的工厂 --><bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><!-- IP地址 --><property name="hostName" value="${spring.redis.hostName}"/><!-- 端口 --><property name="port" value="${spring.redis.port}"/><!-- 端口 --><property name="password" value="${spring.redis.password}"/><!-- 连接池 --><property name="poolConfig" ref="poolConfig"/></bean><!-- Redis模板对象:是SpringDataRedis提供的用户操作Redis的对象 --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory"/><!-- 默认的序列化器: 序列化器就是根据规则将存储的数据中的key与value做字符串的序列化处理 --><!-- keySerializer、valueSerializer: 对应的是Redis中的String类型 --><!-- hashKeySerializer、hashValueSerializer: 对应的是Redis中的Hash类型 --><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean></property></bean> </beans>

3.5. 新建User.java

package com.bjbs.pojo;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String name;private Integer age;public User() {}public User(Integer id, String name, Integer age) {super();this.id = id;this.name = name;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", age=" + age + "]";} }

3.6. 新建RedisTest.java

package com.bjbs.test;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.bjbs.pojo.User;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class RedisTest {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;/*** 添加一个字符串*/@Testpublic void setStr(){redisTemplate.opsForValue().set("spring-data-redis", "SpringData Redis存储字符串");}/*** 获取一个字符串*/@Testpublic void getStr(){String str = (String)redisTemplate.opsForValue().get("spring-data-redis");System.out.println(str);}/*** 删除一个值*/@Testpublic void delValue(){redisTemplate.delete("user_json");}/*** 添加User对象, 使用JDK的序列化器*/@Testpublic void setUesr() {User user = new User();user.setAge(20);user.setName("张三丰");user.setId(1);// 重新设置序列化器redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());redisTemplate.opsForValue().set("user", user);}/*** 取User对象, 使用JDK的序列化器*/@Testpublic void getUser() {// 重新设置序列化器redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());User users = (User) redisTemplate.opsForValue().get("user");System.out.println(users);}/*** 基于JSON格式存User对象*/@Testpublic void setUseJSON() {User user = new User();user.setAge(20);user.setName("李四丰");user.setId(1);redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));redisTemplate.opsForValue().set("user_json", user);}/*** 基于JSON格式取User对象*/@Testpublic void getUseJSON() {redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));User user = (User) this.redisTemplate.opsForValue().get("user_json");System.out.println(user);} }

3.7. 启动服务器上的Redis, 选中setStr方法, 右键——>Run As——>JUnit Test

3.8. 选中getStr方法, 右键——>Run As——>JUnit Test 

3.9. 选中setUser方法, 右键——>Run As——>JUnit Test 

3.10. 选中getUser方法, 右键——>Run As——>JUnit Test

 3.11. 选中setUserJSON方法, 右键——>Run As——>JUnit Test 

3.12. 选中getUserJSON方法, 右键——>Run As——>JUnit Test

总结

以上是生活随笔为你收集整理的012_Spring Data Redis的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。