Springboot mybais配置多数据源过程解析

这篇文章主要介绍了Springboot+mybais配置多数据源过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、分包方式实现:

1、在application.properties中配置两个数据库

#druid连接池

#dataSoureOne(这里是我本地的数据源)

spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.one.driver-class-name=com.MysqL.jdbc.Driver

spring.datasource.one.jdbc-url=jdbc:MysqL://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC

spring.datasource.one.username=root

spring.datasource.one.password=root

#dataSoureTwo(这里是我们服务器的数据源)

spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.two.driver-class-name=com.MysqL.jdbc.Driver

spring.datasource.two.jdbc-url=jdbc:MysqL://xx.xxx.xx.xxx:3306/kds_master_info?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC

spring.datasource.two.username=root

spring.datasource.two.password=KDSmaster123

2、建立连个数据源的配置文件

注意下面DataSource包引入的是import javax.activation.DataSource;

@Configuration// 配置mybatis的接口类放的地方 @MapperScan(basePackages = "com.example.mybatis.mapper",sqlSessionFactoryRef = "sqlSessionFactoryOne") public class DataSourceConfigOne { @Bean(name = "dataSourceOne") @Primary// 表示这个数据源是认数据源 // 读取application.properties中的配置参数映射成为一个对象,prefix表示参数的前缀 @ConfigurationProperties(prefix = "spring.datasource.one") public DataSource dataSourceOne() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactoryOne") @Primary public sqlSessionFactory sqlSessionFactoryOne(@Qualifier("dataSourceOne") DataSource datasource)throws Exception { sqlSessionfactorybean bean = new sqlSessionfactorybean(); bean.setDataSource(datasource); bean.setMapperLocations( // 设置mybatis的xml所在位置 new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); bean.getobject().getConfiguration().setMapUnderscoreToCamelCase(true);return bean.getobject(); } @Primary public sqlSessionTemplate sqlsessiontemplateOne(@Qualifier("sqlsessiontemplateOne") sqlSessionFactory sessionfactory) { return new sqlSessionTemplate(sessionfactory); } }

@Configuration @MapperScan(basePackages = "com.example.mybatis.mapper2",sqlSessionFactoryRef = "sqlSessionFactoryTwo") public class DataSourceConfigTwo { @Bean(name = "dataSourceTwo") // 读取application.properties中的配置参数映射成为一个对象,prefix表示参数的前缀 @ConfigurationProperties(prefix = "spring.datasource.two") public DataSource dataSourceTwo() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactoryTwo") public sqlSessionFactory sqlSessionFactoryTwo(@Qualifier("dataSourceTwo") DataSource datasource)throws Exception { sqlSessionfactorybean bean = new sqlSessionfactorybean(); bean.setDataSource((javax.sql.DataSource) datasource); bean.setMapperLocations( // 设置mybatis的xml所在位置 new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/*.xml")); bean.getobject().getConfiguration().setMapUnderscoreToCamelCase(true); return bean.getobject(); } public sqlSessionTemplate sqlsessiontemplateTwo(@Qualifier("sqlsessiontemplateTwo") sqlSessionFactory sessionfactory) { return new sqlSessionTemplate(sessionfactory); } }

注意:1、@Primary这个注解必须要加,因为不加的话spring将分不清楚那个为主数据源(认数据源)2、mapper的接口、xml形式以及dao层都需要两个分开,目录如图:

3、bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“XXXX”));mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致的)

4、在service层中根据不同的业务注入不同的dao层:

5.开始我启动项目并访问接口会报错,查看了半小时才发现,是下划线与驼峰映射失败,这个要在sqlSessionFactoryOne和sqlSessionFactoryTwo里面添加一行bean.getobject().getConfiguration().setMapUnderscoreToCamelCase(true);才可以,然后继续访问,又报错Failed to obtain JDBC Connection; nested exception is com.MysqL.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure.后来查询得知,要在上面的数据库连接url中将&useSSL=true改为&useSSL=false

最后测试一下,两个数据库userInfo和user表的数据都显示出来了:

userInfo:

user:

最后还有一个错误忘了补充,在这里补充一下,我的springboot是2.x版本,在配置单个数据源时候,数据库连接的url是spring.datasource.url=xxx,这样没有问题,但是在配置多数据源的时候spring.datasource.one.url和spring.datasource.two.url会报错jdbcUrl is required with driverClassName.将spring.datasource.one.url和spring.datasource.two.url中的url改成spring.datasource.one.jdbc-url,也就是将url改成jdbc-url即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

Java中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...