mybatis-plus 支持不同数据源sql切换
本篇内容主要讲解的是mybatis-plus 支持不同数据源sql切换
直接上代码
1、结构代码
2、引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- Tag-mybatis plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3</version></dependency><!-- Tag-mybatis plus--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</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>
3、代码
MybatisPlusConfig
@Configuration
@MapperScan("com.liuhm.dao.mapper*")
public class MybatisPlusConfig {@AutowiredMybatisPlusProperties mybatisPlusProperties;/*** 分页插件*/@Beanpublic PaginationInterceptor paginationInterceptor() {PaginationInterceptor paginationInterceptor = new PaginationInterceptor();paginationInterceptor.setDialectType(mybatisPlusProperties.getGlobalConfig().getDbConfig().getDbType().getDb());return paginationInterceptor;}/*** SQL执行效率插件* 设置 dev test 环境开启*/@Bean/*@Profile({"dev","test"})*/public PerformanceInterceptor performanceInterceptor() {return new HyperformanceInterceptor();}}
MenuMapper.java
/*** 菜单 mapper 接口** @author liuhaomin* @date 2023.7.10*/
@Repository
@CacheNamespace
public interface MenuMapper extends BaseMapper<Menu> {public List<Menu> select();
}
com.liuhm.dao.mysqlmapper.MenuMapper.xml
mysql查询10条
<?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.MenuMapper"><cache-ref namespace="com.liuhm.dao.mapper.MenuMapper"/><select id="select" resultType="com.liuhm.entity.Menu">select * from mcp_menu limit 10</select></mapper>
com.liuhm.dao.tidb.MenuMapper.xml
tidb查询1条
<?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.MenuMapper"><cache-ref namespace="com.liuhm.dao.mapper.MenuMapper"/><select id="select" resultType="com.liuhm.entity.Menu">select * from mcp_menu limit 1</select></mapper>
application.yml
spring:datasource:# mysql
# driver-class-name: com.mysql.jdbc.Driver
# username: root
# password: Cobbler1234!
# url: jdbc:mysql://192.168.0.229:43306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
# gaussdb
# driver-class-name: org.opengauss.Driver
# username: ga_mason
# password: OpenGauss@123
# url: jdbc:opengauss://192.168.0.120:5432/my_db
# tidbdriver-class-name: com.mysql.jdbc.Driverusername: rootpassword: 123456url: jdbc:mysql://192.168.56.10:4000/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
mybatis-plus:mapper-locations: classpath*:com/liuhm/dao/tidbmapper/*Mapper.xml# 实体扫描,多个package用逗号或者分号分隔type-aliases-package: com.liuhm.entityglobal-config:db-config:id-type: id_workerfield-strategy: not_nulllogic-delete-value: 0logic-not-delete-value: 0db-type: mysqlbanner: falseconfiguration:map-underscore-to-camel-case: truecache-enabled: falseserver:port: 9099
★4、不同数据源进行切换步骤
改变对应的连接方式,修改连接,驱动
增加对应的特殊sql
修改db-type
修改对应的mapper-locations
5、测试
使用mysql时候
yml 配置 设置扫描xml地址
mybatis-plus:mapper-locations: classpath*:com/liuhm/dao/mysqlmapper/*Mapper.xmlglobal-config:db-config:db-type: mysql
启动服务测试
MenuMapperTest.select
使用tidb时候
yml 配置 设置扫描xml地址
mybatis-plus:mapper-locations: classpath*:com/liuhm/dao/tidbmapper/*Mapper.xmlglobal-config:db-config:db-type: mysql
启动服务测试
MenuMapperTest.select
6、不同版本对应分页插件情况
官方地址
3.1.0版本支持的分页插件
MYSQL("mysql", "%s LIKE CONCAT('%%',#{%s},'%%')", "MySql数据库"),MARIADB("mariadb", "%s LIKE CONCAT('%%',#{%s},'%%')", "MariaDB数据库"),ORACLE("oracle", "%s LIKE CONCAT(CONCAT('%%',#{%s}),'%%')", "Oracle数据库"),DB2("db2", "%s LIKE CONCAT(CONCAT('%%',#{%s}),'%%')", "DB2数据库"),H2("h2", "%s LIKE CONCAT('%%',#{%s},'%%')", "H2数据库"),HSQL("hsql", "%s LIKE CONCAT('%%',#{%s},'%%')", "HSQL数据库"),SQLITE("sqlite", "%s LIKE CONCAT('%%',#{%s},'%%')", "SQLite数据库"),POSTGRE_SQL("postgresql", "%s LIKE CONCAT('%%',#{%s},'%%')", "Postgre数据库"),SQL_SERVER2005("sqlserver2005", "%s LIKE '%%'+#{%s}+'%%'", "SQLServer2005数据库"),SQL_SERVER("sqlserver", "%s LIKE '%%'+#{%s}+'%%'", "SQLServer数据库"),DM("dm", (String)null, "达梦数据库"),OTHER("other", (String)null, "其他数据库");
3.5.3版本支持的分页插件
MYSQL("mysql", "MySql数据库"),MARIADB("mariadb", "MariaDB数据库"),ORACLE("oracle", "Oracle11g及以下数据库(高版本推荐使用ORACLE_NEW)"),ORACLE_12C("oracle12c", "Oracle12c+数据库"),DB2("db2", "DB2数据库"),H2("h2", "H2数据库"),HSQL("hsql", "HSQL数据库"),SQLITE("sqlite", "SQLite数据库"),POSTGRE_SQL("postgresql", "Postgre数据库"),SQL_SERVER2005("sqlserver2005", "SQLServer2005数据库"),SQL_SERVER("sqlserver", "SQLServer数据库"),DM("dm", "达梦数据库"),XU_GU("xugu", "虚谷数据库"),KINGBASE_ES("kingbasees", "人大金仓数据库"),PHOENIX("phoenix", "Phoenix HBase数据库"),GAUSS("zenith", "Gauss 数据库"),CLICK_HOUSE("clickhouse", "clickhouse 数据库"),GBASE("gbase", "南大通用(华库)数据库"),GBASE_8S("gbase-8s", "南大通用数据库 GBase 8s"),/** @deprecated */@DeprecatedGBASEDBT("gbasedbt", "南大通用数据库"),/** @deprecated */@DeprecatedGBASE_INFORMIX("gbase 8s", "南大通用数据库 GBase 8s"),OSCAR("oscar", "神通数据库"),SYBASE("sybase", "Sybase ASE 数据库"),OCEAN_BASE("oceanbase", "OceanBase 数据库"),FIREBIRD("Firebird", "Firebird 数据库"),HIGH_GO("highgo", "瀚高数据库"),CUBRID("cubrid", "CUBRID数据库"),GOLDILOCKS("goldilocks", "GOLDILOCKS数据库"),CSIIDB("csiidb", "CSIIDB数据库"),SAP_HANA("hana", "SAP_HANA数据库"),IMPALA("impala", "impala数据库"),VERTICA("vertica", "vertica数据库"),XCloud("xcloud", "行云数据库"),REDSHIFT("redshift", "亚马逊redshift数据库"),OPENGAUSS("openGauss", "华为 opengauss 数据库"),TDENGINE("TDengine", "TDengine数据库"),INFORMIX("informix", "Informix数据库"),UXDB("uxdb", "优炫数据库"),OTHER("other", "其他数据库");
博客地址
代码下载
下面的mybatis-plus-demo