手搭手Mybatis-Plus数据迁移至TDSQL

环境介绍

|-------------|-----------------------------------------------------|
| 技术栈 | springboot+mybatis-plus+druid+baomidou+mysql+oracle |
| 软件 | 版本 |
| mysql | 8 |
| IDEA | IntelliJ IDEA 2022.2.1 |
| JDK | 1.8 |
| Spring Boot | 2.7.13 |
| mybatis | 2.3.1 |

开启访问外网

IDEA环境搭建

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>

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
        DSQL:
          url: jdbc:mysql://gz-cynosdbmyxxxxxxxxxxxxxxxxxxxxxxx.com:29569/ToDemo
          username: root
          password: pwd
          driver-class-name: com.mysql.cj.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逆向工程只是一个便携的工具,自定义接口和实现类还是要自己写

IDEA连接TDSQL

MybatisX逆向工程

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

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

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);
}

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

service接口层

public interface UserService extends IService<User> {
}

public interface TUserService extends IService<TUser> {
}

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);
}

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);
    }
}

@Service
@DS("TDSQL")
public class TdUserServiceImpl extends ServiceImpl<TdUserMapper, TdUser>
    implements TdUserService{
}

测试类

@Autowired
    private TUserService tUserService;

    @Autowired
    private UserService userService;

    @Autowired
    private TestService testService;

    @Autowired
    private DmuserService dmuserService;

    @Autowired
    private TdUserService tdUserService;


@Test
void TDSqlDemo(){
    tdUserService.list();
}

迁移测试

Mysql迁移至TDSQL

@Test
        void MysqlToTDsql(){
            List<TUser> mysqls =tUserService.list();
            for (TUser mysql : mysqls) {
                TdUser tdUser =new TdUser();
                tdUser.setId("");
                tdUser.setName(mysql.getName());
                tdUser.setGender(0);
                tdUser.setEmail(mysql.getEmail());
                tdUserService.save(tdUser);
                System.out.println(""+mysql);
            }
        }

Oracle迁移至TDSQL

@Test
        void OracleToTDsql(){
        List<com.example.domain.Test> oracles =testService.selectTestAll();
        for (com.example.domain.Test oracle : oracles) {
            TdUser tdUser =new TdUser();
            tdUser.setId(String.valueOf(oracle.getId()));
            tdUser.setName(oracle.getName());
            tdUser.setEmail(String.valueOf(oracle.getClass()));
            tdUserService.save(tdUser);
            System.out.println(""+oracle);
        }

DM8(达梦)迁移至TDSQL

TDSQL-C MySQL 版(TDSQL-C for MySQL)是腾讯云自研的新一代云原生关系型数据库。融合了传统数据库、云计算与新硬件技术的优势,为用户提供具备高弹性、高性能、海量存储、安全可靠的数据库服务。TDSQL-C MySQL 版100%兼容 MySQL 5.7、8.0。实现超百万级 QPS 的高吞吐,最高 PB 级智能存储,保障数据安全可靠。

TDSQL-C MySQL 版采用存储和计算分离的架构,所有计算节点共享一份数据,提供秒级的配置升降级、秒级的故障恢复,单节点可支持百万级 QPS,自动维护数据和备份,最高以GB/秒的速度并行回档。

TDSQL-C MySQL 版既融合了商业数据库稳定可靠、高性能、可扩展的特征,又具有开源云数据库简单开放、高效迭代的优势。TDSQL-C MySQL 版引擎完全兼容原生 MySQL,您可以在不修改应用程序任何代码和配置的情况下,将 MySQL 数据库迁移至 TDSQL-C MySQL 版引擎。

Mybatis-Plus

https://baomidou.com/

为简化开发而生

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


相关推荐
蓝染-惣右介28 分钟前
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
java·数据库·tomcat·mybatis
武子康1 小时前
Java-07 深入浅出 MyBatis - 一对多模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据库·sql·mybatis·springboot
一二小选手2 小时前
【Mybatis】@Param注解 resultMap手动映射
java·mybatis
郑祎亦4 小时前
Spring Boot 项目 myblog 整理
spring boot·后端·java-ee·maven·mybatis
jokerest12318 小时前
web——sqliabs靶场——第十三关——报错注入+布尔盲注
mybatis
武子康18 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
WindFutrue21 小时前
使用Mybatis向Mysql中的插入Point类型的数据全方位解析
数据库·mysql·mybatis
AiFlutter1 天前
Java实现简单的搜索引擎
java·搜索引擎·mybatis
天天扭码1 天前
五天SpringCloud计划——DAY1之mybatis-plus的使用
java·spring cloud·mybatis