springboot+redisTemplate多库操作

单库操作
  • 我做了依赖管理,所以就不写版本号了
  • 添加依赖
        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
  • 配置文件
spring:redis:database: 2host: 127.0.0.1port: 6379password: 123456
  • redisTemplate配置
    /*** @author liouwb*/@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);JavaTimeModule javaTimeModule = new JavaTimeModule();DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS.getValue());javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));om.registerModule(javaTimeModule);jackson2JsonRedisSerializer.setObjectMapper(om);RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(jackson2JsonRedisSerializer);template.setHashKeySerializer(jackson2JsonRedisSerializer);template.setHashValueSerializer(jackson2JsonRedisSerializer);template.setDefaultSerializer(new StringRedisSerializer());template.afterPropertiesSet();return template;}
  • 使用
    @Autowired(required = false)private RedisTemplate<String, Object> redisTemplate;@Testpublic void testRedis() {redisTemplate.opsForValue().set("redisTemplate", "redisTemplate");Object value = redisTemplate.opsForValue().get("redisTemplate");System.out.println("redis设置的值为:" + value);}

在这里插入图片描述

  • 能够正常往redis设置数据,也可以正常获取到
    在这里插入图片描述
  • 可以看到数据库编号是yml里面配置的database编号
多库操作
  • 使用lettuce连接池,添加commons-pools依赖
        <dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>
  • 配置类
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;/*** redis数据库配置** @author liouwb*/
@Data
@Configuration
public class RedisConfigProperties {@Value("${spring.redis.host}")private String host;@Value("${spring.redis.port}")private int port;@Value("${spring.redis.password}")private String password;/*** 连接超时时间* 目前不从配置文件获取*/private int timeout = 200;private int maxActive = 200;private int maxIdle = 8;private int minIdle = 0;private int maxWait = 100;
}
  • 配置连接池
    /*** redis连接池** @author liouwb* @rutern org.apache.commons.pool2.impl.GenericObjectPoolConfig*/@Beanpublic GenericObjectPoolConfig getPoolConfig() {GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();poolConfig.setMaxTotal(redisConfigProperties.getMaxActive());poolConfig.setMaxIdle(redisConfigProperties.getMaxIdle());poolConfig.setMinIdle(redisConfigProperties.getMinIdle());poolConfig.setMaxWaitMillis(redisConfigProperties.getMaxWait());return poolConfig;}
  • 设置redisTemplate操作模版工厂类
    /*** RedisTemplate连接工厂** @param database数据库编号* @author liouwb* @rutern org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.Object>*/private RedisTemplate<String, Object> redisTemplateFactory(int database) {// 构建工厂对象RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();configuration.setHostName(redisConfigProperties.getHost());configuration.setPort(redisConfigProperties.getPort());configuration.setPassword(RedisPassword.of(redisConfigProperties.getPassword()));LettucePoolingClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofSeconds(redisConfigProperties.getTimeout())).poolConfig(getPoolConfig()).build();// 连接工厂LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration, clientConfiguration);// 设置使用的redis数据库factory.setDatabase(database);// 重新初始化工厂factory.afterPropertiesSet();// 序列化配置Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);JavaTimeModule javaTimeModule = new JavaTimeModule();DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS.getValue());javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));om.registerModule(javaTimeModule);jackson2JsonRedisSerializer.setObjectMapper(om);// 初始化连接RedisTemplate<String, Object> template = new RedisTemplate<>();// 设置连接工厂template.setConnectionFactory(factory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(jackson2JsonRedisSerializer);template.setHashKeySerializer(jackson2JsonRedisSerializer);template.setHashValueSerializer(jackson2JsonRedisSerializer);template.setDefaultSerializer(new StringRedisSerializer());template.afterPropertiesSet();return template;}
  • 设置对应数据库的redisTemplate模版类
    /*** db0 第一个库** @author liouwb* @time 2024-01-03* @rutern org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.Object>*/@Beanpublic RedisTemplate<String, Object> redisTemplate0() {return this.redisTemplateFactory(0);}/*** db1 第二个库** @author liouwb* @rutern org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.Object>*/@Beanpublic RedisTemplate<String, Object> redisTemplate1() {return this.redisTemplateFactory(1);}
  • 对不同redis库进行操作
    @Autowired(required = false)@Qualifier("redisTemplate0")private RedisTemplate<String, Object> redisTemplate0;@Autowired(required = false)@Qualifier("redisTemplate1")private RedisTemplate<String, Object> redisTemplate1;@Testpublic void testRedis() {redisTemplate0.opsForValue().set("redisTemplate0", "redisTemplate0");Object value0 = redisTemplate0.opsForValue().get("redisTemplate0");System.out.println("redis0设置的值为:" + value);redisTemplate1.opsForValue().set("redisTemplate1", "redisTemplate1");Object value1 = redisTemplate0.opsForValue().get("redisTemplate1");System.out.println("redis1设置的值为:" + value1);}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/594601.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Git 概念与基础命令

Git 是一个版本控制系统&#xff0c;用于记录文件或代码的修改历史&#xff0c;并且可以通过多个分支进行协作开发。 Git 的基本概念包括&#xff1a; 仓库&#xff08;Repository&#xff09;&#xff1a;包含所有文件和历史记录的地方&#xff0c;可以在本地或远程服务器上存…

勒索检测能力升级,亚信安全发布《勒索家族和勒索事件监控报告》

评论员简评 近期(12.08-12.14)共发生勒索事件119起&#xff0c;相较之前呈现持平趋势。 与上周相比&#xff0c;本周仍然流行的勒索家族为lockbit3和8base。在涉及的勒索家族中&#xff0c;活跃程度Top5的勒索家族分别是&#xff1a;lockbit3、siegedsec、dragonforce、8base和…

确保数据安全性与系统稳定性:在Spring Boot中实现API幂等性的完整指南

当在Spring Boot中构建应用程序时&#xff0c;处理重复提交和确保幂等性是至关重要的。幂等性的概念是指无论客户端发送的请求次数&#xff0c;系统状态都保持一致。在API设计中实现幂等性可以防止重复操作&#xff0c;避免意外的数据修改或损坏。 实现幂等性保护API 在开发W…

MatrixOne 1.1.0 Release

我们非常高兴地宣布&#xff1a; MatrixOne内核1.1.0版本 正式发布啦&#xff01; 项目文档网站 https://docs.matrixorigin.cn MatrixOne是一款分布式超融合异构数据库&#xff0c;MatrixOne旨在提供一个云原生、高性能、高弹性、高度兼容MySQL的HSTAP数据库&#xff0c;让…

Oracle database 12cRAC异地恢复至单机

环境 rac 环境 byoradbrac Oracle12.1.0.2 系统版本&#xff1a;Red Hat Enterprise Linux Server release 6.5 软件版本&#xff1a;Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit byoradb1&#xff1a;172.17.38.44 byoradb2&#xff1a;172.17.38.4…

基于遗传算法的格栅路径优化,遗传算法的基本原理

目录 背影 遗传算法的原理及步骤 基本定义 编码方式 适应度函数 运算过程 代码 结果分析 完整代码下载: https://download.csdn.net/download/abc991835105/88691336 背影 基于遗传算法的格栅路径优化,求解运算量大,一般都无法用直接求解,本文用遗传算法进行求解,遗传算…

NCC开发记录

YonBuilder for NCC 是一个带插件的eclipse工具&#xff0c;跟eclipse没什么区别 NC Cloud2021.11版本开发环境搭建改动 https://nccdev.yonyou.com/article/detail/495 不管是NC Cloud 新手还是老NC开发&#xff0c;在开发NC Cloud时开发环境搭建必看&#xff01;&#xff…

工具篇--Spring-Cloud--feign 通过feign 接口完成文件的下载

文章目录 前言一、feign接口获取文件流程&#xff1a;二、文件获取实现2.1 引入jar&#xff1a;2.2 实现&#xff1a; 总结 前言 通常在spring-boot 项目中&#xff0c;对于文件的下载都是直接调用到对应的服务中&#xff0c;而不是通过feign 接口获取文件&#xff1b;有时我们…

使用 Process Explorer 和 Windbg 排查软件线程堵塞案例分享

目录 1、问题说明 2、线程堵塞的可能原因分析 3、使用Windbg和Process Explorer确定线程中发生了死循环 4、根据Windbg中显示的函数调用堆栈去查看源码&#xff0c;找到问题 4.1、在Windbg定位发生死循环的函数的方法 4.2、在Windbg中查看变量的值去辅助分析 4.3、是循环…

HubSpot电子邮件自动化的关键功能和流程!

HubSpot提供了强大的电子邮件自动化工具&#xff0c;使用户能够创建、执行和跟踪复杂的电子邮件市场营销活动。以下是HubSpot电子邮件自动化的一些关键功能和流程&#xff1a; 1.电子邮件工作流程&#xff08;Email Workflows&#xff09;&#xff1a; 用户可以使用HubSpot的工…

机器学习笔记 - 偏最小二乘回归 (PLSR)

一、偏最小二乘回归:简介 PLS 方法构成了一个非常大的方法族。虽然回归方法可能是最流行的 PLS 技术,但它绝不是唯一的一种。即使在 PLSR 中,也有多种不同的算法可以获得解决方案。PLS 回归主要由斯堪的纳维亚化学计量学家 Svante Wold 和 Harald Martens 在 20 世纪 80 年代…

在vscode中写C# 教程

在 Visual Studio Code 中编写 C# 代码 安装 .NET SDK&#xff1a;访问 https://dotnet.microsoft.com/download/dotnet 来下载和安装最新的 .NET SDK。根据你的操作系统选择下载版本&#xff0c;并按照安装向导进行操作。安装完成后&#xff0c;你将能在命令行中使用 dotnet 命…

【零基础入门TypeScript】判断条件和循环

目录 定环 无限循环 示例&#xff1a;while 与 do..while 中断语句 句法 流程图 例子 继续语句 句法 流程图 例子 输出 无限循环 语法&#xff1a;使用 for 循环的无限循环 示例&#xff1a;使用 for 循环的无限循环 语法&#xff1a;使用 while 循环进行无限循…

LeetCode-轮转数组的三种方法(189)

题目描述&#xff1a; 给定一个整数数组 nums&#xff0c;将数组中的元素向右轮转 k 个位置&#xff0c;其中 k 是非负数。 思路一&#xff1a; 建立一个两倍原数组长度的数组&#xff0c;然后其中保存两遍原数组中的元素&#xff0c;轮转的过程就可以看成是在这个新数组中截…

mac电脑配置本地连接开发机器一键打包部署

mac电脑 安装homebrew&#xff08;已安装请跳过&#xff09; /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"安装rsync同步工具 brew install rsync配置服务器免密 生成公/私钥&#xff08;生成过的请跳过&a…

云计算:OpenStack 分布式架构管理VXLAN网络(单控制节点与多计算节点)

目录 一、实验 1.环境 2.各节点新增网卡准备VXLAN网络 3.控制节点配置私有网络 4.计算节点1配置私有网络 5.计算节点2配置私有网络 6.重启服务 7.修改Dashboard 8.新建项目&#xff08;租户&#xff09;及用户 9.新建网络与子网 10.新建实例 11.新建路由 12.新增浮…

物联网的网络管理技术开发

物联网并不是新的事物。不论称为物联网或者是传感网&#xff0c;物联网的基本组成可以看成为传感器网络接入互联网构成,当然也有仅仅是传感器网络组成的简单的物联网系统。但是总的来说,物联网有许多新的特点。这些特点导致物联网对于其网络的管理有新的要求。因此电信网和互联…

算法与数据结构之数组(Java)

目录 1、数组的定义 2、线性结构与非线性结构 3、数组的表现形式 3.1 一维数组 3.2 多维数组 4、重要特性&#xff1a;随机访问 5、ArrayList和数组 6、堆内存和栈内存 7、数组的增删查改 7.1 插入数据 7.2 删除一个数据 7.3 修改数组 7.4 查找数据 8、总结 什么…

面试数据库八股文十问十答第五期

面试数据库八股文十问十答第五期 作者&#xff1a;程序员小白条&#xff0c;个人博客 1&#xff09;介绍一下 MySQL8 的新特性 Window Functions&#xff1a; 提供了对查询结果进行窗口化处理的功能&#xff0c;例如使用 ROW_NUMBER() 进行分页。Common Table Expressions (CT…

MongoDB聚合:$facet

对输入的文档执行多个聚合管道&#xff0c;在输出结果中&#xff0c;每个子管道一个字段&#xff0c;字段值是一个文档数组。 $facet可以在一个阶段创建多面聚合&#xff0c;横跨多个维度或方面来描述数据特征。多面聚合可提供多个过滤器和分类指导数据浏览和分析。 $facet 阶…