新建一个spring boot项目。然后贴代码
配置文件:application.properties
bootapp.name=SpringBoot bootapp.description=${bootapp.name} server.port=8080 spring.profiles.active=dev server.tomcat.uri-encoding=utf-8 server.servlet.encoding.force=true server.servlet.encoding.charset=UTF-8 server.servlet.encoding.enabled=true spring.datasource.primary.jdbc-url=jdbc:mysql://127.0.0.1:3306/db2?characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.primary.username=root spring.datasource.primary.password=123456 spring.datasource.datasource2.jdbc-url=jdbc:mysql://127.0.0.1:3306/db3?characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.datasource2.driver-class-name=com.mysql.jdbc.Driver spring.datasource.datasource2.username=root spring.datasource.datasource2.password=123456 spring.datasource.datasource3.jdbc-url=jdbc:kingbase8://10.10.5.189:54321/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8 spring.datasource.datasource3.driver-class-name=com.kingbase8.Driver spring.datasource.datasource3.username=KCDB spring.datasource.datasource3.password=1kc@QAZ # mybatis mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.wdbyte.domain
目录结构:
PrimaryDataSourceConfig类
package com.wdbyte.config; import javax.sql.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; /** * 主数据源配置 * */ @Configuration @MapperScan(basePackages = {"com.wdbyte.mapper.primary"}, sqlSessionFactoryRef = "sqlSessionFactory") public class PrimaryDataSourceConfig { @Bean(name = "dataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") @Primary public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); return bean.getObject(); } @Bean(name = "transactionManager") @Primary public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplate") @Primary public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
SecondData2SourceConfig类:
package com.wdbyte.config; 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 guoshuai * @date 2022年01月05日 17:37 */ @Configuration @MapperScan(basePackages = {"com.wdbyte.mapper.datasource3"}, sqlSessionFactoryRef = "sqlSessionFactory3") public class SecondData2SourceConfig { @Bean(name = "dataSource3") @ConfigurationProperties(prefix = "spring.datasource.datasource3") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactory3") public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource3") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); return bean.getObject(); } @Bean(name = "transactionManager3") public DataSourceTransactionManager transactionManager(@Qualifier("dataSource3") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplate3") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory3") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
SecondDataSourceConfig类:
package com.wdbyte.config; import javax.sql.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.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; /** * 第二个数据源配置 * * @author * @website: https://www.wdbyte.com * @date 2020/12/19 */ @Configuration @MapperScan(basePackages = {"com.wdbyte.mapper.datasource2"}, sqlSessionFactoryRef = "sqlSessionFactory2") public class SecondDataSourceConfig { @Bean(name = "dataSource2") @ConfigurationProperties(prefix = "spring.datasource.datasource2") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactory2") public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); return bean.getObject(); } @Bean(name = "transactionManager2") public DataSourceTransactionManager transactionManager(@Qualifier("dataSource2") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplate2") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
其他的看gitee吧 懒得复制了
https://gitee.com/work123gs/springboot-data-mybatis-multiple-datasource.git