专栏首页Java技术栈Spring Boot 集成 Mybatis 实现双数据源

Spring Boot 集成 Mybatis 实现双数据源

这里用到了Spring Boot + Mybatis + DynamicDataSource配置动态双数据源,可以动态切换数据源实现数据库的读写分离。

添加依赖

加入Mybatis启动器,这里添加了Druid连接池、Oracle数据库驱动为例。

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

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
</dependency>

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
</dependency>

添加启动类

@EnableMybatis
@EnableTransactionManagement
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }

}

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }): 这里用到了双数据源,需要排除数据源的自动配置,如果只有一个数据源用Spring Boot的自动配置就行。

@EnableTransactionManagement:开启事务支持。

@EnableMybatis:开启Mybatis功能

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MybatisConfig.class)
public @interface EnableMybatis {

}

Mybatis配置类

@Configuration
@MapperScan(basePackages = DSConfig.BASE_PACKAGES)
public class MybatisConfig implements DSConfig {

    @Primary
    @Bean
    public DynamicDataSource dynamicDataSource(@Qualifier(DB_MASTER) DataSource master,
            @Qualifier(DB_SLAVE) DataSource slave) {
        Map<Object, Object> dsMap = new HashMap<>();
        dsMap.put(DB_MASTER, master);
        dsMap.put(DB_MASTER, slave);

        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setDefaultTargetDataSource(master);
        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }

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

    @Bean
    public SqlSessionFactory sqlSessionFactory(DynamicDataSource dynamicDataSource)
            throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dynamicDataSource);
        sessionFactory.setMapperLocations(
                ((ResourcePatternResolver) new PathMatchingResourcePatternResolver())
                        .getResources(DSConfig.MAPPER_LOCATIONS));
        return sessionFactory.getObject();
    }

}

DSConfig常量类:

public interface DSConfig {

    String DS_PREFIX = "spring.datasource";
    String DS_ACTIVE = "active";

    String DB_MASTER = "db-master";
    String DB_SLAVE = "db-slave";

    String DRUID = "druid";

    String DRUID_MONITOR_USERNAME = "spring.druid.username";
    String DRUID_MONITOR_PASSWORD = "spring.druid.password";
    String DRUID_MONITOR_URL = "/druid/*";
    String DRUID_FILTER_EXCLUSIONS = "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*";
    String DRUID_FILTER_URL = "/*";

    String BASE_PACKAGES = "com.example.**.mapper";
    String MAPPER_LOCATIONS = "mapper/**/*.xml";

}

连接池配置类

Druid连接池的自动配置类:

@Configuration
@Import({ PropertiesConfig.class })
@ConditionalOnClass(DruidDataSource.class)
@ConditionalOnProperty(prefix = DSConfig.DS_PREFIX, value = DSConfig.DS_ACTIVE, havingValue = DSConfig.DRUID)
public class DruidAutoConfig implements DSConfig {

    private Logger logger = LoggerUtils.getLogger(this);

    @Bean(name = DB_MASTER, initMethod = "init", destroyMethod = "close")
    public DataSource dataSourceMaster(DruidMasterProperties masterProperties) throws SQLException {
        logger.debug("master properties: {}", masterProperties.toString());

        DruidDataSource dds = new DruidDataSource();
        dds.setDriverClassName(masterProperties.getDriverClassName());
        dds.setUrl(masterProperties.getUrl());
        dds.setUsername(masterProperties.getUsername());
        dds.setPassword(masterProperties.getPassword());
        dds.setInitialSize(masterProperties.getInitialSize());
        dds.setMinIdle(masterProperties.getMinIdle());
        dds.setMaxActive(masterProperties.getMaxActive());
        dds.setMaxWait(masterProperties.getMaxWait());
        dds.setTimeBetweenEvictionRunsMillis(masterProperties.getTimeBetweenEvictionRunsMillis());
        dds.setMinEvictableIdleTimeMillis(masterProperties.getMinEvictableIdleTimeMillis());
        dds.setValidationQuery(masterProperties.getValidationQuery());
        dds.setTestOnBorrow(masterProperties.isTestOnBorrow());
        dds.setTestWhileIdle(masterProperties.isTestWhileIdle());
        dds.setTestOnReturn(masterProperties.isTestOnReturn());
        dds.setPoolPreparedStatements(masterProperties.isPoolPreparedStatements());
        dds.setMaxPoolPreparedStatementPerConnectionSize(
                masterProperties.getMaxPoolPreparedStatementPerConnectionSize());
        dds.setFilters(masterProperties.getFilters());

        return dds;
    }

    @Bean(name = DB_SLAVE, initMethod = "init", destroyMethod = "close")
    public DataSource dataSourceSlave(DruidSlaveProperties slaveProperties) throws SQLException {
        logger.debug("slave properties: {}", slaveProperties.toString());

        DruidDataSource dds = new DruidDataSource();
        dds.setDriverClassName(slaveProperties.getDriverClassName());
        dds.setUrl(slaveProperties.getUrl());
        dds.setUsername(slaveProperties.getUsername());
        dds.setPassword(slaveProperties.getPassword());
        dds.setInitialSize(slaveProperties.getInitialSize());
        dds.setMinIdle(slaveProperties.getMinIdle());
        dds.setMaxActive(slaveProperties.getMaxActive());
        dds.setMaxWait(slaveProperties.getMaxWait());
        dds.setTimeBetweenEvictionRunsMillis(slaveProperties.getTimeBetweenEvictionRunsMillis());
        dds.setMinEvictableIdleTimeMillis(slaveProperties.getMinEvictableIdleTimeMillis());
        dds.setValidationQuery(slaveProperties.getValidationQuery());
        dds.setTestOnBorrow(slaveProperties.isTestOnBorrow());
        dds.setTestWhileIdle(slaveProperties.isTestWhileIdle());
        dds.setTestOnReturn(slaveProperties.isTestOnReturn());
        dds.setPoolPreparedStatements(slaveProperties.isPoolPreparedStatements());
        dds.setMaxPoolPreparedStatementPerConnectionSize(
                slaveProperties.getMaxPoolPreparedStatementPerConnectionSize());
        dds.setFilters(slaveProperties.getFilters());

        return dds;
    }

    @Bean
    public ServletRegistrationBean druidServletRegistrationBean(EnvConfig env) {
        String username = env.getStringValue(DSConfig.DRUID_MONITOR_USERNAME);
        String password = env.getStringValue(DSConfig.DRUID_MONITOR_PASSWORD);
        return new ServletRegistrationBean(new DruidStatViewServlet(username, password),
                DSConfig.DRUID_MONITOR_URL);
    }

    @Bean
    public FilterRegistrationBean druidFilterRegistrationBean() {
        WebStatFilter wsf = new WebStatFilter();
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(wsf);
        filterRegistrationBean.setUrlPatterns(Arrays.asList(DSConfig.DRUID_FILTER_URL));
        filterRegistrationBean.setInitParameters(
                Collections.singletonMap("exclusions", DSConfig.DRUID_FILTER_EXCLUSIONS));
        return filterRegistrationBean;
    }

}

根据类路径下有DruidDataSource这个类即有Druid这个jar包和配置文件中spring.datasource.active=druid才开启对Druid连接池的自动配置。

导入的配置文件:

@Configuration
@ComponentScan(basePackages = "com.example.common.config.properties")
public class PropertiesConfig {

}

DruidMasterProperties、DruidSlaveProperties属性文件读取的配置省略。

连接池监控配置类:

public class DruidStatViewServlet extends StatViewServlet {

    private static final long serialVersionUID = 1L;

    private String username;
    private String password;

    @Override
    public String getInitParameter(String name) {
        if ("loginUsername".equals(name)) {
            return username;
        }

        if ("loginPassword".equals(name)) {
            return password;
        }

        return super.getInitParameter(name);
    }

    public DruidStatViewServlet(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

}

在META-INF/spring.factories中加入Druid自动配置映射:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.common.config.ds.DruidAutoConfig

切换数据源

切换数据源注解:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DS {
    String value() default DSConfig.DB_MASTER;
}

动态数据源类:

public class DynamicDataSource extends AbstractRoutingDataSource {

    private final Logger logger = LoggerUtils.getLogger(this);

    @Override
    protected Object determineCurrentLookupKey() {
        logger.debug("当前数据源为{}", DataSourceContextHolder.getDS());
        return DataSourceContextHolder.getDS();
    }

}

动态数据源AOP实现类:

@Aspect
@Component
public class DynamicDataSourceAspect {

    @Before("@annotation(DS)")
    public void beforeSwitchDS(JoinPoint point) {
        Class<?> className = point.getTarget().getClass();
        String methodName = point.getSignature().getName();
        Class<?>[] argClass = ((MethodSignature) point.getSignature()).getParameterTypes();
        String dataSource = DataSourceContextHolder.DEFAULT_DS;

        try {
            Method method = className.getMethod(methodName, argClass);
            if (method.isAnnotationPresent(DS.class)) {
                DS annotation = method.getAnnotation(DS.class);
                dataSource = annotation.value();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        DataSourceContextHolder.setDS(dataSource);
    }

    @After("@annotation(DS)")
    public void afterSwitchDS(JoinPoint point) {
        DataSourceContextHolder.clearDS();
    }

}

绑定当前线程数据源类:

public class DataSourceContextHolder {

    public static final String DEFAULT_DS = DSConfig.DB_MASTER;

    private static final ThreadLocal<String> DS_HOLDER = new ThreadLocal<>();

    public static void setDS(String dbType) {
        DS_HOLDER.set(dbType);
    }

    public static String getDS() {
        return (DS_HOLDER.get());
    }

    public static void clearDS() {
        DS_HOLDER.remove();
    }
}

-END-

不错,转发分享一下吧

往期推荐

【1】Spring Boot & Cloud 技术教程汇总

【2】京东购物车的 Java 架构实现及原理

【3】最新后端架构师技术图谱!附学习资料~

【4】Java序列化技术即将被废除!!

【5】Java 中的伪共享详解及解决方案

本文分享自微信公众号 - Java技术栈(javastack)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2018-06-08

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • JDK 的 3 个 bug 啊!

    如果两个变量中间隔了比较长的其它代码,很可能会导致开发人员将两者混淆,导致逻辑认知错误,从而写出或改出有问题的代码。

    Java技术栈
  • Spring Boot读取配置的几种方式

    读取application文件 在application.yml或者properties文件中添加: info.address=USA info.company...

    Java技术栈
  • 教你用 Netty 实现一个简单的 RPC!

    众所周知,dubbo 底层使用了 Netty 作为网络通讯框架,而 Netty 的高性能我们之前也分析过源码,对他也算还是比较了解了。今天我们就自己用 Nett...

    Java技术栈
  • Spring Boot 集成 Mybatis 实现双数据源

    这里用到了Spring Boot + Mybatis + DynamicDataSource配置动态双数据源,可以动态切换数据源实现数据库的读写分离。

    用户5224393
  • 前端新趋势,我是第一个吃螃蟹的人?

    这两年,前端圈真的很热闹,当你以为前端圈angular、react、vue等呈现三足鼎立之势时,其实冰山下面已经开始暗流涌动。当你还在为错过第三届VueConf...

    闰土大叔
  • 高通5G开始挤牙膏?骁龙865+发布,性能小幅提升,一加听了要笑,魅族看了会流泪

    相较于骁龙865,865 Plus不仅将SoC频率提升到了3.1GHz,而且支持WiFi-6E。

    量子位
  • Linux下sendmail发送邮件

    sendEmail的主页http://caspian.dotconf.net/menu/Software/SendEmail/ 下载地址http://caspi...

    我是李超人
  • AI从入门到放弃:CNN的导火索,用MLP做图像分类识别?

    图片标题会显示Accuracy(准确度),准确度的计算公式是: 识别正确图片数/图片总数。

    鹅厂优文
  • AI从入门到放弃2:CNN的导火索,用MLP做图像分类识别?

    阅读本文的基础:我会认为你对BP神经网络有充分的了解,熟读过我上一篇文章,本文会大量引用上一篇文章的知识以及代码。

    AI科技大本营
  • 亲测有效 | OpenVINO支持ONNX格式文件直接读取了

    OpenVINO 2020R04版本支持ONNX格式文件的直接读取了,使用的函数还是跟之前读取IR文件函数保持一致,只是第二个参数缺省为空时候,就会尝试读取ON...

    OpenCV学堂

扫码关注云+社区

领取腾讯云代金券