第一步:创建SpringBoot项目
Maven依赖:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
8.0.20
org.mybatis
mybatis
3.5.4
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.3
在application.yml配置文件中新增两个数据源
server:
port: 80
servlet:
context-path: /mds
spring:
datasource:
test1:
driverClassName: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/db_test?useSSL=false&serverTimezone=UTC&user=root&password=&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true
password: root
username: root
test2:
driverClassName: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&user=root&password=&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true
password: root
username: root
第二步:创建Mapper
在包com.hc.test1.mapper下创建DeptMapper1.java
@Mapper
public interface DeptMapper1 {
@Insert("insert into tb_dept(dname,loc) values(#{dname},#{loc})")
void insert(Dept dept);
@Select("select * from tb_dept")
List selectAllDept();
}
在包com.hc.test2.mapper下创建DeptMapper2.java
@Mapper
public interface DeptMapper2 {
@Insert("insert into tb_dept(dname,loc) values(#{dname},#{loc})")
void insert(Dept dept);
@Select("select * from tb_dept")
List selectAllDept();
}
第三步:配置文件中指定数据源
application.ym中test1所对应的配置文件DataSource1Config
@Configuration // 注册到springboot容器中
@MapperScan(basePackages = "com.hc.test1.mapper", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {
// 创建数据源
@Bean(name = "test1DataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.test1")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
// 创建会话工厂
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(
@Qualifier("test1DataSource") DataSource dataSource)throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
return factory.getObject();
}
// 创建模板
@Bean(name = "test1SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
//创建事物管理
@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource ds) {
return new DataSourceTransactionManager(ds);
}
}
application.ym中test2所对应的配置文件DataSource2Config
@Configuration // 注册到springboot容器中
@MapperScan(basePackages = "com.hc.test2.mapper", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {
//创建数据源
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
//创建会话工厂
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
return factory.getObject();
}
//创建模板
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
//创建事务管理
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource ds) {
return new DataSourceTransactionManager(ds);
}
}
第四步:测试代码
DeptMapper1Test
@SpringBootTest
public class DeptMapper1Test {
@Resource
private DeptMapper1 deptMapper1;
@Test
public void insert() {
Dept dept = new Dept(10, "aaa", "aaaaaaaa");
deptMapper1.insert(dept);
System.out.println(deptMapper1);
}
@Test
public void selectAllDept() {
List depts = deptMapper1.selectAllDept();
depts.forEach(System.out::println);
}
}
DeptMapper2Test
@SpringBootTest
class DeptMapper2Test {
@Resource
private DeptMapper2 deptMapper2;
@Test
void insert() {
}
@Test
void selectAllDept() {
deptMapper2.selectAllDept().forEach(System.out::println);
}
}