前言
参考文章:基于Mybatis框架,采用切面编程方式实现
https://www.jianshu.com/p/2222257f96d3
以上,包括其他技术文章,实现方式都是大同小异,都存在着一些小问题。譬如:
- 采用切面编程,字符串匹配方式,局限于方法写死,特别是项目处于开发收尾的话,改动代码量大。
- 以注解的方式类同。
本篇优势: - 源代码不变(通过mybatis拦截器),易扩展
- 多数据源可配置
示例图
导包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.42</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
配置文件application.yaml
spring:
datasource:
slave:
url: jdbc:mysql://192.168.1.70:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
username: kmob
password: kmob0724(
filters: mergeStat
on-off: true
driver-class-name: com.mysql.jdbc.Driver
master:
url: jdbc:mysql://**.**.**.**:3306/test?autoReconnect=