MyBatis中多数据库支持---待续

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: MyBatis中多数据库支持---待续

注意:该文章只是指明MyBatis怎么匹配多种数据库,而不是多数据源的实现

在实际项目开发中可能实现多数据开发,用MyBatis中的databaseIdProvider数据库标签可以直接配置多数据源,使用的时候可以直接在if标签上面根据databaseId来加载不同数据库的数据。

使用多数据源只要有按照如下步骤即可:

第一步

在mybatis-config.xml中加入databaseIdProvider配置即可

<databaseIdProvider type="DB_VENDOR">
    <property name="SQL Server" value="sqlserver"/>
    <property name="DB2" value="db2"/>
    <property name="Oracle" value="oracle"/>
    <property name="MySQL" value="mysql"/>
    <property name="PostgreSQL" value="ppostgresql"/>
    <property name="Derby" value="derby"/>
    <property name="HSQL" value="hsqldb"/>
    <property name="H2" value="h2"/>
</databaseIdProvider>
由于每个数据厂商对应数据库产品名称不同,这里的DB_VENDOR会通过DatabaseMetaData#getDatabaseProductName()返回的字符串进行设置,通常情况下都会比较长,所以通常会设置别名。如上代码设置了常见厂商的别名

第二步

配置了配置文件之后,我们需要对mapper映射文件中的需要加载其他数据库数据的标签进行设置。这里只演示like在MySQL和Oracle中的实现

  • MySQL
<select id="selectByUserToMySQL" resultType="com.echo.springmybatis.model.SysUserPo" databaseId="mysql">
    select
    id, user_name userName, user_password userPassword,
    user_email userEmail, user_info userInfo, head_img headImg,
    create_time createTime
    from sys_user
    <where>
        <if test="userName != null and userName != ''">
            <bind name="userName" value="'%' + userName + '%'"/>
            and user_name like concat('%', #{userName}, '%')
        </if>
        <if test="userEmail !='' and userEmail != null">
            and user_email = #{userEmail}
        </if>
    </where>
</select>
  • Oracle
<select id="selectByUserToOracle" resultType="com.echo.springmybatis.model.SysUserPo" databaseId="oracle">
    select
    id, user_name userName, user_password userPassword,
    user_email userEmail, user_info userInfo, head_img headImg,
    create_time createTime
    from sys_user
    <where>
        <if test="userName != null and userName != ''">
            <bind name="userName" value="'%' + userName + '%'"/>
            and user_name like '%'||#{userName}||'%')
        </if>
        <if test="userEmail !='' and userEmail != null">
            and user_email = #{userEmail}
        </if>
    </where>
</select>
  • 注意以上两种用法要配置对应的数据源,如果要项目中有数据源的切换,这里只需要按照如下写法即可,不需要更改xml代码
<select id="selectByUserToDatabase" resultType="com.echo.springmybatis.model.SysUserPo">
    select
    id, user_name userName, user_password userPassword,
    user_email userEmail, user_info userInfo, head_img headImg,
    create_time createTime
    from sys_user
    <where>
        <if test="userName != null and userName != ''">
            <if test="_databaseId == 'oracle'">
              and user_name like '%'||#{userName}||'%')
            </if>
            <if test="_databaseId == 'mysql'">
              and user_name like conat('%', #{userName}, '%')
            </if>
        </if>
        <if test="userEmail !='' and userEmail != null">
            and user_email = #{userEmail}
        </if>
    </where>
</select>
以上代码直接在关键部分使用if添加_databaseId来判断需要查询的是什么数据库,在有数据库切换的时候,能够有效的改善更改xml代码的操作。但是如果数据库切换很少,不建议这么使用,会增加很多冗余代码。
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
9天前
|
XML Java 数据库连接
【MyBatis】MyBatis操作数据库(一)
【MyBatis】MyBatis操作数据库(一)
17 1
|
6天前
|
SQL XML Java
后端数据库开发JDBC编程Mybatis之用基于XML文件的方式映射SQL语句实操
后端数据库开发JDBC编程Mybatis之用基于XML文件的方式映射SQL语句实操
24 3
|
6天前
|
Java 数据库连接 API
后端开发之用Mybatis简化JDBC的开发快速入门2024及数据库连接池技术和lombok工具详解
后端开发之用Mybatis简化JDBC的开发快速入门2024及数据库连接池技术和lombok工具详解
13 3
|
9天前
|
Java 关系型数据库 数据库连接
【MyBatis】初步解析MyBatis:实现数据库交互与关系映射的全面指南
【MyBatis】初步解析MyBatis:实现数据库交互与关系映射的全面指南
16 1
|
10天前
|
Java 数据库连接 数据库
实现Spring Boot与MyBatis结合进行数据库历史数据的定时迁移
实现Spring Boot与MyBatis结合进行数据库历史数据的定时迁移
23 2
|
25天前
|
Java 数据库连接 数据库
Spring日志完结篇,MyBatis操作数据库(入门)
Spring日志完结篇,MyBatis操作数据库(入门)
|
25天前
|
关系型数据库 MySQL 数据库
mysql 中文问号,mybatis-plus insert中文数据库显示问号
mysql 中文问号,mybatis-plus insert中文数据库显示问号
31 1
|
5天前
|
XML 关系型数据库 数据库
使用mybatis-generator插件生成postgresql数据库model、mapper、xml
使用mybatis-generator插件生成postgresql数据库model、mapper、xml
24 0
|
9天前
|
SQL Java 数据库连接
【MyBatis】深入解析MyBatis:高效操作数据库技术详解
【MyBatis】深入解析MyBatis:高效操作数据库技术详解
20 0
|
9天前
|
SQL Java 数据库连接
【MyBatis】MyBatis操作数据库(二):动态SQL、#{}与${}的区别
【MyBatis】MyBatis操作数据库(二):动态SQL、#{}与${}的区别
14 0