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.csdn.net/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下面寫個測試一下。大家遇到問題可以留言問我。大家一起努力加油