【创建包:datasource,配置2个数据源(DataSource01主数据源)】
package com.java.test1.datasource; 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 javax.sql.DataSource; /** * @Author: TestRookie * @Date: 2021/9/4 21:20 * @Description: * */ @Configuration //注解到springboot容器中 @MapperScan(basePackages = "com.java.test1.test1.mapper",sqlSessionFactoryRef = "test1SqlSessionFactory") public class DataSource01 { /** * 返回test1数据库的数据源 */ @Bean(name = "test1DataSource") @Primary//主数据源,一个应用只能有一个主数据源 @ConfigurationProperties(prefix = "spring.datasource.test1") public DataSource dataSource(){ return DataSourceBuilder.create().build(); } /** * 返回test1数据库的会话工厂 */ @Bean(name = "test1SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("test1DataSource") DataSource ds) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(ds); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/test1/*.xml")); return bean.getObject(); } /** * 返回test1数据库的事务 */ @Bean(name = "test1TransactionManager") @Primary //主事务 public DataSourceTransactionManager transactionManager(@Qualifier("test1DataSource") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } /** * 返回test1数据库的会话模板 */ @Bean(name = "test1SqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate( @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } }
package com.java.test1.datasource; 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 javax.sql.DataSource; /** * @Author: TestRookie * @Date: 2021/9/4 21:20 * @Description: * */ @Configuration //注解到springboot容器中 @MapperScan(basePackages = "com.java.test1.test2.mapper",sqlSessionFactoryRef = "test2SqlSessionFactory") public class DataSource02 { /** * 返回test2数据库的数据源 */ @Bean(name = "test2DataSource") @ConfigurationProperties(prefix = "spring.datasource.test2") public DataSource dataSource(){ return DataSourceBuilder.create().build(); } /** * 返回test2数据库的会话工厂 */ @Bean(name = "test2SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("test2DataSource") DataSource ds) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(ds); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/test2/*.xml")); return bean.getObject(); } /** * 返回test2数据库的事务 */ @Bean(name = "test2TransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("test2DataSource") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } /** * 返回test2数据库的会话模板 */ @Bean(name = "test2SqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate( @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } }
【配置文件】
#tomcat端口号 server.port=8080 #spring.datasource.url=jdbc:mysql://localhost:3306/study2020 #spring.datasource.driver-class-name=com.mysql.jdbc.Driver #spring.datasource.username=root #spring.datasource.password=root #数据库配置 spring.datasource.test1.driver-class-name=com.mysql.jdbc.Driver spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8 spring.datasource.test1.username=root spring.datasource.test1.password=root spring.datasource.test2.driver-class-name=com.mysql.jdbc.Driver spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8 spring.datasource.test2.username=root spring.datasource.test2.password=root mybatis.configuration.map-underscore-to-camel-case=true #mybatis 配置 # 配置映射文件加载,使用自定义数据源时此配置无效 #mybatis.mapper-locations=classpath*:mappers/*.xml spring.resources.static-locations:classpath:/static/,classpath:/templates/