mybatis-plus3.5.3.1 支持不同数据源sql适配

mybatis-plus3.5.3.1 支持不同数据源sql适配

背景

最近公司要求支持国产数据库达梦,人大金仓,高斯等数据库,这些数据库与mysql的语法有一些差异,需要做一些兼容操作。

解决问题

1.不同数据库分页不同

2.支持通过参数控制执行不同的sql

3.没有特殊sql执行默认sql

实现流程

1.代码结构

在这里插入图片描述

2.引入依赖

 <!-- Tag-单元测试 junit--><!-- Tag-mybatis plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.1</version></dependency><!-- Tag-mybatis plus--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version></dependency><!-- https://mvnrepository.com/artifact/org.opengauss/opengauss-jdbc --><dependency><groupId>org.opengauss</groupId><artifactId>opengauss-jdbc</artifactId><version>3.0.0</version></dependency><!--kingbase 最低可支持JDK1.8 --><dependency><groupId>cn.com.kingbase</groupId><artifactId>kingbase8</artifactId><version>8.6.0</version></dependency><!--  达梦--><dependency><groupId>com.dameng</groupId><artifactId>DmJdbcDriver18</artifactId><version>8.1.1.193</version></dependency><dependency><groupId>com.oracle.database.jdbc</groupId><artifactId>ojdbc8-production</artifactId><version>19.7.0.0</version></dependency>

3.代码

MybatisAutoConfiguration
@Configuration
@MapperScan("com.liuhm.dao.mapper*")
public class MybatisAutoConfiguration {@Autowiredprivate DataSource dataSource;@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();String driverClassName = ((HikariDataSource) dataSource).getDriverClassName();// 分页插件mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DatabaseIdEnums.getDbTypeByDriver(driverClassName)));return mybatisPlusInterceptor;}@Beanpublic DatabaseIdProvider databaseIdProvider() {VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();Properties properties = new Properties();// 设置数据库厂商和databaseid别名for (DatabaseIdEnums databaseIdEnum : DatabaseIdEnums.values()) {// key=value// key     数据库厂商的databaseid// value   别名,和xml里面对应properties.put(databaseIdEnum.getName(),databaseIdEnum.getName());}databaseIdProvider.setProperties(properties);return databaseIdProvider;}
}
TestMapper
@Repository
@CacheNamespace
public interface TestMapper {/*** 需要兼容* @return*/public List<String> select();/*** 公共的方法 不兼容* @return*/public List<String> selectCommon();/*** 测试分页* @param page* @return*/IPage<String> selectPage(Page<String> page);
}
DatabaseIdEnums
@AllArgsConstructor
@Getter
public enum DatabaseIdEnums {MYSQL("MySQL", "mysql", "com.mysql.jdbc.Driver",DbType.MYSQL),KINGBASEESV8("KingbaseES","kingbaseesv8","com.kingbase8.Driver", com.baomidou.mybatisplus.annotation.DbType.KINGBASE_ES),OPENGAUSS3("PostgreSQL", "opengauss3"," org.opengauss.Driver",DbType.OPENGAUSS),ORACLE("Oracle", "oracle","oracle.jdbc.driver.OracleDriver",DbType.ORACLE),DM("DM DBMS", "dm","dm.jdbc.driver.DmDriver",DbType.DM);/*** 数据库名*/private String name;/*** 数据库名 别名*/private String nameAlias;/*** 数据库名 别名*/private String driver;/*** 分页插件名*/private DbType dbType;/*** 通过数据名获取对应的分页插件* @param driver* @return*/public static DbType getDbTypeByDriver(String driver) {for (DatabaseIdEnums databaseIdEnum : DatabaseIdEnums.values()) {if(Objects.equals(databaseIdEnum.getDriver(),driver)){return databaseIdEnum.dbType;}}return null;}
}
TestMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liuhm.dao.mapper.TestMapper"><cache-ref namespace="com.liuhm.dao.mapper.TestMapper"/><select id="selectCommon" resultType="java.lang.String">SELECT * FROM test;</select><select id="selectPage" resultType="java.lang.String">select * from test</select><select id="select" resultType="java.lang.String" databaseId="mysql">select * from test where 'mysql' = 'mysql'</select><select id="select" resultType="java.lang.String" databaseId="kingbaseesv8">select * from test where 'kingbaseesv8' = 'kingbaseesv8'</select><select id="select" resultType="java.lang.String" databaseId="opengauss3">select * from test where 'opengauss3' = 'opengauss3'</select><select id="select" resultType="java.lang.String" databaseId="oracle">select * from test where 'oracle' = 'oracle'</select><select id="select" resultType="java.lang.String" databaseId="dm">select * from test where 'dm' = 'dm'</select></mapper>

4.测试

测试要求

  • 对应数据库导入测试sql

  • 测试公共的方法是否正确

  • 分页测试是否正确

  • 特殊兼容sql测试是否正确

测试代码:

    @Testpublic void Test(){testMapper.selectCommon();System.out.println("------------------------------");testMapper.selectPage(new Page<>(1,5));System.out.println("------------------------------");testMapper.selectPage(new Page<>(2,5));System.out.println("------------------------------");testMapper.select();}
4.1.mysql测试

使用yml配置

    driver-class-name: com.mysql.jdbc.Driverusername: rootpassword: Cobbler1234!url: jdbc:mysql://192.168.0.229:43306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false

运行结果

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5a05dd30] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1590202270 wrapping com.mysql.jdbc.JDBC4Connection@7a04f730] will not be managed by Spring
==>  Preparing: SELECT * FROM test;
==> Parameters: 
<==    Columns: name
<==        Row: 测试数据mysql 1
<==        Row: 测试数据mysql 2
<==        Row: 测试数据mysql 3
<==        Row: 测试数据mysql 4
<==        Row: 测试数据mysql 5
<==        Row: 测试数据mysql 6
<==        Row: 测试数据mysql 7
<==      Total: 7
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5a05dd30]
------------------------------
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@353e6389] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@11646065 wrapping com.mysql.jdbc.JDBC4Connection@7a04f730] will not be managed by Spring
==>  Preparing: SELECT COUNT(*) AS total FROM test
==> Parameters: 
<==    Columns: total
<==        Row: 7
<==      Total: 1
==>  Preparing: select * from test LIMIT ?
==> Parameters: 5(Long)
<==    Columns: name
<==        Row: 测试数据mysql 1
<==        Row: 测试数据mysql 2
<==        Row: 测试数据mysql 3
<==        Row: 测试数据mysql 4
<==        Row: 测试数据mysql 5
<==      Total: 5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@353e6389]
------------------------------
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62cbc478] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1413020227 wrapping com.mysql.jdbc.JDBC4Connection@7a04f730] will not be managed by Spring
==>  Preparing: SELECT COUNT(*) AS total FROM test
==> Parameters: 
<==    Columns: total
<==        Row: 7
<==      Total: 1
==>  Preparing: select * from test LIMIT ?,?
==> Parameters: 5(Long), 5(Long)
<==    Columns: name
<==        Row: 测试数据mysql 6
<==        Row: 测试数据mysql 7
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62cbc478]
------------------------------
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@615db358] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@627815870 wrapping com.mysql.jdbc.JDBC4Connection@7a04f730] will not be managed by Spring
==>  Preparing: select * from test where 'mysql' = 'mysql'
==> Parameters: 
<==    Columns: name
<==        Row: 测试数据mysql 1
<==        Row: 测试数据mysql 2
<==        Row: 测试数据mysql 3
<==        Row: 测试数据mysql 4
<==        Row: 测试数据mysql 5
<==        Row: 测试数据mysql 6
<==        Row: 测试数据mysql 7
<==      Total: 7
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@615db358]
4.2.kingbaseesv8测试

使用yml配置

    driver-class-name: com.kingbase8.Driverusername: systempassword: hcloud1234url: jdbc:kingbase8://192.168.0.248:54321/kingbase?currentSchema=mcp_manager

运行结果

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1a2ac487] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@629092599 wrapping com.kingbase8.jdbc.KbConnection@3fde2209] will not be managed by Spring
==>  Preparing: SELECT * FROM test;
==> Parameters: 
<==    Columns: name
<==        Row: 测试数据kingbaseesv8 1
<==        Row: 测试数据kingbaseesv8 2
<==        Row: 测试数据kingbaseesv8 3
<==        Row: 测试数据kingbaseesv8 4
<==        Row: 测试数据kingbaseesv8 5
<==        Row: 测试数据kingbaseesv8 6
<==        Row: 测试数据kingbaseesv8 7
<==      Total: 7
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1a2ac487]
------------------------------
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@496cc217] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@936544568 wrapping com.kingbase8.jdbc.KbConnection@3fde2209] will not be managed by Spring
==>  Preparing: SELECT COUNT(*) AS total FROM test
==> Parameters: 
<==    Columns: total
<==        Row: 7
<==      Total: 1
==>  Preparing: select * from test LIMIT ?
==> Parameters: 5(Long)
<==    Columns: name
<==        Row: 测试数据kingbaseesv8 1
<==        Row: 测试数据kingbaseesv8 2
<==        Row: 测试数据kingbaseesv8 3
<==        Row: 测试数据kingbaseesv8 4
<==        Row: 测试数据kingbaseesv8 5
<==      Total: 5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@496cc217]
------------------------------
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@723e2d08] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@7162498 wrapping com.kingbase8.jdbc.KbConnection@3fde2209] will not be managed by Spring
==>  Preparing: SELECT COUNT(*) AS total FROM test
==> Parameters: 
<==    Columns: total
<==        Row: 7
<==      Total: 1
==>  Preparing: select * from test LIMIT ? OFFSET ?
==> Parameters: 5(Long), 5(Long)
<==    Columns: name
<==        Row: 测试数据kingbaseesv8 6
<==        Row: 测试数据kingbaseesv8 7
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@723e2d08]
------------------------------
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@79df80a4] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@319618119 wrapping com.kingbase8.jdbc.KbConnection@3fde2209] will not be managed by Spring
==>  Preparing: select * from test where 'kingbaseesv8' = 'kingbaseesv8'
==> Parameters: 
<==    Columns: name
<==        Row: 测试数据kingbaseesv8 1
<==        Row: 测试数据kingbaseesv8 2
<==        Row: 测试数据kingbaseesv8 3
<==        Row: 测试数据kingbaseesv8 4
<==        Row: 测试数据kingbaseesv8 5
<==        Row: 测试数据kingbaseesv8 6
<==        Row: 测试数据kingbaseesv8 7
<==      Total: 7
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@79df80a4]
4.3.其余数据库自测

5.注意

5.1.查找对应的数据库名

通过查询 java.sql.DatabaseMetaData接口中getDatabaseProductName方法,找到对应是实现类

在这里插入图片描述

在这里插入图片描述

5.2.databaseId对应的是设置的别名
    @Beanpublic DatabaseIdProvider databaseIdProvider() {VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();Properties properties = new Properties();properties.put("Oracle","oracle");properties.put("MySQL","mysql");databaseIdProvider.setProperties(properties);return databaseIdProvider;}

xml中

    <select id="select" resultType="java.lang.String" databaseId="mysql">select * from test where 'mysql' = 'mysql'</select><select id="select" resultType="java.lang.String" databaseId="oracle">select * from test where 'oracle' = 'oracle'</select>
5.3.没有设置databaseId代表该xmlsql全部数据库都通用
5.4.分页插件设置

通过定义的驱动找到对应的DbType

	mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DatabaseIdEnums.getDbTypeByDriver(driverClassName)));

博客地址

代码下载

下面的mybatis-plus-demo3_5_3_1

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

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

相关文章

电脑怎么备份文件?简单几步,轻松备份!

电脑中存储着大量的个人和工作文件&#xff0c;包括照片、文档、音乐和视频等。但突发状况&#xff0c;如硬件故障、病毒感染或误删文件&#xff0c;可能会导致数据丢失。因此&#xff0c;备份文件至关重要。在本文中&#xff0c;我们将介绍三种电脑怎么备份文件的方法&#xf…

CleanMyMac X2024免费苹果笔记本清理内存清理工具

CleanMyMac X的优点包括&#xff1a; 全面的系统清理&#xff1a;CleanMyMac X可以扫描和清理系统中的垃圾文件、缓存文件、无用的语言文件、残留文件等&#xff0c;从而释放磁盘空间&#xff0c;提高系统性能。 优化工具&#xff1a;该软件还提供了一系列优化工具&#xff0c…

Freeswitch中CHANNEL_HANGUP外呼挂断事件

1.CHANNEL_HANGUP外呼挂断事件 事件详细 ################## 外呼挂断&#xff1a;############################# [EslMessage{contentTypecommand/reply, headers3, body0 lines}] 2023-11-16T03:41:33.5140800 INFO c.e.c.v.s.c.i.FsServerEventHandler - eventReceived:…

成都瀚网科技有限公司抖音带货是否靠谱

成都瀚网科技有限公司&#xff0c;作为一家专业的科技公司&#xff0c;近年来积极参与了抖音带货的浪潮&#xff0c;凭借其出色的产品和服务&#xff0c;赢得了广大用户的信赖。 在当今的数字化时代&#xff0c;抖音带货已经成为了一种非常流行的购物方式。消费者可以通过抖音平…

出行类app如何提升广告变现收益?

出行类APP已经成为越来越多人们出行的首选&#xff0c;出行类app在变现方式上存在以下痛点&#xff1a;APP功能单一、使用场景单一&#xff1b;用户使用时间集中&#xff0c;粘性低...这些痛点使得开发者获取收益的提升面临极大的挑战。 https://www.shenshiads.com 如何让出…

leetcode算法之前缀和

目录 1.DP34[模板]一维前缀和2.DP35[模板]二维前缀和3.寻找数组的中心下标4.除自身以外数组的乘积5.和为K的子数组6.和可被K整除的子数组7.连续数组8.矩阵区域和 1.DP34[模板]一维前缀和 一维前缀和 #include <iostream> #include <vector> using namespace std…

电压跟随器

电压跟随器即输入多大电压就输出多大的电压&#xff0c;那其起什么作用呢&#xff0c;直接用导线不行吗&#xff1f; 下图为Multisim软件仿真结果&#xff0c;很明显输入电压6.5V输出电压使用万用表测得同为6.5V&#xff0c;验证了电压跟随器的作用。 在同相放大电路的基础上&a…

安装SSL证书有什么意义?

在当今的数字化时代&#xff0c;网络安全已经成为了一个重要的议题。为了保护网站和用户数据的安全&#xff0c;许多网站都选择了安装SSL证书。同时&#xff0c;很多用户不明白安装SSL证书到底有什么意义&#xff1f; 一、网站实现加密传输 用户通过http协议访问网站时&#x…

单片机语音芯片在工业控制中的应用优势

单片机语音芯片&#xff0c;这一智能化的代表产品&#xff0c;不仅在家庭和消费电子领域发挥着重要的作用&#xff0c;更为工业控制领域注入了新的活力。将单片机语音芯片与语音交互技术相结合&#xff0c;为工业设备的控制和监测提供了前所未有的解决方案。 首先&#xff0c;…

【C#】解析和操作HTML文档,并进行同级或跨级匹配元素

给自己一个目标&#xff0c;然后坚持一段时间&#xff0c;总会有收获和感悟&#xff01; 在前端&#xff0c;我们可以通过javascript操作元素&#xff0c;只要知道元素的class或id等属性值&#xff0c;学过jQuery的小伙伴&#xff0c;使用jQuery操作元素对象更加方便。 如果是后…

解决SSH连接自动断开的问题

一、环境 centos7.6 xshell7 二、目标 ssh长联状态&#xff0c;不主动断开 三、实施 1.修改/etc/profile文件 末尾添加export TMOUT0 vim /etc/profileexport TMOUT02.修改/etc/ssh/sshd_config文件 默认都被注释掉&#xff0c;放开并设置CAI参数为0-60间 vim /etc…

基于单片机设计的电子指南针(LSM303DLH模块(三轴磁场 + 三轴加速度)

一、前言 本项目是基于单片机设计的电子指南针&#xff0c;主要利用STC89C52作为主控芯片和LSM303DLH模块作为指南针模块。通过LCD1602液晶显示屏来展示检测到的指南针信息。 在日常生活中&#xff0c;指南针是一种非常实用的工具&#xff0c;可以帮助我们确定方向&#xff0…