springboot+mybatis配置多数据源_Spring Boot+MyBatis多数据源解决方案

Spring Boot+MyBatis多数据源解决方案

这篇文档教大家配置Spring Boot、MyBatis整合多数据源的配置方法。

一、数据库连接配置

Spring Boot启动的application.yml配置文件配置数据库的连接配置,代码如如下:

#访问端口server: port: 8480#mybatis配置mybatis: config-location: classpath:mybatis/mybatis-config.xml#mybatis分页插件pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql#jdbc urlspring: main: allow-bean-definition-overriding: true session: store-type: none datasource: test1: jdbc-url: jdbc:mysql://10.10.1.232:3306/zhlc_demo?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true username: zhlc password: zhlc123 driver-class-name: com.mysql.jdbc.Driver test2: jdbc-url: jdbc:mysql://10.10.1.96:31938/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true username: root password: root driver-class-name: com.mysql.jdbc.Driver

其中server.port为项目启动端口号,项目访问根路径为http://localhost:8480。spring.datasource.test1为连接的第一个数据库的数据源(相应的jdbc连接url、MySQL用户名密码)。spring.datasource.test2为连接的第二个数据库的数据源。并加上相应的驱动。

注意:因为使用的是application.yml为yml格式的文件,因此每一级的层级关系为

a10442affe148b0c128aacf825388888.png

呈递进关系,逐级空行。

mybatis-config.xml在src/main/resources/mybatis目录下

0125ca64c1272b45054213bf7bcdc804.png

mybatis-config.xml文件的内容为:

<?xml version="1.0" encoding="UTF-8" ?>

二、数据源DataSource配置

新建包

com.ocai.core.datasource

在该包下新建DataSource1Config.java文件,文件上部加上包扫描注解

da7f8e94d3e125e6e3ed4c7065874d36.png

表示此数据源文件扫描com.ocai.core.mapper.test1包下的DAO文件。

数据源建立如下

14f802fac8d9beb064172fe15c764a8e.png

spring.datasource.test1为前面application.yml中的数据源配置。

建立sqlSessionFactory,如下,并注入datasource

b8732d4a6f7c3dabeaa621e3d8716641.png

配置事务管理器transactionManager

293bd716417f5cbd21786716b3db7821.png

配置sqlSessionTemplate如下

65c55053f9b36f8ebc13059c7b9375a9.png

完整的DataSourceConfig文件代码如下:

package com.ocai.core.datasource;import com.github.pagehelper.PageInterceptor;import org.apache.ibatis.plugin.Interceptor;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.boot.jdbc.DataSourceBuilder;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;import java.util.Properties;/** * Created by liyue on 2019/07/12. */@Configuration@MapperScan(basePackages = "com.ocai.core.mapper.test1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值