springboot_mybatis_sources
介绍
一个springboot mybatis配置多数据源的demo
软件架构
软件架构说明
使用说明
1.配置application.properties文件
这里有个踩坑点,url的配置一定要以spring.datasource.db1.jdbc-url的形式配置,不然启动时会找不到url
spring.datasource.db1.jdbc-url=jdbc:mysql://localhost/urun_statistic?zeroDateTimeBehavior=convertToNull&useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=UTC
spring.datasource.db1.username=
spring.datasource.db1.password=
spring.datasource.db1.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.db2.jdbc-url=jdbc:mysql://localhost/hangzhou?zeroDateTimeBehavior=convertToNull&useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=UTC
spring.datasource.db2.username=
spring.datasource.db2.password=
spring.datasource.db2.driverClassName=com.mysql.cj.jdbc.Driver
2.对不同的配置源有不同的config配置文件
对db1的配置,修改对应的mapper包位置,还有因为这个demo是用注解形式写语句的,没有xml文件,但是也是要配置mapper的xml形式文件位置
@Configuration
@MapperScan(basePackages = "com.urun.interceptor.hotanalysis.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class Db1DataSourceConfig {
@Bean("db1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db1") //读取application.yml中的配置参数映射成为一个对象
public DataSource getDb1DataSource(){
return DataSourceBuilder.create().build();
}
@Bean("db1SqlSessionFactory")
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/db1/*.xml"));
return bean.getObject();
}
@Bean("db1SqlSessionTemplate")
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
同理配置db2
@Configuration
@MapperScan(basePackages = “com.urun.interceptor.hotanalysis.mapper.db2”, sqlSessionFactoryRef = “db2SqlSessionFactory”)
public class Db2DataSourceConfig {
@Bean(“db2DataSource”)
@ConfigurationProperties(prefix = “spring.datasource.db2”) //读取application.yml中的配置参数映射成为一个对象
public DataSource getDb2DataSource(){
return DataSourceBuilder.create().build();
}
@Bean("db2SqlSessionFactory")
public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/db2/*.xml"));
return bean.getObject();
}
@Bean("db2SqlSessionTemplate")
public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
3.不同数据源的mapper层要分开写
4.最后记得一定要在resources建立一个mapping包存放db1和db2,即使使用注解开发也要带上,不然会报错的。
项目源码
gitee:https://gitee.com/MiddleZqz/springboot_mybatis_sources