springboot3分库分表
环境信息、springboot3、jdk17
pom依赖
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<version>4.2.0</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
配置文件
spring:
datasource:
dynamic:
primary: master
datasource:
master: # 主库
url: jdbc:mysql://127.0.0.1:3306/qing?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
slave: # 从库
url: jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
两个库都建表
create table GE_PO_HEADER
(
ID int auto_increment
primary key,
BILL_NO varchar(30) null,
BILL_NAME varchar(300) null,
ORDER_TYPE varchar(30) null,
CONTRACT_NO varchar(50) null,
SUPPLIER_CD varchar(30) null,
SUPPLIER_NAME varchar(300) null,
PO_COMPANY_CD varchar(30) null,
PO_COMPANY_NAME varchar(30) null,
ORG_CODE varchar(30) null,
ORG_NAME varchar(100) null,
BUYER_NAME varchar(30) null,
SUBMITED_DATE date null,
APPROVED_DATE date null,
REFERENCE1 varchar(240) null,
REFERENCE2 varchar(240) null,
REFERENCE3 varchar(240) null,
REFERENCE4 varchar(240) null,
REFERENCE5 varchar(240) null,
VERSION_ID int null,
OBJECT_VERSION_NUMBER int default 1 not null,
CREATED_BY int default -1 not null,
LAST_UPDATED_BY int default -1 not null,
LAST_UPDATE_LOGIN int null
);
实体类
package com.flowable.dto;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("GE_PO_HEADER")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class GePoHeader {
@TableId("ID")
private Integer id;
@TableField("BILL_NO")
private String billNo;
@TableField("BILL_NAME")
private String billName;
@TableField("ORDER_TYPE")
private String orderType;
@TableField("CONTRACT_NO")
private String contractNo;
@TableField("SUPPLIER_CD")
private String supplierCd;
@TableField("SUPPLIER_NAME")
private String supplierName;
@TableField("PO_COMPANY_CD")
private String poCompanyCd;
@TableField("PO_COMPANY_NAME")
private String poCompanyName;
@TableField("ORG_CODE")
private String orgCode;
@TableField("ORG_NAME")
private String orgName;
@TableField("BUYER_NAME")
private String buyerName;
@TableField("SUBMITED_DATE")
private Date submittedDate;
@TableField("APPROVED_DATE")
private Date approvedDate;
@TableField("REFERENCE1")
private String reference1;
@TableField("REFERENCE2")
private String reference2;
@TableField("REFERENCE3")
private String reference3;
@TableField("REFERENCE4")
private String reference4;
@TableField("REFERENCE5")
private String reference5;
@TableField("VERSION_ID")
private int versionId;
@TableField("OBJECT_VERSION_NUMBER")
private int objectVersionNumber = 1; // default value
@TableField("CREATED_BY")
private int createdBy = -1; // default value
@TableField("LAST_UPDATED_BY")
private int lastUpdatedBy = -1; // default value
@TableField("LAST_UPDATE_LOGIN")
private int lastUpdateLogin;
}
启动类
package com.flowable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy(exposeProxy=true)
@SpringBootApplication(proxyBeanMethods = false)
public class FlowableApplication {
public static void main(String[] args) {
SpringApplication.run(FlowableApplication.class, args);
}
}
使用方法@DS
@DS指定要使用的数据库,若没有指定,则使用spring.datasource.dynamic.primary指定的库,spring.datasource.dynamic.primary必须指定
@DS("slave")
public List<?> query() {
return gePoHeaderMapper.selectList(null);
}
@DS("master")
public List<?> insert() {
gePoHeaderMapper.insert(GePoHeader.builder().approvedDate(new Date()).build());
return List.of();
}