一个基于springboot的多数据源(mysql、sqlserver)项目,先看看项目结构,注意dao层
多数据源mysql配置代码:
package com.douzi.robotcenter.core; 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.core.io.support.ResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; /** * mysql */ @Configuration @MapperScan( basePackages = {"com.douzi.robotcenter.mysql.dao"}, sqlSessionFactoryRef = "mysqlSqlSessionFactory", sqlSessionTemplateRef = "mysqlSqlSessionTemplate" ) @EnableTransactionManagement(proxyTargetClass = true) public class MysqltMybatisConfig { @Bean @Primary //必须加此注解,不然报错,下一个类则不需要添加 @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource mysqlDataSource() { return DataSourceBuilder.create().build(); } @Bean public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); return template; } @Bean public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { bean.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } //使用方式:@Transactional(默认msyql主事物) @Bean @Primary public PlatformTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource mysqlDataSource) { return new DataSourceTransactionManager(mysqlDataSource); } }
多数据源sqlserver配置代码:
package com.douzi.robotcenter.core; 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.core.io.support.ResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; /** * sqlserver */ @Configuration @MapperScan( basePackages = {"com.douzi.robotcenter.sqlserver.dao"}, sqlSessionFactoryRef = "sqlserverSqlSessionFactory", sqlSessionTemplateRef = "sqlserverSqlSessionTemplate" ) @EnableTransactionManagement(proxyTargetClass = true) public class SqlserverMybatisConfig { @Bean(name = "sqlserverDataSource") @ConfigurationProperties(prefix = "spring.datasource.second") // prefix值必须是application.properteis中对应属性的前缀 public DataSource sqlserverDataSource() { return DataSourceBuilder.create().build(); } @Bean public SqlSessionFactory sqlserverSqlSessionFactory(@Qualifier("sqlserverDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { bean.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Bean public SqlSessionTemplate sqlserverSqlSessionTemplate(@Qualifier("sqlserverSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory return template; } //使用方式:@Transactional(transactionManager="sqlserverTransactionManager") @Bean public PlatformTransactionManager sqlserverTransactionManager(@Qualifier("sqlserverDataSource") DataSource oracleDataSource) { return new DataSourceTransactionManager(oracleDataSource); } }
之前dao层都是放一个目录下的:发现本地运行没问题,放到Linux上就会出现No statement Error(找不到xml),后来把dao层分两个目录来存放,然后扫描不同的包,就OK了
源码地址:https://github.com/wangfajun/robotCenter