参考
SpringBoot 3.1.10 整合Mybatis-plus(快速学会)
开发环境
名称 |
版本 |
操作系统 |
Windows 11 X64 |
SpringBoot |
3.2.4 |
Mybatis-plus |
3.5.5 |
JDK |
17 |
源码下载
sharding-jdbc-demo.zip
测试表
CREATE TABLE `payment_wallet_detail` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`pay_month` varchar(6) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '支付月份yyyyMM,分片键',
`tenant_id` bigint(20) DEFAULT NULL COMMENT '"tenantId',
`create_by` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '创建人',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_by` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '修改人(数据库中最终取 update_by)',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间(数据库中最终取 update_time)',
`is_delete` tinyint(1) DEFAULT '0' COMMENT '是否删除(0、否,1、是) 默认是0',
PRIMARY KEY (`id`),
KEY `payment_wallet_detail_kv_id_IDX` (`kv_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='发放明细表';
CREATE TABLE `payment_wallet_transaction` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`pay_order_number` varchar(30) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '外部订单号-对应明细表的明细唯一单号,分片键(hash-mod)',
`tenant_id` bigint(20) DEFAULT '1' COMMENT '租户id',
`create_by` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '创建人',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_by` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '修改人',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
`is_delete` tinyint(1) DEFAULT NULL COMMENT '是否删除(0:否,1:是)',
PRIMARY KEY (`id`) USING BTREE,
KEY `pay_order_number_IDX` (`pay_order_number`,`transaction_direction`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC COMMENT='发放流水表';
创建空项目
项 |
值 |
名称 |
SpringBoot3-Study |
位置 |
D:\Study-Java\2023 |
创建模块-sharding-jdbc-demo
项 |
值 |
名称 |
sharding-jdbc-demo |
位置 |
D:\Study-Java\2023\SpringBoot3-Study |
语言 |
Java |
构建系统 |
Maven |
JDK |
版本:17 供应商:Eclipse Temurin(AdoptOpenJDK HotSpot)17.0.8 位置:C:\Users\Administrator.jdks\temurin-17.0.8
|
主 ID |
com.atguigu |
工件 ID |
sharding-jdbc-demo |
sharding-jdbc-demo
目录结构
├─sharding-jdbc-demo
│ │ pom.xml
│ │
│ ├─src
│ │ ├─main
│ │ │ ├─java
│ │ │ │ └─com
│ │ │ │ └─sharding
│ │ │ │ └─demo
│ │ │ │ │ ShardingJdbcDemoApplication.java
│ │ │ │ │
│ │ │ │ ├─common
│ │ │ │ │ Contents.java
│ │ │ │ │ ResponseCode.java
│ │ │ │ │
│ │ │ │ ├─config
│ │ │ │ │ MybatisPlusConfig.java
│ │ │ │ │
│ │ │ │ ├─controller
│ │ │ │ │ TestController.java
│ │ │ │ │
│ │ │ │ ├─DTO
│ │ │ │ │ BatchParam.java
│ │ │ │ │
│ │ │ │ ├─exception
│ │ │ │ │ InternalApiException.java
│ │ │ │ │
│ │ │ │ ├─handler
│ │ │ │ │ MyMetaObjectHandler.java
│ │ │ │ │
│ │ │ │ ├─mapper
│ │ │ │ │ PaymentWalletDetailMapper.java
│ │ │ │ │ PaymentWalletTransactionMapper.java
│ │ │ │ │
│ │ │ │ ├─model
│ │ │ │ │ PaymentWalletDetail.java
│ │ │ │ │ PaymentWalletTransaction.java
│ │ │ │ │ SuperEntity.java
│ │ │ │ │
│ │ │ │ ├─service
│ │ │ │ │ │ IPaymentWalletDetailService.java
│ │ │ │ │ │ IPaymentWalletTransactionService.java
│ │ │ │ │ │
│ │ │ │ │ └─impl
│ │ │ │ │ PaymentWalletDetailServiceImpl.java
│ │ │ │ │ PaymentWalletTransactionServiceImpl.java
│ │ │ │ │
│ │ │ │ ├─sharingrule
│ │ │ │ │ PayMonthAlgorithm.java
│ │ │ │ │ WalletPayOrderNumberShardingRule.java
│ │ │ │ │
│ │ │ │ └─utils
│ │ │ │ AssertUtil.java
│ │ │ │ CommonUtil.java
│ │ │ │ DateUtils.java
│ │ │ │
│ │ │ └─resources
│ │ │ │ application.yml
│ │ │ │ sharding-dev.yml
│ │ │ │
│ │ │ ├─mapper
│ │ │ │ PaymentWalletDetailMapper.xml
│ │ │ │ PaymentWalletTransactionMapper.xml
│ │ │ │
│ │ │ └─META-INF
│ │ │ └─services
│ │ │ org.apache.shardingsphere.sharding.spi.ShardingAlgorithm
│
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sharding.demo</groupId>
<artifactId>sharding-jdbc-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sharding-jdbc-demo</name>
<description>sharding-jdbc-demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入 MybatisPlus 依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.5</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!--引入 durid 数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.25</version>
</dependency>
<!-- springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
application.yml
server:
port: 8088
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://a.a.a.a:3306/testDB?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false
username: username
password: password
#mybatis
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage:
typeEnumsPackage:
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
#刷新mapper 调试神器
refresh-mapper: true
#数据库大写下划线转换
#capital-mode: true
#序列接口实现类配置
#key-generator:
#逻辑删除配置
logic-delete-value: 0
logic-not-delete-value: 1
# #自定义填充策略接口实现
# meta-object-handler:
configuration:
map-underscore-to-camel-case: true #开启驼峰命名
cache-enabled: false
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志
主启动类-ShardingJdbcDemoApplication
package com.sharding.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.sharding.demo.mapper")
public class ShardingJdbcDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingJdbcDemoApplication.class, args);
}
}
entity
SuperEntity
package com.sharding.demo.model;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable;
import java.util.Date;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* 基础实体类
*/
@Getter
@Setter
@ToString
public class SuperEntity<T extends Model<T>> extends Model<T> {
/**
* 主键ID
*/
@TableId(type = IdType.AUTO)
private Long id;
@TableField(value = "create_by", fill = FieldFill.INSERT)
private String createBy;
@TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
private String updateBy;
/**
* 创建时间
*/
@TableField(value = "create_time", fill = FieldFill.INSERT)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/**
* 更新时间
*/
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/**
* 是否删除(0:未删除;1:已删除)
*/
@TableField(value = "is_delete", fill = FieldFill.INSERT)
private Integer isDelete;
@Override
public Serializable pkVal() {
return this.id;
}
}
PaymentWalletDetail
package com.sharding.demo.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.math.BigDecimal;
/**
* 发放明细表
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class PaymentWalletDetail extends SuperEntity {
/**
* 主键ID
*/
@TableId(type = IdType.ASSIGN_ID)
private Long id;
/**
* "tenantId
*/
private Long tenantId;
/**
*支付月份yyyyMM,分片键,取自iface pay_date
*/
private String payMonth;
private static final long serialVersionUID = 1L;
}
PaymentWalletTransaction
package com.sharding.demo.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.math.BigDecimal;
/**
* 发放流水表
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class PaymentWalletTransaction extends SuperEntity {
/**
* 主键ID
*/
@TableId(type = IdType.ASSIGN_ID)
private Long id;
/**
* 外部订单号-对应明细表的明细唯一单号
*/
private String payOrderNumber;
/**
* 租户id
*/
private Long tenantId;
}
Mapper
PaymentWalletDetailMapper
package com.sharding.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sharding.demo.model.PaymentWalletDetail;
import org.springframework.stereotype.Repository;
@Repository
public interface PaymentWalletDetailMapper extends BaseMapper<PaymentWalletDetail> {
}
PaymentWalletDetailMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sharding.demo.mapper.PaymentWalletDetailMapper">
</mapper>
PaymentWalletTransactionMapper
package com.sharding.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sharding.demo.model.PaymentWalletTransaction;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface PaymentWalletTransactionMapper extends BaseMapper<PaymentWalletTransaction> {
}
PaymentWalletTransactionMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sharding.demo.mapper.PaymentWalletTransactionMapper">
</mapper>
service 和 serviceImpl 层
IPaymentWalletDetailService
package com.sharding.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sharding.demo.model.PaymentWalletDetail;
public interface IPaymentWalletDetailService extends IService<PaymentWalletDetail> {
}
PaymentWalletDetailServiceImpl
package com.sharding.demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sharding.demo.mapper.PaymentWalletDetailMapper;
import com.sharding.demo.model.PaymentWalletDetail;
import com.sharding.demo.service.IPaymentWalletDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class PaymentWalletDetailServiceImpl extends ServiceImpl<PaymentWalletDetailMapper, PaymentWalletDetail>
implements IPaymentWalletDetailService {
}
IPaymentWalletTransactionService
package com.sharding.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sharding.demo.model.PaymentWalletTransaction;
public interface IPaymentWalletTransactionService extends IService<PaymentWalletTransaction> {
}
PaymentWalletTransactionServiceImpl
package com.sharding.demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sharding.demo.mapper.PaymentWalletTransactionMapper;
import com.sharding.demo.model.PaymentWalletTransaction;
import com.sharding.demo.service.IPaymentWalletTransactionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class PaymentWalletTransactionServiceImpl
extends ServiceImpl<PaymentWalletTransactionMapper, PaymentWalletTransaction>
implements IPaymentWalletTransactionService {
}
controller
package com.sharding.demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sharding.demo.mapper.PaymentWalletDetailMapper;
import com.sharding.demo.model.PaymentWalletDetail;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
@Resource
PaymentWalletDetailMapper detailService;
@GetMapping("/testGetDetail")
public List<PaymentWalletDetail> testGetDetail(@RequestParam(name = "payMonth") String payMonth) {
LambdaQueryWrapper<PaymentWalletDetail> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(PaymentWalletDetail::getPayMonth, payMonth);
return detailService.selectList(wrapper);
}
}
测试结果
[{"id":1,"createBy":null,"updateBy":null,"createTime":"2024-04-24 23:26:31","updateTime":"2024-04-24 23:26:31","isDelete":0,"payMonth":"202403"}]