一、环境和目录结构
1.1、环境springboot+mysql+mybatis
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>
1.2目录
二、yml配置两个MySQL数据源
url 用户名 密码 写上自己的
primary是第一个数据源
secondary是第二个数据源
spring:
datasource:
primary:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url:
username:
password:
secondary:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url:
username:
password:
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.suntaeiv.operation.operationplatform.pojo
logging:
level:
com.suntaeiv.operation.operationplatform.mapper: debug
三、配置数据源
写两个配置类如上图目录所示
DataSource1Config和DataSource2Config
关键点一:第19行代码. 看下面的图
@ConfigurationProperties(prefix = "spring.datasource.primary")
会指定我们的配置类的第一个数据源
关键点二:第30行代码
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources( "classpath:mapper/mapper1/*.xml"));
会指定哪一个xml使用我们的这套配置。说明:在关键点一时,我们就选择好了这个配置类的数据源了
package com.wfx.springbootredis.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;
@Configuration
@MapperScan(basePackages = "com.wfx.springbootredis.mapper.mapper1", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class DataSource1Config {
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(
"classpath:mapper/mapper1/*.xml"));
return bean.getObject();
}
@Bean(name = "primaryTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory)
throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
DataSource2Config和1基本相同只改我们的关键点即可
四、mapper(dao层)
我这里只写了一个根据id删除的例子
CarMapper2基本一样
五、mapper的实现xml
CarMapper2.xml基本一样
红色时因为我没有链接database
六、controller展示
直接在controller测试了
carMapper1对象的删除会使用第一个数据源
carMapper2对象的删除会使用第二个数据源
测试成功的截图我就不发了,是有效的。
希望对你有所帮助。