欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot+Mybatis+Durid整合多数据源的三种方式,第三种(注解切换)

程序员文章站 2022-04-15 19:11:58
pom文件和yml配置在SpringBoot+Mybatis+Durid整合多数据源的三种方式,第一种这个方式,我目前系统正在使用,很方便很好用!注解切换我的目录结构:第一步:配置类DataSourceContextHolderpackage ky.arraign.common.datasourcetools.kydsconfig;import lombok.extern.slf4j.Slf4j;/** * @Description:动态切换数据源配置文件 * @Author:Y...

pom文件和yml配置在SpringBoot+Mybatis+Durid整合多数据源的三种方式,第一种

这个方式,我目前系统正在使用,很方便很好用!注解切换

我的目录结构:
SpringBoot+Mybatis+Durid整合多数据源的三种方式,第三种(注解切换)

第一步:配置类

  • DataSourceContextHolder
package ky.arraign.common.datasourcetools.kydsconfig;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description:动态切换数据源配置文件
 * @Author:YJG
 * @Date 9:36 2020/5/25
 */
@Slf4j
public class DataSourceContextHolder {
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    /**设置数据源名称*/
    public static void setDataSource(String dataSource){
        log.info("应用<{}>数据源!",dataSource);
        contextHolder.set(dataSource);
    }

    public static String getDataSource(){
        return contextHolder.get();
    }

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

  • DynamicDataSource
package ky.arraign.common.datasourcetools.kydsconfig;


import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * @Description: 动态切换数据源配置文件
 * @Author:YJG
 * @Date 9:34 2020/5/25
 */
public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContextHolder.getDataSource();
    }
}

  • DataSourceConfig
package ky.arraign.common.datasourcetools.kydsconfig;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:动态切换数据源配置文件
 * @Author:YJG
 * @Date 9:39 2020/5/25
 */
@Configuration
@MapperScan(sqlSessionFactoryRef = "SqlSessionFactory",basePackages = "ky.arraign.*.mapper")
public class DataSourceConfig {
    /**引入数据源*/
    @Bean(name = "arraignDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.druid.arraign")
    public DataSource getDateSourceArraign() {
        /**下面一行代码表示启用阿里数据源*/
        return DruidDataSourceBuilder.create().build();
        /**下面一行代码表示启用SpringBoot2.0默认数据源hikari*/
        // return DataSourceBuilder.create().build();
    }
    @Bean(name = "dzblDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.dzbl")
    public DataSource getDateSourceDzbl() {
        return  DruidDataSourceBuilder.create().build();
        //return DataSourceBuilder.create().build();
    }
    @Bean(name = "gawlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.gawl")
    public DataSource getDateSourceGawl() {
        return  DruidDataSourceBuilder.create().build();
        //return DataSourceBuilder.create().build();
    }
    /**
     * 数据源切换: 通过AOP在不同数据源之间动态切换
     */
    @Bean(name = "dynamicDataSource")
    public DataSource dynamicDataSource(){

        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        //设置默认数据源
        dynamicDataSource.setDefaultTargetDataSource(getDateSourceArraign());
        //配置多数据源
        Map<Object,Object> dsMap = new HashMap<>(2);
        dsMap.put(DataSourceName.ARRAIGN,getDateSourceArraign());
        dsMap.put(DataSourceName.DZBL,getDateSourceDzbl());
        dsMap.put(DataSourceName.GAWL,getDateSourceGawl());

        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }
    @Bean(name = "SqlSessionFactory")
    public SqlSessionFactory arraignSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dynamicDataSource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:ky/arraign/**/mapper/xml/*Mapper.xml"));
        return bean.getObject();
    }
    /**
     * @Description:配置@Transactional注解事务(影响数据源切换注释掉)
     *              配置事务需要额外增加@Transactional注解
     * @Author:YJG
     * @Date 10:59 2020/5/26
     * @Param
     * @ReturnType
     */
    @Bean(name = "arraignDataSourceManager")
    public PlatformTransactionManager arraignDataSourceManager(@Qualifier("arraignDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name = "dzblDataSourceManager")
    public PlatformTransactionManager DZBLDataSourceManager(@Qualifier("dzblDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name = "gawlDataSourceManager")
    public PlatformTransactionManager gawlDataSourceManager(@Qualifier("gawlDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

  • DataSourceAspect
package ky.arraign.common.datasourcetools.kyaspect;

import ky.arraign.common.datasourcetools.kyannotation.KYDataSource;
import ky.arraign.common.datasourcetools.kydsconfig.DataSourceContextHolder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Description:自定义切面
 * @Author:YJG
 * @Date 9:43 2020/5/25
 */
@Aspect
@Component
public class DataSourceAspect {
    /**自定义切点*/
    @Pointcut("@annotation(ky.arraign.common.datasourcetools.kyannotation.KYDataSource)")
    public void pointcutConfig(){

    }
    @Before("pointcutConfig()")
    public void before(JoinPoint joinPoint){
        //获得当前访问的class
        Class<?> className = joinPoint.getTarget().getClass();
        //获得访问的方法名
        String methodName = joinPoint.getSignature().getName();
        //得到方法的参数的类型
        Class[] argClass = ((MethodSignature)joinPoint.getSignature()).getParameterTypes();

        String dataSource = null;
        try {
            // 得到访问的方法对象
            Method method = className.getMethod(methodName, argClass);
            // 判断是否存在@KYDataSource
            if (method.isAnnotationPresent(KYDataSource.class)) {
                KYDataSource annotation = method.getAnnotation(KYDataSource.class);
                // 取出注解中的数据源名
                dataSource = annotation.value();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 切换数据源
        DataSourceContextHolder.setDataSource(dataSource);
    }



    @After("pointcutConfig()")
    public void after(JoinPoint joinPoint){
        DataSourceContextHolder.clearDataSource();
    }
}

  • KYDataSource
package ky.arraign.common.datasourcetools.kyannotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @Description:自定义注解
 * @Author:YJG
 * @Date 9:24 2020/5/25
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface KYDataSource {
    String value() default "arraign";
}

  • DataSourceName
package ky.arraign.common.datasourcetools.kydsconfig;

/**
 * @Author:YJG86166
 * @Date:2020/5/25 16:41
 */
public final class DataSourceName {

    public static final String ARRAIGN = "arraign";
    public static final String DZBL = "dzbl";

}


开始使用

  • controller
package ky.arraign.web.controller.system;

import ky.arraign.common.api.system.DemoApi;
import ky.arraign.common.po.system.Demo;
import ky.arraign.common.shiro.response.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 多数据源应用案例
 * @Author:YJG
 * @Date 16:17 2020/5/25
 */
@RestController
@RequestMapping("/test")
public class DemoController {

    @Autowired
    DemoApi demoApi;
    @GetMapping("/demo")
    public ServerResponse demo(){
        List<Demo> demos = demoApi.listResult();
        return ServerResponse.responseSuccess(demos);
    }
}

  • service 接口
package ky.arraign.common.api.system;

import ky.arraign.common.po.system.Demo;

import java.util.List;

/**
 * @Description:多数据源应用demo
 * @Author:YJG
 * @Date 16:23 2020/5/25
 * @Param
 * @ReturnType
 */
public interface DemoApi {
    /**测试
     * @return demo*/
    List<Demo> listResult();
}

  • service 实现类
package ky.arraign.backservice.service;

import ky.arraign.backservice.mapper.DemoMapper;
import ky.arraign.common.api.system.DemoApi;
import ky.arraign.common.datasourcetools.kyannotation.KYDataSource;
import ky.arraign.common.datasourcetools.kydsconfig.DataSourceContextHolder;
import ky.arraign.common.datasourcetools.kydsconfig.DataSourceName;
import ky.arraign.common.po.system.Demo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author:yjg86166
 * @Date:2020/5/25 16:25
 */
@Service
@Slf4j
public class DemoApiImpl implements DemoApi {

    @Autowired
    private DemoMapper demoMapper;

    @Override
    //@KYDataSource //默认数据源arraign 可以不写
    //@KYDataSource(DataSourceName.ARRAIGN) //默认数据源 可以不写
    /**注解里字段的值*/
    @KYDataSource(DataSourceName.DZBL) //切换至单子笔录数据源
    @Transactional(value = "arraignDataSourceManager",rollbackFor = Exception.class)
    public List<Demo> listResult() {
        //此时在使用dzbl数据源
        List<Demo> demos = demoMapper.listDeptInfo();
        //切换至arraign
        DataSourceContextHolder.setDataSource("arraign");
        List<Demo> demos1 = this.listUseArraign();
        //...逻辑代码
        /**如果一下逻辑不需使用arraign则需要切换回dzbl*/
        DataSourceContextHolder.setDataSource("dzbl");
        return demos;
    }

    public List<Demo> listUseArraign(){
        return demoMapper.listDeptInfo();
    }
}

  • mapper 接口类
package ky.arraign.backservice.mapper;

import ky.arraign.common.po.system.Demo;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: 多数据源应用demo
 * @Author:YJG
 * @Date 16:21 2020/5/25
 * @Param
 * @ReturnType
 */
@Component
public interface DemoMapper {
    List<Demo> listDeptInfo();
}
  • mapper.xml
<?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="ky.arraign.backservice.mapper.DemoMapper">
   <select id="listDeptInfo" resultType="ky.arraign.common.po.system.Demo">
       SELECT * FROM t_dept
   </select>
</mapper>

完成!使用的注意事项已经在代码进行提示,各位大佬如有疑问或者有建议请评论或者私信!!!

本文地址:https://blog.csdn.net/weixin_44253327/article/details/107590050