前言申明
springBoot配置双数据源主要是配置类的书写和配置文件的书写,本文采用yml的形式进行配置,mapper和pojo两个个数据库都共用一个路径,dao层分开书写,各自配置文件中直接配置各自扫描路径即可。本文只讲解如何配置双数据源,如需配置单数据源的童鞋,可参看该链接:https://www.cnblogs.com/zblwyj/p/10668803.html
一、导入jar包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!-- sqlite3驱动包 -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
二、书写配置文件View Code
# Tomcat server: tomcat: uri-encoding: UTF-8 max-threads: 1000 min-spare-threads: 30 port: 8088 #spring spring: # 指定静态资源的路径 resources: static-locations: classpath:/static/,classpath:/views/,file:${web.upload},file:${web.ueditorUpload} # 数据源一 datasource: master: jdbc-url: jdbc:sqlite:D:/java/xy.db # jdbc-url: jdbc:sqlite:/usr/java/myfile/xy.db driver-class-name: org.sqlite.JDBC username: password: #数据源二 other: driver-class-name: org.sqlite.JDBC # 方式一: 引用外部文件 jdbc-url: jdbc:sqlite:D:/system.db username: password: # Mybatis配置 #mybatis: # mapperLocations: classpath:mapper/**/*.xml # configLocation: classpath:/mybatis.xml # sql打印 logging: level: debug level.com.xuanyin: debug # path: logs/ # file: admin.log
三、配置主数据源
package com.xuanyin.database; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; 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 com.github.pagehelper.PageInterceptor; import java.util.Properties; import javax.sql.DataSource; /** * 主数据源的配置 * @author Administrator * */ @Configuration @Primary @MapperScan(basePackages ="com.xuanyin.dao", sqlSessionFactoryRef = "masterSqlSessionFactory") public class MybatisDbMasterConfig { @Bean(name = "masterDataSource") @ConfigurationProperties(prefix = "spring.datasource.master") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "masterSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setTypeAliasesPackage("com.xuanyin.pojo"); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); //添加PageHelper插件 Interceptor interceptor = new PageInterceptor(); Properties properties = new Properties(); //数据库 properties.setProperty("helperDialect", "sqlite"); //是否将参数offset作为PageNum使用 properties.setProperty("offsetAsPageNum", "true"); //是否进行count查询 properties.setProperty("rowBoundsWithCount", "true"); //是否分页合理化 properties.setProperty("reasonable", "false"); interceptor.setProperties(properties); factoryBean.setPlugins(new Interceptor[] {interceptor}); return factoryBean.getObject(); } @Bean(name = "masterTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "masterSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate( @Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
四、从数据源的配置
package com.xuanyin.database; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.github.pagehelper.PageInterceptor; import java.util.Properties; import javax.sql.DataSource; /** * 从数据源的配置 * @author Administrator * */ @Configuration @MapperScan(basePackages = "com.xuanyin.system.dao", sqlSessionFactoryRef = "otherSqlSessionFactory") public class MybatisDbOtherConfig { @Bean(name = "otherDataSource") @ConfigurationProperties(prefix = "spring.datasource.other") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "otherTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("otherDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "otherSqlSessionFactory") public SqlSessionFactory basicSqlSessionFactory(@Qualifier("otherDataSource") DataSource basicDataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(basicDataSource); factoryBean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); //添加PageHelper插件 Interceptor interceptor = new PageInterceptor(); Properties properties = new Properties(); //数据库 properties.setProperty("helperDialect", "sqlite"); //是否将参数offset作为PageNum使用 properties.setProperty("offsetAsPageNum", "true"); //是否进行count查询 properties.setProperty("rowBoundsWithCount", "true"); //是否分页合理化 properties.setProperty("reasonable", "false"); interceptor.setProperties(properties); factoryBean.setPlugins(new Interceptor[] {interceptor}); return factoryBean.getObject(); } @Bean(name = "otherSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate( @Qualifier("otherSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
五、调用方法
由于2个数据源dao层位置分开存放,故此,调用时不用做任何操作,直接正常书写即可。