1,添加pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--注意配置版本号-->
<version>8.0.20</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2,配置数据源
package com.example.demo.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author haitao.li
* @title: DataSourceConfig
* @description:
* @date 2020/12/169:45
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "oneDataSource")
@ConfigurationProperties(prefix = "mybatis.one")
public DruidDataSource one() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "twoDataSource")
@ConfigurationProperties(prefix = "mybatis.two")
public DruidDataSource two() {
return DruidDataSourceBuilder.create().build();
}
}
3,配置mybatis
(1)第一个数据源
package com.example.demo.config.one;
import com.alibaba.druid.pool.DruidDataSource;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.annotation.Resource;
/**
* @author haitao.li
* @title: OneDataSource
* @description:
* @date 2020/12/169:44
*/
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.one"
, sqlSessionFactoryRef = "oneSqlSessionFactory"
, sqlSessionTemplateRef = "oneSqlSessionTemplate")
public class OneMybatisConfig {
@Resource(name = "oneDataSource")
private DruidDataSource dataSource;
@Bean("oneSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
try {
sqlSessionFactoryBean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/one/*Mapper.xml"));
return sqlSessionFactoryBean.getObject();
} catch (Exception e) {
return null;
}
}
@Bean("oneSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
(2)第二个数据源
package com.example.demo.config.two;
import com.alibaba.druid.pool.DruidDataSource;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.annotation.Resource;
/**
* @author haitao.li
* @title: OneDataSource
* @description:
* @date 2020/12/169:44
*/
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.two"
, sqlSessionFactoryRef = "twoSqlSessionFactory"
, sqlSessionTemplateRef = "twoSqlSessionTemplate")
public class TwoMybatisConfig {
@Resource(name = "twoDataSource")
private DruidDataSource dataSource;
@Bean("twoSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
try {
sqlSessionFactoryBean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/two/*Mapper.xml"));
return sqlSessionFactoryBean.getObject();
} catch (Exception e) {
return null;
}
}
@Bean("twoSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("twoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
4,yml配置
server:
port: 8080
mybatis:
one:
url: jdbc:mysql://localhost:3306/one?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 123456
type: com.mysql.cj.jdbc.Driver
two:
url: jdbc:mysql://localhost:3306/two?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 123456
type: com.mysql.cj.jdbc.Driver
如图:
运行结果: