SpringBoot系列:Spring Boot多数据源,Mybatis方式

一、Mybatis配置多数据源

mybatis可谓是国内最火的ORM框架了,mybatis的多数据源当然在实际使用中也必不可少。mybatis多数据源一般有两种方式,一种是基于不同的包使用不同的数据源,这个类似于JPA配置多数据源,另一种则是注解+aop实现动态多数据源。

本章节就来看一看使用不同的包怎么做到多数据源。

首先是pom文件,除了SpringBoot的依赖外,主要就是mybatis及数据库的依赖了。

<!--mysql连接-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<!--druid连接池-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

<!--mybatis依赖-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

然后是配置文件,我们需要配置多数据源。这里不需要对mybatis进行配置,因为配置都会移到配置类里。

server:
  port: 10900

spring:
  datasource:
    master:
      # 新版驱动从com.mysql.jdbc.Driver变更为com.mysql.cj.jdbc.Driver
      driver-class-name: com.mysql.cj.jdbc.Driver
      # 数据源需要添加时间标准和指定编码格式解决乱码 You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
      url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
      username: root
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值