Spring boot集成Redis(2)—RedisTemplate的使用来存储Map集合
发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,前言:上一篇文章我们用的是StringRedisTemplate,但是它存在一点问题,也迫使我重新写了代码,问题是:在我们往缓存中存入数字形式的String类型时,我们在利用Spring could将
千家信息网最后更新 2025年02月03日Spring boot集成Redis(2)—RedisTemplate的使用来存储Map集合
前言:上一篇文章我们用的是StringRedisTemplate,但是它存在一点问题,也迫使我重新写了代码,问题是:在我们往缓存中存入数字形式的String类型时,我们在利用Spring could将获取到的数据发送到另一服务时,我们发现数据已经被强转为Integer类型了,因为我们可能传输的数据庞大,类型多样,为了统一类型,以及开发方便,所以我将缓存改成RedisTemplate这种类型,进行增删改查的操作,文中没有特别举例更新操作,其更新操作与添加操作一样,当key一样时进行添加就会覆盖原value值,完成更新。RedisTemplate需要我们自己去配置它并进行实例化。接下来,举例子,上代码:
首先建立Spring boot项目添加Redis依赖
下载导入IDE,我们观察pom.xml文件:
4.0.0 com.test redis 0.0.1-SNAPSHOT jar redis Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.springframework.boot spring-boot-maven-plugin
1.配置application.properties
#redisspring.redis.host=主机地址spring.redis.password=adminspring.redis.port=6379spring.redis.timeout=10000spring.redis.jedis.pool.max-idle=200 spring.redis.jedis.pool.min-idle=300000 spring.redis.jedis.pool.max-active=400spring.redis.jedis.pool.max-wait=10000
2.我们写配置配置类实例化RedisTemplate
package com.test.redis.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import org.springframework.data.redis.connection.RedisConnectionFactory;@Configurationpublic class RedisConfig { /** * 实例化 RedisTemplate 对象 * * @return */ @Bean public RedisTemplate functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate redisTemplate = new RedisTemplate<>(); initDomainRedisTemplate(redisTemplate, redisConnectionFactory); return redisTemplate; } /** * 设置数据存入 redis 的序列化方式,并开启事务 * * @param redisTemplate * @param factory */ private void initDomainRedisTemplate(RedisTemplate redisTemplate, RedisConnectionFactory factory) { // 如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to // String! redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // 开启事务 redisTemplate.setEnableTransactionSupport(true); redisTemplate.setConnectionFactory(factory); }}
3.写缓存操作的Service层,进行增删改查方法的定义:
package cn.com.dhcc.idatabus.admin.console.service;import java.util.List;import java.util.Map;import javax.annotation.Resource;import org.springframework.data.redis.core.HashOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;import org.springframework.stereotype.Service;@Servicepublic class RedisService { @Resource private RedisTemplate template; /** * 存储数据或修改数据 * * @param modelMap * @param mapName */ public void setKey(String mapName, Map modelMap) { HashOperations hps = template.opsForHash(); hps.putAll(mapName, modelMap); } /** * 获取数据Map * * @param mapName * @return */ public Map getMapValue(String mapName) { HashOperations hps = this.template.opsForHash(); return hps.entries(mapName); } /** * 获取数据value * * @param mapName * @param hashKey * @return */ public Object getValue(String mapName, String hashKey) { HashOperations hps = this.template.opsForHash(); return hps.get(mapName, hashKey); } /** * 批量删除缓存数据 * * @param keys */ public void deleteData(List keys) { // 执行批量删除操作时先序列化template template.setKeySerializer(new JdkSerializationRedisSerializer()); template.delete(keys); }}
4.本次例子的实体类
package com.test.redis.entity;public class User { private Integer id; private String name; private String password; public User() { super(); } public User(Integer id, String name, String password) { super(); this.id = id; this.name = name; this.password = password; } 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 String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; }}
5.编写Controller层,来实现缓存的操作
package com.test.redis.web;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.test.redis.entity.User;import com.test.redis.service.RedisService;@Controllerpublic class UserController { private static final String mapName="mapName"; @Autowired private RedisService redisService; @GetMapping( "/templateAdd.do") @ResponseBody public Map addUser(HttpServletRequest request){ Map modelMap=new HashMap(); User user=new User(); user.setName("hehename"); user.setPassword("hehePassword"); //存放hash值 modelMap.put("name", user.getName()); modelMap.put("password", user.getPassword()); redisService.setKey(mapName, modelMap); //获取map集合 Map modelMap1= redisService.getMapValue(mapName); Object value= redisService.getValue(mapName, "name"); System.out.println(" value : "+value); modelMap1.put("从缓存中根据key取到的value", value); return modelMap1; } @GetMapping( "/templateDelete.do") @ResponseBody public Map deleteUser(HttpServletRequest request){ //获取即将删除的key值,这里我们做的批量删除 List keys=new ArrayList<>(); keys.add("heheanme"); //开始执行删除操作 redisService.deleteData(keys); //获取map集合 Map modelMap1= redisService.getMapValue(mapName); Object value= redisService.getValue(mapName, "name"); System.out.println(" value : "+value); modelMap1.put("从缓存中根据key取到的value", value); return modelMap1; }}
接下来,我们访问Controller路径
(1)http://localhost:8081/templateAdd.do
结果:
(2)http://localhost:8081/templateDelete.do
结果:
数据
缓存
类型
配置
存储
实例
更新
接下来
事务
例子
序列
结果
问题
UTF-8
上代
主机
代码
前言
地址
实体
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全产品合作伙伴大会致辞
临沂嵌入式软件开发
app影视软件开发
下拉选项隐藏源数据库
服务器bmc监控管理功能
qt 连不上数据库
佩鸿软件开发
足球数据库采集
进口网络安全设备
监控行业需要服务器吗
有线电视如何链接到服务器
航天金穗软件开发商
奉贤区上门软件开发代理品牌
服务器 密码机 开源
解密微信数据库
杭州紫光网络技术公司
多功能网络技术哪家强
数据库课件 sql
软件开发有哪些关联行业
服务好的app软件开发
上海互联网金融软件开发
绿盟网络安全架构师
仇小玲科技互联网
网络模式和网络安全模式
传祺GS4内外循环伺服务器
数据库看表的数据
安徽锋视网络技术
安徽hp服务器续保哪家便宜
如何用jdbc连接数据库
怎样查看app 服务器在哪儿