Springboot+Mybatis多数据源

本文详细介绍如何在SpringBoot项目中配置MyBatis的多数据源,包括使用C3P0和Druid连接池,以及如何设置SessionFactory和DAO扫描路径。

Springboot+Mybatis多数据源

多数据源配置

创建一个Spring配置类,定义两个DataSource用来读取application.properties中的不同配置。如下例子中,第一数据源配置为ms.db.开头的配置,第二数据源配置为ms.db2.开头的配置。此处直接配置了C3P0的连接池
代码片.

package com.app.config.mybatis;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.beans.PropertyVetoException;
/**
 * 数据源
 * ClassName: DBconfig <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2018年4月22日 下午6:13:15 <br/>
 *
 * @author lk
 * @version 
 * @since JDK 1.8
 */
@Configuration
public class DBconfig {
    @Autowired
    private Environment env;

    @Bean(name="msDbdataSource")
    public ComboPooledDataSource msDbdataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(env.getProperty("ms.db.driverClassName"));
        dataSource.setJdbcUrl(env.getProperty("ms.db.url"));
        dataSource.setUser(env.getProperty("ms.db.username"));
        dataSource.setPassword(env.getProperty("ms.db.password"));
        dataSource.setMaxPoolSize(60);
        dataSource.setMinPoolSize(5);
        dataSource.setInitialPoolSize(10);
        dataSource.setMaxIdleTime(300);
        dataSource.setAcquireIncrement(5);
        dataSource.setIdleConnectionTestPeriod(60);
        return dataSource;
    }

    @Bean(name="msDb2dataSource")
    public ComboPooledDataSource msDb2dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(env.getProperty("ms.db2.driverClassName"));
        dataSource.setJdbcUrl(env.getProperty("ms.db2.url"));
        dataSource.setUser(env.getProperty("ms.db2.username"));
        dataSource.setPassword(env.getProperty("ms.db2.password"));
        dataSource.setMaxPoolSize(60);
        dataSource.setMinPoolSize(5);
        dataSource.setInitialPoolSize(10);
        dataSource.setMaxIdleTime(300);
        dataSource.setAcquireIncrement(5);
        dataSource.setIdleConnectionTestPeriod(60);
        return dataSource;
    }
}

对应的application.properties配置如下:
application.properties.


ms.db.driverClassName = com.mysql.jdbc.Driver
ms.db.url = jdbc:mysql://localhost:3306/layui
ms.db.username = root
ms.db.password = root

ms.db2.driverClassName = com.mysql.jdbc.Driver
ms.db2.url = jdbc:mysql://localhost:3306/jvpwbb
ms.db2.username = root
ms.db2.password = root

有想配置阿里druid的也可以将dataSource替换一下就行

DruidDataSource dataSource = new DruidDataSource();
        //dataSource.setDriverClassName(driverClassName);//如果不配置druid会根据url自动识别dbType,然后选择相应的driverClassName
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setValidationQuery("SELECT 1");//用来检测连接是否有效
        dataSource.setTestOnBorrow(false);//申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
        dataSource.setTestOnReturn(false);//归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
        //申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
        dataSource.setTestWhileIdle(true);//如果检测失败,则连接将被从池中去除
        dataSource.setTimeBetweenEvictionRunsMillis(600000);
        dataSource.setMaxActive(20);
        dataSource.setInitialSize(10);
        return dataSource;

详情配置可以参考一下,感谢这位博主的分享 https://blog.youkuaiyun.com/yzh_1346983557/article/details/88547234
下面只需要配置多个SessionFactory

package com.app.config.mybatis;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * mybatis配置
 * ClassName: MyBatisConfig <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2018年4月22日 下午6:13:31 <br/>
 *
 * @author lk
 * @version 
 * @since JDK 1.8
 */
@Configuration
@ComponentScan
public class MyBatisConfig {

    @Autowired
    private DataSource msDbdataSource;

    @Autowired
    private DataSource msDb2dataSource;

    @Bean(name = "msDbsqlSessionFactory")
    public SqlSessionFactoryBean sqlSessionFactory(ApplicationContext applicationContext) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(msDbdataSource);
        // sessionFactory.setPlugins(new Interceptor[]{new PageInterceptor()});
        sessionFactory.setMapperLocations(applicationContext.getResources("classpath*:mapper/*.xml"));
        return sessionFactory;
    }

    @Bean(name = "msDb2sqlSessionFactory")
    public SqlSessionFactoryBean testsqlSessionFactory(ApplicationContext applicationContext) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(msDb2dataSource);
        // sessionFactory.setPlugins(new Interceptor[]{new PageInterceptor()});
        sessionFactory.setMapperLocations(applicationContext.getResources("classpath*:mapper/*.xml"));
        return sessionFactory;
    }
}

下面继续配置mybatis包扫描的路径 也就是dao所在的配置

package com.app.config.mybatis;

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis扫描路径
 * ClassName: MyBatisScannerConfig <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2018年4月22日 下午6:13:48 <br/>
 *
 * @author lk
 * @version 
 * @since JDK 1.8
 */
@Configuration
public class MyBatisScannerConfig {
    @Bean
    public MapperScannerConfigurer MapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.app.layui.dao");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }
    /*@Bean
    public MapperScannerConfigurer MapperScannerConfigurertest() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.qiangang.daotwo");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("testsqlSessionFactory");
        return mapperScannerConfigurer;
    }*/
}

最后就大公告成了,大家可以在指定的2个不同的dao下面写个测试一下。大家遇到问题可以留言问我。大家一起努力加油
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值