
spring保存对象到Redis的简单栗子
Spring Context:用于管理Spring容器。 Jedis:Redis的Java客户端。 Spring Data Redis:Spring对Redis的高级抽象层,提供更简单的操作接口。
发布日期:2021-05-10 02:13:54
浏览次数:15
分类:精选文章
本文共 3485 字,大约阅读时间需要 11 分钟。
Redis最佳的NoSQL数据库之一,学习它的价值不言而喻。
在之前的SSM项目中,Redis被用于缓存以提升访问效率。 现在正式入手Redis,希望通过实践总结一些学习经验。以下将从配置、实体类设计、Spring配置以及测试案例等方面,为Redis学习打下基础。
Redis与Spring的配置
为实现Redis与Spring的集成,需要依赖以下工具包:
pom.xml配置示例如下:
org.springframework spring-context 5.0.8.RELEASE redis.clients jedis 2.9.0 org.springframework.data spring-data-redis 2.0.6.RELEASE
实体类设计:Role
在实际应用中,Role实体类通常用于管理用户权限。以下是简单的Java实体类定义:
import java.io.Serializable; public class Role implements Serializable { private static final long serialVersionUID = -5880680723667053957L; private long id; private String roleName; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } }
Spring配置文件解读
配置文件的核心部分为RedisTemplate,它负责连接工厂与序列化器的配置。以下是需要关注的关键配置:
测试类1:简单操作
以下是一个简单的测试类,演示RedisTemplate的基本操作:
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.redis.core.RedisTemplate; public class test { @Test public void xx(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class); Role role = new Role(); role.setId(12); role.setRoleName("sd"); redisTemplate.opsForValue().set("na1",role); Role role1 = (Role) redisTemplate.opsForValue().get("na1"); System.out.print(role1.getId()); } }
测试类2:确保同一连接池执行
为了减少Redis连接开销,建议在同一连接池内执行Redis操作。以下是一个实现细节:
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.SessionCallback; public class test { @Test public void xx(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class); final Role role = new Role(); role.setId(12); role.setRoleName("sd"); SessionCallback sessionCallback = new SessionCallback() { @Override public Object execute(RedisOperations redisOperations) throws DataAccessException { redisOperations.boundValueOps("role2").set(role); return (Role) redisOperations.boundValueOps("role2").get(); } }; Role role1 = (Role) redisTemplate.execute(sessionCallback); System.out.print(role1.getId()); } }
总结
通过以上配置与测试,我们可以更好地理解Spring与Redis的集成流程。
在实际开发中,建议根据应用需求灵活配置Redis来源(例如,是否启用持久化,或利用Redis扩展功能如 Redis模板)。发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月16日 03时06分16秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
《STM32从零开始学习历程》——CAN相关结构体
2019-03-07
Dubbo笔记 ② : 架构概述
2019-03-07
ROS参数服务器
2019-03-07
malloc分配0个字节
2019-03-07
new与delete细节探索
2019-03-07
vim配置
2019-03-07
原生Javascript实现New方法
2019-03-07
Promise串行执行
2019-03-07
CSS三栏布局问题
2019-03-07
js数据类型检测
2019-03-07
winform关闭窗口 取消关闭操作
2019-03-07
解决VS2012的 未能将网站配置为使用ASP.NET4.5问题
2019-03-07
mysql权限问题
2019-03-07
Tomcat中jdk版本与项目版本不一致造成404错误以及Eclipse修改jdk版本
2019-03-07
Spring通过工厂方法配置Bean
2019-03-07
Spring事务的两种常用传播方式
2019-03-07
配置SpringMVC中的视图解析器
2019-03-07
Redis6大基础数据结构以及在spring中的常用命令
2019-03-07
【Unity3D】Scene窗口看不见任何物体问题
2019-03-07