springboot+动态数据源切换+mybatis

数据源切换的目的

目前公司参照旧系统的模式开发新的系统,需要将旧系统的数据库(oracle)里的某些表的数据同步到新系统的数据库(mysql)的表里,所以开发个同步工具,在操作数据库的过程中切换数据源,将oracle的数据取出来存到mysql里。

项目的依赖

	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- aop面向切面 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!-- 热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- 19c的oracle版本 -->
        <dependency>
            <groupId>com.oracle.ojdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>19.3.0.0</version>
        </dependency>
        <dependency>
            <groupId>cn.easyproject</groupId>
            <artifactId>orai18n</artifactId>
            <version>12.1.0.2.0</version>
        </dependency>

        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.11</version>
        </dependency>

        <!-- 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

配置文件application.properties

#oracle
spring.datasource.master.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.master.jdbc-url=jdbc:oracle:thin:@127.0.0.1/orcl
spring.datasource.master.username=root
spring.datasource.master.password=123456

#mysql
spring.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.slave.jdbc-url=jdbc:mysql://localhost:3306/province_platform?useUnicode=true&characterEncoding=UTF-8&&nullCatalogMeansCurrent=true&useSSL=false
spring.datasource.slave.username=root
spring.datasource.slave.password=123456

#print sql
logging.level.com.province.*=debug
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

server.port=8099

项目结构

项目结构

创建枚举类DataSourceType

/**
 * 列出所有的数据源
 */
public enum DataSourceType {
    master,
    slave;
}

数据源切换注解

/**
 *  自定义注解,用于类或方法上,优先级:方法>类
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSource {
    DataSourceType value() default DataSourceType.master;
}

创建动态数据源上下文

/**
 * 动态数据源上下文管理:设置数据源,获取数据源,清除数据源
 */
public class DataSourceContextHolder {
    // 存放当前线程使用的数据源类型
    private static final ThreadLocal<DataSourceType> contextHolder = new ThreadLocal<>();

    // 设置数据源
    public static void setDataSource(DataSourceType type){
        contextHolder.set(type);
    }

    // 获取数据源
    public static DataSourceType getDataSource(){
        return contextHolder.get();
    }

    // 清除数据源
    public static void clearDataSource(){
        contextHolder.remove();
    }
}

动态数据源

/**
 * 动态数据源上下文管理:设置数据源,获取数据源,清除数据源
 */
public class DataSourceContextHolder {
    // 存放当前线程使用的数据源类型
    private static final ThreadLocal<DataSourceType> contextHolder = new ThreadLocal<>();

    // 设置数据源
    public static void setDataSource(DataSourceType type){
        contextHolder.set(type);
    }

    // 获取数据源
    public static DataSourceType getDataSource(){
        return contextHolder.get();
    }

    // 清除数据源
    public static void clearDataSource(){
        contextHolder.remove();
    }
}

数据源配置

@Configuration(proxyBeanMethods = false)
@MapperScan(basePackages = "com.province.mapper")
public class MybatisConfig {

    @Bean("master")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        DataSource build = DataSourceBuilder.create().build();
        return build;
    }

    @Bean("slave")
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    public DataSource slaveDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public DynamicDataSource dataSource(@Qualifier("master") DataSource masterDataSource,
                                        @Qualifier("slave") DataSource slaveDataSource) {
        Map<Object, Object> map = new HashMap<>();
        map.put(DataSourceType.master, masterDataSource);
        map.put(DataSourceType.slave, slaveDataSource);

        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(map);
        dynamicDataSource.setDefaultTargetDataSource(masterDataSource);

        return dynamicDataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DynamicDataSource dynamicDataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dynamicDataSource);
        // 设置mapper.xml的位置路径
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*Mapper.xml");

        factoryBean.setMapperLocations(resources);
        return factoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager(DynamicDataSource dynamicDataSource) {
        return new DataSourceTransactionManager(dynamicDataSource);
    }

}

AOP切面设置数据源

@Slf4j
@Aspect
@Component
public class DataSourceAspect {

    @Before("@annotation(ds)")
    public void beforeDataSource(DataSource ds) {
        DataSourceType value = ds.value();
        DataSourceContextHolder.setDataSource(value);
        log.info("当前使用的数据源为:{}", value);
    }
    @After("@annotation(ds)")
    public void afterDataSource(DataSource ds){
        DataSourceContextHolder.clearDataSource();
    }

    @Around("execution(* com.province.task..*.*(..))")
    public void aroundMethod(ProceedingJoinPoint proceedingJoinPoint){
        log.info("类名:"+ proceedingJoinPoint.getSignature().getDeclaringTypeName());
        log.info("方法名:"+ proceedingJoinPoint.getSignature().getName());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            Object result = proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        stopWatch.stop();
        log.info("执行方法耗时:【"+ stopWatch.getTotalTimeMillis()/1000+"】s");
    }
}

自动生成实体xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="context1">
        <!--<property name="javaFileEncoding" value="UTF-8"/> &lt;!&ndash; 生成一对一配置 &ndash;&gt;-->
        <!--<plugin type="cc.bandaotixi.plugins.OneToOnePlugin"></plugin> &lt;!&ndash; 生成一对多配置 &ndash;&gt;-->
        <!--<plugin type="cc.bandaotixi.plugins.OneToManyPlugin"></plugin>-->
        <!--<plugin type="cc.bandaotixi.plugins.BatchInsertPlugin"></plugin>-->
        <!--<plugin type="cc.bandaotixi.plugins.BatchUpdatePlugin"></plugin>-->
        <!--<commentGenerator type="cc.bandaotixi.plugins.BDTCommentGenerator">-->
        <!--<property name="suppressDate" value="true"/>-->
        <!--<property name="suppressAllComments" value="false" />-->
        <!--</commentGenerator>-->
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:-->
            <property
                    name="suppressAllComments"
                    value="false"/>
        </commentGenerator>
        <!--<jdbcConnection
                connectionURL="jdbc:mysql://localhost:3306/province_platform?useUnicode=true&amp;characterEncoding=UTF-8&amp;nullCatalogMeansCurrent=true&amp;useSSL=false"
                driverClass="com.mysql.jdbc.Driver"
                password="123456"
                userId="root"/>-->
        <jdbcConnection
                connectionURL="jdbc:oracle:thin:@127.0.0.1/orcl"
                driverClass="oracle.jdbc.driver.OracleDriver"
                password="root"
                userId="123456"/>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
            和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property
                    name="forceBigDecimals"
                    value="false"/>
        </javaTypeResolver>
        <!-- 生成的POJO代码放置的包名 还有存放的位置 -->
        <javaModelGenerator targetPackage="com.province.entity.oracle"
                            targetProject="./src/main/java">
            <property
                    name="enableSubPackages"
                    value="false"/>
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property
                    name="trimStrings"
                    value="true"/>
        </javaModelGenerator>

        <!-- 实体包对应映射文件位置及名称,默认存放在src目录下 -->
        <sqlMapGenerator targetPackage="mapper.oracle" targetProject="./src/main/resources">
            <property
                    name="enableSubPackages"
                    value="false"/>
        </sqlMapGenerator>
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.province.mapper.oracle" targetProject="./src/main/java">
            <property
                    name="enableSubPackages"
                    value="false"/>
        </javaClientGenerator>
        <!-- 配置表
            schema:不用填写
            tableName: 表名
            enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:
            去除自动生成的例子
        -->
<!--        <table schema="" tableName="Role"/>-->
<!--        <table schema="" tableName="Role_Auth_Mapping"/>-->
<!--        <table schema="" tableName="Staff_Role_Mapping"/>-->
<!--        <table schema="" tableName="Staff"/>-->
<!--        <table schema="" tableName="Authority"/>-->
<!--        <table schema="" tableName="Curricula_Variable_Record"/>-->
<!--        <table schema="" tableName="Data_Dictionary"/>-->
<!--        <table schema="" tableName="Operation_Log"/>-->
       <!-- <table schema="" tableName="unit_person"/>-->
        <table schema="root" tableName="bas_ry" /> <!-- 如果是oracle要填写schema用户 -->
        <!--<table schema="root" tableName="JCJG_STATISTIC_ETP_REPORT" />-->
<!--        <table schema="" tableName="Teaching_Task"/>-->
    </context>
</generatorConfiguration>

实体生成器配置

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- generator实体生成器  -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.11</version>
                    </dependency>
                    <dependency>
                        <groupId>com.oracle.ojdbc</groupId>
                        <artifactId>ojdbc8</artifactId>
                        <version>19.3.0.0</version>
                    </dependency>
                    <dependency>
                        <groupId>cn.easyproject</groupId>
                        <artifactId>orai18n</artifactId>
                        <version>12.1.0.2.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <configurationFile></configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

数据访问层

本项目默认访问的主数据源oracle,如果要切换数据源,则要在mapper的方法上加上注解,业务层这里就不写了。
在这里插入图片描述

反射实体工具类

本项目由于两边交换有相同的表,则开发了此工具类,将原先oracle的数据格式化处理(原因一些旧oracle的数据没有去空的数据,以及oracle的TimeStamp类型的时间存入mysql的问题,详细看图注释),保证数据的合理性,利用反射的方法将格式化的数据重新存会原先的对象

EntityFilterUtil

package com.province.util;

import java.lang.reflect.Field;
import java.util.Date;

/***
 * 格式化实体(两边数据库相同实体)
 */
public class EntityFilterUtil {

    public static Object doFilter(Object obj){
        Class clazz = obj.getClass();
        //获取该类的所有字段,返回一个数组
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // 避免获取不了私有属性
            field.setAccessible(true);
            try {
                //判断类型为String的属性,将String类型的字段值去空处理
                if(String.class.equals(field.getType())){
                    //判断取值是否为空,不为空则格式化值
                    if(field.get(obj) != null) {
                        String value = StringUtil.toTrim(field.get(obj).toString());
                        field.set(obj, value);
                    }
                }
                //判断类型为Date的属性,将Date类型的字段值小于1970-01-01 08:00:00格式化为1970-01-01 08:00:00
                //原因mysql不能存小于1970-01-01 08:00:00的时间(TimeStamp)
                else if(Date.class.equals(field.getType())){
                    //判断取值是否为空,不为空则格式化值
                    if(field.get(obj) != null){
                        Date date = StringUtil.timeChange((Date) field.get(obj));
                        field.set(obj, date);
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }
}

其他工具类

StringUtil

public class StringUtil {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 将oracle时间小于1970-01-01 08:00:01的时间转为mysql存入的最小时间1970-01-01 08:00:01
     * @param timestamp
     * @return
     */
    public static Date timeChange(Date timestamp){
        if(timestamp != null) {
            String time = "1970-01-01 08:00:01";
            long timestampTime = timestamp.getTime();
            try {
                Date date = dateFormat.parse(time);
                if (timestampTime < date.getTime()) {
                    return date;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return timestamp;
    }

    /**
     * 将oracle有空格的字段都去掉空格
     * @param obj
     * @return
     */
    public static String toTrim(String obj){
        if(obj != null && !"".equals(obj)){
            return obj.replace(" ","");
        }
        return obj;
    }
}

  • 本项目数据源配置参考路径 这儿.
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值