今天就跟大家聊聊有关springboot 中怎么利用mybatis 配置多数据源,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
一、项目启动成功后创建bean的方式
pom.xml
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!-- maven仓库中没有oracle数据库驱动,这里将jar 放到工程下进行引入 --> <dependency> <groupId>com.ojdbc</groupId> <artifactId>ojdbc</artifactId> <version>10.2.0.1</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/lib/ojdbc14-10.2.0.1.0.jar</systemPath> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
application.properties
#oracle数据库 spring.datasource.master.url=jdbc:oracle:thin:@localhost:1521:test spring.datasource.master.username=oracle spring.datasource.master.password=123456 spring.datasource.master.driverClassName=oracle.jdbc.OracleDriver spring.datasource.master.initialSize=5 spring.datasource.master.maxActive=15 spring.datasource.master.minIdle=5 spring.datasource.master.poolPreparedStatements=true spring.datasource.master.validationQuery=SELECT 1 FROM DUAL #配置执行多条sql比如:批量处理 #对应的错误:sql injection violation, multi-statement not allow spring.datasource.master.wall.multiStatementAllow=true #配置执行特殊sql比如sql server的创建临时表(declare)语句 #对应的错误:sql injection violation, class com.alibaba.druid.sql.ast.statement.SQLDeclareStatement not allow spring.datasource.master.wall.noneBaseStatementAllow=true #mysql数据库 spring.datasource.slave.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8 spring.datasource.slave.username=mysql spring.datasource.slave.password=123456 spring.datasource.slave.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.slave.initialSize=5 spring.datasource.slave.maxActive=15 spring.datasource.slave.minIdle=5 spring.datasource.slave.poolPreparedStatements=true spring.datasource.slave.validationQuery=SELECT 1 #配置执行多条sql比如:批量处理 #对应的错误:sql injection violation, multi-statement not allow spring.datasource.master.wall.multiStatementAllow=true #配置执行特殊sql比如sql server的创建临时表(declare)语句 #对应的错误:sql injection violation, class com.alibaba.druid.sql.ast.statement.SQLDeclareStatement not allow spring.datasource.master.wall.noneBaseStatementAllow=true
MasterDataSourceConfig.java
注:Primary注解代表该数据源为主数据源
package com.core.dataSource; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; 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.boot.context.properties.ConfigurationProperties; 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.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * oracle数据配置 */ @Configuration //配置mapper文件目录,进行扫描 @MapperScan(basePackages = "com.wawj.core.mapper.master", sqlSessionTemplateRef = "masterSqlSessionTemplate") public class MasterDataSourceConfig { /** * 创建数据源 * @return */ @Bean @Primary @ConfigurationProperties(prefix = "spring.datasource.master") public DataSource masterDataSource() { return DruidDataSourceBuilder.create().build(); } /** * 创建工厂 * @param dataSource * @return * @throws Exception */ @Bean @Primary public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //指定mapper.xml的目录 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/**/*.xml")); //指定实体类目录 bean.setTypeAliasesPackage("com.wawj.core.entity"); return bean.getObject(); } /** * 创建事务 * @param dataSource * @return */ @Bean @Primary public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * 创建模板 * @param sqlSessionFactory * @return */ @Bean @Primary public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
SlaveDataSourceConfig.java
package com.core.dataSource; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; 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.boot.context.properties.ConfigurationProperties; 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.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * mysql数据配置 */ @Configuration //配置mapper文件目录,进行扫描 @MapperScan(basePackages = "com.wawj.core.mapper.slave", sqlSessionTemplateRef = "slaveSqlSessionTemplate") public class SlaveDataSourceConfig { /** * 创建数据源 * @return */ @Bean @ConfigurationProperties(prefix = "spring.datasource.slave") public DataSource slaveDataSource() { return DruidDataSourceBuilder.create().build(); } /** * 创建工厂 * @param dataSource * @return * @throws Exception */ @Bean public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //指定mapper.xml的目录 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/**/*.xml")); //指定实体类目录 bean.setTypeAliasesPackage("com.wawj.core.entity"); return bean.getObject(); } /** * 创建事务 * @param dataSource * @return */ @Bean public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * 创建模板 * @param sqlSessionFactory * @return */ @Bean public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
这种方式初始化的是时候在druid控制台(http://localhost:8080/druid/login.html)中的数据源标签是没有,要请求过后才会显示当前连接的数据源
二、使用dynamic-datasource 动态配置(项目启动的时候创建)
pom.xml
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.6</version> </dependency> <!-- maven仓库中没有oracle数据库驱动,这里将jar 放到工程下进行引入 --> <dependency> <groupId>com.ojdbc</groupId> <artifactId>ojdbc</artifactId> <version>10.2.0.1</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/lib/ojdbc14-10.2.0.1.0.jar</systemPath> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
application.properties
#全局默认值,可以全局更改 spring.datasource.dynamic.druid.initial-size=5 spring.datasource.dynamic.druid.max-active=5 spring.datasource.dynamic.druid.min-idle=5 spring.datasource.dynamic.druid.pool-prepared-statements=true #配置执行多条sql比如:批量处理 #对应的错误:sql injection violation, multi-statement not allow spring.datasource.dynamic.druid.wall.multi-statement-allow=true #配置执行特殊sql比如sql server的创建临时表(declare)语句 #对应的错误:sql injection violation, class com.alibaba.druid.sql.ast.statement.SQLDeclareStatement not allow spring.datasource.dynamic.druid.wall.none-base-statement-allow=true #oracle数据库 spring.datasource.dynamic.datasource.master.driver-class-name=oracle.jdbc.OracleDriver spring.datasource.dynamic.datasource.master.druid.validation-query=SELECT 1 FROM DUAL spring.datasource.dynamic.datasource.master.url=jdbc:oracle:thin:@localhost:1521:test spring.datasource.dynamic.datasource.master.username=oracle spring.datasource.dynamic.datasource.master.password=123456 #mysql数据库 spring.datasource.dynamic.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.dynamic.datasource.slave.druid.validation-query=SELECT 1 spring.datasource.dynamic.datasource.slave.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8 spring.datasource.dynamic.datasource.slave.username=mysql spring.datasource.dynamic.datasource.slave.password=123456 #指定master为主数据源 spring.datasource.dynamic.primary=master #排除原生Druid的快速配置类(DruidDataSourceAutoConfigure会注入一个DataSourceWrapper,其会在原生的spring.datasource下找url,username,password等。而我们动态数据源的配置路径是变化的。) spring.autoconfigure.exclude=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure #设置dynamic登录名 spring.datasource.druid.stat-view-servlet.login-username=admin #设置dynamic密码 spring.datasource.druid.stat-view-servlet.login-password=admin #mybatis mapper location mybatis-plus.mapper-locations=classpath:mapper/**/*.xml mybatis-plus.type-aliases-package=com.wawj.core.entity
这种方式使用注解(@DS("数据源名称"))切换数据源
@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。
注解在service实现或mapper接口方法上,但强烈不建议同时在service和mapper注解。 (可能会有问题)
注解 | 结果 |
---|---|
没有@DS | 默认数据源 |
@DS("dsName") | dsName可以为组名也可以为具体某个库的名称 |
补充:排除原生的DruidDataSourceAutoConfigure也可以在springboot启动类排除(某些springBoot的版本上面可能无法排除就在application.properties中配置排除)
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 或 #application.properties spring.autoconfigure.exclude=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
看完上述内容,你们对springboot 中怎么利用mybatis 配置多数据源有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。