本文主要介绍如何在一个springboot项目配置两个数据源(mysql和oracle);
1、引进相关依赖
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope></dependency><!--oracle 驱动 --><dependency> <groupid>com.oracle</groupid> <artifactid>ojdbc6</artifactid> <version>11.1.0.7.0</version></dependency>
当ojdbc驱动包版本过低,会报如下图所示错误,即驱动jar与数据库版本不兼容:
2、于applicationcontext.yml中配置数据源连接参数:
spring: datasource: base: driver-class-name: com.mysql.jdbc.driver jdbc-url: ${base.db.url} username: ${base.db.username} password: ${base.db.password} oa: driver-class-name: oracle.jdbc.driver.oracledriver jdbc-url: ${oa.db.url} username: ${oa.db.username} password: ${oa.db.password} hikari: max-lifetime: 60000 login-timeout: 5 validation-timeout: 3000 connection-timeout: 60000 idle-timeout: 60000
3、多数据源配置文件,读取对应连接参数
package com.**.config;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;import org.springframework.context.annotation.primary;import javax.sql.datasource;public class multidatasourceconfig { @bean (name = "primarydatasource") @qualifier("primarydatasource") @primary //定义主数据源 @configurationproperties (prefix = "spring.datasource.base") public datasource primarydatasource () { return datasourcebuilder.create().build (); } @bean (name = "secondarydatasource") @qualifier ("secondarydatasource") @configurationproperties (prefix = "spring.datasource.oa") public datasource secondarydatasource () { return datasourcebuilder.create().build (); }}
3、主数据源配置,指定扫描对应mapper文件以及对应xml文件,在使用mapper包下的mapper文件时会自动使用对应sql会话
package com.**.config;import org.apache.ibatis.session.sqlsessionfactory;import org.mybatis.spring.sqlsessionfactorybean;import org.mybatis.spring.sqlsessiontemplate;import org.mybatis.spring.annotation.mapperscan;import org.springframework.beans.factory.annotation.qualifier;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;import org.springframework.context.annotation.primary;import org.springframeworkcore.io.support.pathmatchingresourcepatternresolver;import org.spr ingframework.jdbc.datasource.datasourcetransactionmanager;import org.springframework.transaction.platformtransactionmanager;import javax.sql.datasource;@configuration@mapperscan (basepackages = "com.**.mapper", sqlsessiontemplateref = "primarysqlsessiontemplate")public class sqlsessiontemplate1 { @bean (name = "primarysqlsessionfactory") @primary public sqlsessionfactory primarysqlsessionfactory (@qualifier("primarydatasource") datasource datasource) throws exception{ sqlsessionfactorybean bean = new sqlsessionfactorybean (); bean.setdatasource (datasource); bean.setmapperlocations (new pathmatchingresourcepatternresolver().getresources (locationpattern: "classpath: mybatis/mapper/*. xml")); return bean.getobject(); } /** * 配置声明式事务管理器 */ @bean (name = "primarytransactionmanager") @primary public platformtransactionmanager primarytransactionmanager (@qualifier("primarydatasource") datasource datasource) { return new datasourcetransactionmanager (datasource); } @bean (name = "primarysqlsessiontemplate") @primary public sqlsessiontemplatel primarysqlsessiontemplate(@qualifier("primarysqlsesionfactory") sqlsessionfactory sqlsessionfactory) throws exception { return new sqlsessiontemplatel(sqlsessionfactory); }}
4、第二数据源配置,指定扫描对应mapper文件以及对应xml文件,在使用mapper包下的mapper文件时会自动使用对应sql会话
package com.**.config;import org.apache.ibatis.session.sqlsessionfactory;import org.mybatis.spring.sqlsessionfactorybean;import org.mybatis.spring.sqlsessiontemplate;import org.mybatis.spring.annotation.mapperscan;import org.springframework.beans.factory.annotation.qualifier;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;import org.springframework.context.annotation.secondary;import org.springframeworkcore.io.support.pathmatchingresourcepatternresolver;import org.spr ingframework.jdbc.datasource.datasourcetransactionmanager;import org.springframework.transaction.platformtransactionmanager;import javax.sql.datasource;@configuration@mapperscan (basepackages = "com.**.oraclemapper", sqlsessiontemplateref = "secondarysqlsessiontemplate")public class sqlsessiontemplate2 { @bean (name = "secondarysqlsessionfactory") public sqlsessionfactory secondarysqlsessionfactory (@qualifier("secondarydatasource") datasource datasource) throws exception{ sqlsessionfactorybean bean = new sqlsessionfactorybean (); bean.setdatasource (datasource); bean.setmapperlocations (new pathmatchingresourcepatternresolver().getresources (locationpattern: "classpath: mybatis/oraclemapper/*. xml")); return bean.getobject(); } /** * 配置声明式事务管理器 */ @bean (name = "secondarytransactionmanager") public platformtransactionmanager secondarytransactionmanager (@qualifier("secondarydatasource") datasource datasource) { return new datasourcetransactionmanager (datasource); } @bean (name = "secondarysqlsessiontemplate") public sqlsessiontemplatel secondarysqlsessiontemplate(@qualifier("secondarysqlsesionfactory") sqlsessionfactory sqlsessionfactory) throws exception { return new sqlsessiontemplatel(sqlsessionfactory); }}
至此,在service层可以像单数据源一样使用了。
以上就是springboot项目配置两个数据源的方法的详细内容。
