准备
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.4</version>
</dependency>
结构
数据源
单个数据源
Spring Boot默认已经封装好了JDBC(或SqlSession等),使用时只需要配置相关属性就行了。
application.properties
# mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC&useSSL=false
spring.datasource.username=username
spring.datasource.password=password
# mybatis config
mybatis.type-aliases-package=com.git.lee.spring.boot.example.model
mybatis.mapper-locations=classpath*:com/git/lee/spring/boot/example/mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
Application
@SpringBootApplication
@MapperScan("com.git.lee.spring.boot.example.mapper")
public class ExApplication {
public static void main(String[] args) {
SpringApplication.run(ExApplication.class, args);
}
}
多个数据源
不同数据源创建不同的DataSource(SqlSessionFactory),Mapper Scan Package也不一样。
application.properties
# mysql
first.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
first.datasource.url=jdbc:mysql://localhost:3306/first?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC&useSSL=false
first.datasource.username=username
first.datasource.password=password
second.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
second.datasource.url=jdbc:mysql://localhost:3306/second?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC&useSSL=false
second.datasource.username=username
second.datasource.password=password
# mybatis config
mybatis.type-aliases-package=com.git.lee.spring.boot.example.model
mybatis.mapper-locations=classpath*:com/git/lee/spring/boot/example/mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
DataSource Configuration
@Configuration
@MapperScan(value = "com.git.lee.spring.boot.example.mapper.first", sqlSessionFactoryRef = "firstSqlSessionFactory")
@EnableConfigurationProperties(MybatisProperties.class)
public class FirstDataSourceConfig {
private MybatisProperties mybatisProperties;
public FirstDataSourceConfig(MybatisProperties properties) {
this.mybatisProperties = properties;
}
@Bean(name = "firstDataSource")
@Primary
@ConfigurationProperties(prefix = "first.datasource")
public DataSource getDataSource() {
return new org.apache.tomcat.jdbc.pool.DataSource();
}
@Bean(name = "firstSqlSessionFactory")
@Primary
public SqlSessionFactory getSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(getDataSource());
sessionFactory.setMapperLocations(mybatisProperties.resolveMapperLocations());
sessionFactory.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());
sessionFactory.setConfiguration(SqlSessionFactoryConfigUtil.clone(mybatisProperties.getConfiguration()));
return sessionFactory.getObject();
}
}
其他数据源同理。只需要把bean的name改下,MapperScan的value改下;还有把@Primary去掉(只能有一个);
这样,不同的mapper就可以操作不同的数据库了。