使⽤springboot+druid+mybatisplus完成多数据源配置⼀. 简介
1. 版本
springboot版本为2.0.3.RELEASE,mybatisplus版本为2.1.9, druid版本为1.1.9,swagger版本为2.7.0
2.项⽬地址
该地址有初始sql和测试⽅法
3. 留个记录,⽅便查
开发步骤:
1. 新建springboot项⽬。
2. 导⼊依赖 --> devtools,lombok,web,thymeleaf,mysql,aop,mybatisplus,druid,swagger。
3. maven多环境配置。
4. 编写l --> 项⽬端⼝,项⽬路径名,mybatisplus配置,mysql多数据源配置。
5. 新建DataSourceContextHolder ⽤于设置,获取,清空当前线程内的数据源变量。
6. 新建 MultipleDataSource 实现 AbstractRoutingDataSource 类。重写determineCurrentLookupKey(),通过
DataSourceContextHolder 获取数据源变量,⽤于当作lookupKey取出指定的数据源。
7. 新建DataSourceEnum ⽤于存放数据源名称。
8. 新建注解 DataSource,⽤于aop类中当作切⼊点来选择数据源。
编写aop类 --> DataSourceAspect.java
9. 新建并配置DruidConfiguration,MybatisplusConfiguration,SwaggerConfiguration 三个类。
DruidConfiguration --> StatViewServlet 和 WebStatFilter Druid监控配置和监控过滤器。
MybatisplusConfiguration --> mybatisplus 分页插件,SQL执⾏效率插件
数据源Bean,MultipleDataSource 注⼊
SqlSessionFactory注⼊
SwaggerConfiguration --> 常规配置。
10. crud Controller接⼝。
11. 完整项⽬结构
⼆. 详细步骤
1. 新建springboot 项⽬
idea 新建Empty 项⽬ 然后创建springboot 项⽬ 勾选 devtools ,lombok ,web ,thymeleaf ,mysql
2. 导⼊依赖
l
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.seawaterbt</groupId>
<artifactId>ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ssm</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding> <java.version>1.8</java.version>
<druid.version>1.1.9</druid.version>
<swagger.version>2.7.0</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.1.9</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>prod</id>
<properties>
<config.dir>prod</config.dir>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<config.dir>dev</config.dir>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>qa</id>
<properties>
<config.dir>qa</config.dir>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources/config/${config.dir}</directory>
<includes>
<include>*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
</build>
</project>
3. Maven多环境配置看pom⽂件
4. 编写l
l
server:
port: 8080
servlet:
context-path: /ssm
spring:
datasource:
druid:
db1:
username: root
password: 123456
driver-class-name: sql.jdbc.Driver
url: jdbc:mysql:///mds
initialSize: 5
minIdle: 5
maxActive: 20
db2:
username: root
password: 123456
driver-class-name: sql.jdbc.Driver
url: jdbc:mysql:///ssj
initialSize: 5
minIdle: 5
maxActive: 20
mybatis-plus:
# 如果是放在src/main/java⽬录下 classpath:/com/yourpackage/*/mapper/*l
# 如果是放在resource⽬录 classpath:/mapper/*l
mapper-locations: classpath:/mapper/*l
springboot aop#实体扫描,多个package⽤逗号或者分号分隔
typeAliasesPackage: com.ity
global-config:
#主键类型 0:"数据库ID⾃增", 1:"⽤户输⼊ID",2:"全局唯⼀ID (数字类型唯⼀ID)", 3:"全局唯⼀ID UUID"; id-type: 0
#字段策略 0:"忽略判断",1:"⾮ NULL 判断"),2:"⾮空判断"
field-strategy: 2
#驼峰下划线转换
db-column-underline: true
#mp2.3+ 全局表前缀 mp_
#table-prefix: mp_
#刷新mapper 调试神器
refresh-mapper: true
#数据库⼤写下划线转换
#capital-mode: true
#逻辑删除配置(下⾯3个配置)
logic-delete-value: 4
logic-not-delete-value: 0
configuration:
#配置返回数据库(column下划线命名&&返回java实体是驼峰命名),⾃动匹配⽆需as(没开启这个,SQL需要写as: select user_id as userId) map-underscore-to-camel-case: true
cache-enabled: false
#配置JdbcTypeForNull, oracle数据库必须配置
jdbc-type-for-null: 'null'
5. 新建DataSourceContextHolder
DataSourceContextHolder.java
package com.seawaterbt.ssm.multiple;
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new InheritableThreadLocal<>();
/**
* 设置数据源
* @param db
*/
public static void setDataSource(String db){
contextHolder.set(db);
}
/**
* 取得当前数据源
* @return
*/
public static String getDataSource(){
();
}
/**
* 清除上下⽂数据
*/
public static void clear(){
}
}
DataSourceContextHolder.java
6. 新建 MultipleDataSource
MultipleDataSource.java
package com.seawaterbt.ssm.multiple;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class MultipleDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
DataSource();
}
}
MultipleDataSource.java
7. 新建DataSourceEnum
DataSourceEnum.java
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论