手搭手Mybatis-Plus多数据源异构数据迁移案例

举报
QGS 发表于 2023/10/04 12:41:09 2023/10/04
【摘要】 手搭手Mybatis-Plus多数据源异构数据迁移案例(oracle迁移至dm8,mysql迁移至dm8)

环境介绍

技术栈

springboot+mybatis-plus+druid+baomidou+mysql+oracle+dm

软件

版本

mysql

8

IDEA

IntelliJ IDEA 2022.2.1

JDK

1.8

Spring Boot

2.7.13

mybatis

2.3.1

pom.xml所需依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.3.2</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
        <version>3.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.14</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.15</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.dameng</groupId>
        <artifactId>Dm8JdbcDriver18</artifactId>
        <version>8.1.1.49</version>
    </dependency>
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

MybatisX逆向工程

逆向工程:通过数据库表接口,逆向生成java工程结构

实体类、mapper接口、mapper映射文件、Service接口、service实现类



application.yml

spring:
  datasource:
    dynamic:
      primary:  #设置默认的数据源或者数据源组,默认值即为master
      strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      datasource:
        mysql1:
          username: root
          password: PWD
          url: jdbc:mysql://IP:3306/mysql?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
          driver-class-name: com.mysql.cj.jdbc.Driver
          #          driver-class-name: com.mysql.jdbc.Driver
        mysql2:
          url: jdbc:mysql://IP:3306/sys?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
          username: root
          password: PWD
          driver-class-name: com.mysql.cj.jdbc.Driver
        oracle:
          username: system
          password: PWD
          url: jdbc:oracle:thin:@IP:1521:orcl
          driver-class-name: oracle.jdbc.driver.OracleDriver
          #          driver-class-name: com.mysql.jdbc.Driver
        dm:
          url: jdbc:dm://127.0.0.1:5236/demo?
          username: SYSDBA
          password: SYSDBA
          driver-class-name: dm.jdbc.driver.DmDriver
        #          driver-class-name: com.mysql.jdbc.Driver

server:
  port: 8089

mybatis-plus:
  configuration:
    #输出日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #配置映射规则
    map-underscore-to-camel-case: true #表示支持下划线到驼蜂的映射
    #隐藏mybatis图标
  global-config:
    banner: false
    db-config:
      logic-delete-field: status
      logic-not-delete-value: 1
      logic-delete-value: 0

spy.properties

#3.2.1以上使用
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
#3.2.1以下使用或者不配置
#modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2

MybatisX逆向工程只是一个便携的工具,自定义接口和实现类还是要自己写

mapper(dao层)

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

@Mapper
public interface TUserMapper extends BaseMapper<TUser> {
}

@Mapper
public interface TestMapper extends BaseMapper<Test> {
    List<Test> selectTestAll();
}

@Mapper
public interface DmuserMapper extends BaseMapper<Dmuser> {
    List<Dmuser> selectDmUserAll();
    Integer addDmUser(Dmuser dmuser);
}

service接口层

public interface UserService extends IService<User> {
}

public interface TUserService extends IService<TUser> {
}
public interface TestService extends IService<Test> {
    List<Test> selectTestAll();
}
public interface DmuserService extends IService<Dmuser> {
    List<Dmuser> selectDmUserAll();
    Integer addDmUser(Dmuser dmuser);
}

mapper映射文件

<mapper namespace="com.example.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.example.domain.User">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="age" column="age" jdbcType="INTEGER"/>
            <result property="email" column="email" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="INTEGER"/>
            <result property="gender" column="gender" jdbcType="INTEGER"/>
            <result property="contact" column="contact" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,name,age,
        email,status,gender,
        contact,create_time,update_time
    </sql>
</mapper>

<mapper namespace="com.example.mapper.TUserMapper">

    <resultMap id="BaseResultMap" type="com.example.domain.TUser">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="age" column="age" jdbcType="INTEGER"/>
            <result property="email" column="email" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,name,age,
        email
    </sql>
</mapper>

<mapper namespace="com.example.mapper.TestMapper">

    <resultMap id="BaseResultMap" type="com.example.domain.Test">
            <id property="id" column="id" jdbcType="DECIMAL"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,name
    </sql>

    <select id="selectTestAll" resultType="com.example.domain.Test">select * from "test"</select>
</mapper>

<mapper namespace="com.example.mapper.DmuserMapper">

    <resultMap id="BaseResultMap" type="com.example.domain.Dmuser">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="name" column="name" jdbcType="CHAR"/>
            <result property="address" column="address" jdbcType="CHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,name,address
    </sql>
    <select id="selectDmUserAll" resultType="com.example.domain.Dmuser">select "id","name","address"from "SYSDBA"."DMUser";</select>
    <insert id="addDmUser">
        insert into "SYSDBA"."DMUser"("id", "name", "address") VALUES(#{id},#{name},#{address});
    </insert>
</mapper>

serviceimpl类

@Service
@DS("mysql2")
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
}
@Service
@DS("mysql1")
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser>
    implements TUserService{
}

@Service
@DS("oracle")
public class TestServiceImpl extends ServiceImpl<TestMapper, Test>
    implements TestService{
    @Autowired
    private TestMapper testMapper;
    @Override
    public List<Test> selectTestAll() {
        return testMapper.selectTestAll();
    }
}

@Service
@DS("dm")
public class DmuserServiceImpl extends ServiceImpl<DmuserMapper, Dmuser>
    implements DmuserService{
    @Autowired
    private DmuserMapper dmuserMapper;
    @Override
    public List<Dmuser> selectDmUserAll() {
        return dmuserMapper.selectDmUserAll();
    }
    @Override
    public Integer addDmUser(Dmuser dmuser) {
        return dmuserMapper.addDmUser(dmuser);
    }
}

测试类

@SpringBootTest
class DatasourcesApplicationTests {

    @Autowired
    private TUserService tUserService;
    @Autowired
    private UserService userService;
    @Autowired
    private TestService testService;
    @Autowired
    private DmuserService dmuserService;

    @Test
    void sys(){
        userService.list();
    }
    @Test
    void oracleDemo(){
        testService.selectTestAll();
    }
    @Test
    void dmDemo(){
        dmuserService.selectDmUserAll();
    }
    @Test
    void oracleToDMTest(){
        List<com.example.domain.Test> oracles =testService.selectTestAll();
        for (com.example.domain.Test oracle : oracles) {
            Dmuser dmuser =new Dmuser();
            dmuser.setId(Math.toIntExact(oracle.getId()));
            dmuser.setName(oracle.getName());
            dmuser.setAddress("null");
            dmuserService.addDmUser(dmuser);
            System.out.println(""+oracle);
        }
    }
    @Test
    void mysqlToDMTest(){
        List<TUser> mysqls =tUserService.list();
        for (TUser mysql : mysqls) {
            Dmuser dmuser =new Dmuser();
            dmuser.setId(Math.toIntExact(mysql.getId()));
            dmuser.setName(mysql.getName());
            dmuser.setAddress(mysql.getEmail());
            dmuserService.addDmUser(dmuser);
            System.out.println(""+mysql);
        }
    }

}

Mybatis-Plus介绍

https://baomidou.com/

为简化开发而生

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window) 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。


特性

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
  • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
  • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere
  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper Model Service Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
  • 分页插件支持多种数据库:支持 MySQLMariaDBOracleDB2H2HSQLSQLitePostgreSQLServer 等多种数据库
  • 内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
  • 内置全局拦截插件:提供全表 delete update 操作智能分析阻断,也可自定义拦截规则,预防误操作

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。