之前写过一篇jpa的多数据源配置,随着各种项目的不断开发,发现有时Mybatis操作数据更加方便,所以把Mybatis多数据源配置的方法也随手记录下,希望对你有帮助
1、先在application.yml 中配置多个数据库
server:
port: 13001
address: 127.0.0.1
spring:
application:
name: log_common
datasource:
user_center:
url: jdbc:mysql://localhost:3306/user_center?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 12345678
driver-class-name: com.mysql.jdbc.Driver
log_common:
url: jdbc:mysql://localhost:3306/log_common?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 12345678
driver-class-name: com.mysql.jdbc.Driver
# mybatis
mybatis:
configuration:
call-setters-on-nulls: true
map-underscore-to-camel-case: true
mapper:
mappers:
- tk.mybatis.mapper.common.Mapper
notEmpty: true
# 分页插件配置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
2、数据源连接配置
第一个数据源配置
LCDataSourceConfig.java
package com.haizhi.log_common.config;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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.core.io.support.ResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"com.haizhi.log_common.mapper.lc"}, sqlSessionTemplateRef = "logCommonSqlSessionTemplate")
public class LCDataSourceConfig {
@Value("${spring.datasource.log_common.url}")
private String url;
@Value("${spring.datasource.log_common.username}")
private String username;
@Value("${spring.datasource.log_common.password}")
private String password;
@Value("${spring.datasource.log_common.driver-class-name}")
private String driverClassName;
@Bean(name = "logCommonSource")
public DataSource userDataSource() {
// 此处用的是HikariDataSource,也可替换为druid
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(url);
hikariDataSource.setUsername(username);
hikariDataSource.setPassword(password);
hikariDataSource.setDriverClassName(driverClassName);
return hikariDataSource;
}
@Bean
public SqlSessionFactory logCommonSqlSessionFactory(@Qualifier("logCommonSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath*:mapper/lc/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate logCommonSqlSessionTemplate(@Qualifier("logCommonSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
return template;
}
}
上述注意两个地方
com.haizhi.log_common.mapper.lc : 第一个数据源的mapper路径classpath:mapper/lc/.xml : 第一个数据源的xml路径
第二个数据源配置
UCDataSourceConfig.java
package com.haizhi.log_common.config;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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.core.io.support.ResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"com.haizhi.log_common.mapper.uc"}, sqlSessionTemplateRef = "userCenterSqlSessionTemplate")
public class UCDataSourceConfig {
@Value("${spring.datasource.user_center.url}")
private String url;
@Value("${spring.datasource.user_center.username}")
private String username;
@Value("${spring.datasource.user_center.password}")
private String password;
@Value("${spring.datasource.user_center.driver-class-name}")
private String driverClassName;
@Bean(name = "userCenterSource")
@Primary //必须加此注解,不然报错,下一个类则不需要添加
public DataSource userDataSource() {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(url);
hikariDataSource.setUsername(username);
hikariDataSource.setPassword(password);
hikariDataSource.setDriverClassName(driverClassName);
return hikariDataSource;
}
@Bean
public SqlSessionFactory userCenterSqlSessionFactory(@Qualifier("userCenterSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath*:mapper/uc/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate userCenterSqlSessionTemplate(@Qualifier("userCenterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
return template;
}
}
上述注意两个地方
com.haizhi.log_common.mapper.uc : 第一个数据源的mapper路径classpath:mapper/uc/.xml : 第一个数据源的xml路径