1.配置文件
spring.datasource.a.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.a.jdbc-url=jdbc:mysql://127.0.0.1:3306/test_a?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.a.username=root
spring.datasource.a.password=root
spring.datasource.b.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.b.jdbc-url=jdbc:mysql://127.0.0.1:3306/test_b?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.b.username=root
spring.datasource.b.password=root
2.数据源配置类
对应a数据源的配置类
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
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;
@Configuration
@MapperScan(basePackages = "com.xxx.test.mapper.a", sqlSessionFactoryRef = "testASqlSessionFactory")
public class DataSourceConfigA {
//自动注入DataSource的属性
@Bean(name = "testADataSource")
@ConfigurationProperties(prefix = "spring.datasource.a")
public DataSource getDateSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "testASqlSessionFactory")
public SqlSessionFactory getSqlSessionFactory(@Qualifier("testADataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
//设置model的全包名
bean.setTypeAliasesPackage("com.xxx.test.model");
//这个必须加,不然打包后配置的TypeAliasesPackage不起作用,如果xml中用到resultType="类名"会报错找不到model中的类
bean.setVfs(SpringBootVFS.class);
return bean.getObject();
}
@Bean("testASqlSessionTemplate")
public SqlSessionTemplate getSqlsessiontemplate(
@Qualifier("testASqlSessionFactory") SqlSessionFactory sessionfactory) {
//开启从表的字段名到类的属性名自动映射
sessionfactory.getConfiguration().setMapUnderscoreToCamelCase(true);
return new SqlSessionTemplate(sessionfactory);
}
}
对应b数据源的配置类
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
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;
@Configuration
@MapperScan(basePackages = "com.xxx.test.mapper.b", sqlSessionFactoryRef = "testBSqlSessionFactory")
public class DataSourceConfigB {
@Bean(name = "testADataSource")
@ConfigurationProperties(prefix = "spring.datasource.b")
public DataSource getDateSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "testBSqlSessionFactory")
public SqlSessionFactory getSqlSessionFactory(@Qualifier("testBDataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setTypeAliasesPackage("com.xxx.test.model");
//这个必须加,不然打包后配置的TypeAliasesPackage不起作用,如果xml中用到resultType="类名"会报错找不到model中的类
bean.setVfs(SpringBootVFS.class);
return bean.getObject();
}
@Bean("testBSqlSessionTemplate")
public SqlSessionTemplate getSqlsessiontemplate(
@Qualifier("testBSqlSessionFactory") SqlSessionFactory sessionfactory) {
sessionfactory.getConfiguration().setMapUnderscoreToCamelCase(true);
return new SqlSessionTemplate(sessionfactory);
}
}
3.项目包结构
/src/main/java
com.xxx.test
mapper
a
TestA.java
TestA.xml
b
TestB.java
TestB.xml
model
A.java
B.java
4. 注意事项
//这个必须加,不然打包后配置的TypeAliasesPackage不起作用,如果xml中用到resultType="类名"会报错找不到model中的类
bean.setVfs(SpringBootVFS.class);