一、配置单数据源
在学习配置多数据源之前,先来复习一下单数据源的配置。
创建好 SpringBoot 项目,在pom.xml
添加需要的依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
由于我们将 mapper.xml 和 mapper.java 放在相同的 java 目录,所以还要添加如下配置,避免打包时java目录下的xml文件被忽略掉:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include> **/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
在项目启动类上使用MapperScan
注解配置要扫描的 mapper 包路径,需要将 mapper 文件放在这个目录下:
@SpringBootApplication
@MapperScan("com.sh.mybatis.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
然后在application.properties
进行数据库连接的相关配置:
spring.datasource.url=jdbc:mysql://localhost:3306/mytest?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B0
spring.datasource.username=root
spring.datasource.password=shehuan
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 如果mapper.xml是放置在resources下边的mapper目录可以配置这个
# mybatis.mapper-locations=classpath:mapper/*.xml
# 打印sql
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
二、配置多数据源
在单数据源的基础上我们完成多数据源的配置,我们以两个数据源为例。
首先pom.xml
不用做修改。
取消掉启动类上的MapperScan
注解。
然后就是修改application.properties
,添加两个数据源:
# 数据源1
spring.datasource.one.url=jdbc:mysql://localhost:3306/mytest?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B0
spring.datasource.one.username=root
spring.datasource.one.password=shehuan
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源2
spring.datasource.two.url=jdbc:mysql://localhost:3306/mytest2?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B0
spring.datasource.two.username=root
spring.datasource.two.password=shehuan
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.driver-class-name=com.mysql.cj.jdbc.Driver
接下来通过Configuration
配置类的像是来初始化两个数据源:
@Configuration
public class DataSourceConfig {
@Bean(name = "dsOne")
@ConfigurationProperties(prefix = "spring.datasource.one")
DataSource dsOne() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "dsTwo")
@ConfigurationProperties(prefix = "spring.datasource.two")
DataSource dsTwo() {
return DruidDataSourceBuilder.create().build();
}
}
由于是多数据源,我们无法在启动类配置MapperScan
,所以需要为每个数据源单独配置要扫描的 mapper 文件目录,以及创建SqlSessionFactory
、SqlSessionTemplate
。
@Configuration
@MapperScan(basePackages = "com.sh.mybatis.mapper", sqlSessionFactoryRef = "sqlSessionFactory1", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class MyBatisConfigOne {
@Autowired
@Qualifier("dsOne")
DataSource dsOne;
@Bean
SqlSessionFactory sqlSessionFactory1() {
SqlSessionFactory sqlSessionFactory = null;
try {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dsOne);
sqlSessionFactory = bean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return sqlSessionFactory;
}
@Bean
SqlSessionTemplate sqlSessionTemplate1() {
return new SqlSessionTemplate(sqlSessionFactory1());
}
}
@Configuration
@MapperScan(basePackages = "com.sh.mybatis.mapper2", sqlSessionFactoryRef = "sqlSessionFactory2", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class MyBatisConfigTwo {
@Autowired
@Qualifier("dsTwo")
DataSource dsTwo;
@Bean
SqlSessionFactory sqlSessionFactory2() {
SqlSessionFactory sessionFactory = null;
try {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dsTwo);
sessionFactory = bean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return sessionFactory;
}
@Bean
SqlSessionTemplate sqlSessionTemplate2() {
return new SqlSessionTemplate(sqlSessionFactory2());
}
}
最后只需要在com.sh.mybatis.mapper
、com.sh.mybatis.mapper2
两个包下分别放置两个数据源对应的 mapper.java、mapper.xml 文件即可,后边的用法就没有什么特别之处了。
网友评论