From e04a663fdbb903d3330f5befd69dea66e945a3c9 Mon Sep 17 00:00:00 2001 From: wol <1293433164@qq.com> Date: Mon, 11 Aug 2025 23:01:07 +0800 Subject: [PATCH] common-mybatis --- agileboot-common/pom.xml | 1 + agileboot-common/wol-common-box/pom.xml | 5 + .../common/core/core/base/BaseEntity.java | 46 ------- .../core/core/page/AbstractPageQuery.java | 44 ------ .../common/core/core/page/AbstractQuery.java | 90 ------------ .../common/core/core/page/PageDTO.java | 38 ----- .../core/exception/ServiceException.java | 67 +++++++++ .../factory/YmlPropertySourceFactory.java | 31 +++++ .../common/core/utils/sql/SqlUtil.java | 56 ++++++++ agileboot-common/wol-common-mybatis/pom.xml | 25 ++++ .../config/MybatisPlusConfiguration.java | 24 ++++ .../mybatis/core/domain/BaseEntity.java | 70 ++++++++++ .../common/mybatis/core/page/PageQuery.java | 130 ++++++++++++++++++ .../mybatis/core/page/TableDataInfo.java | 107 ++++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../src/main/resources/common-mybatis.yml | 28 ++++ pom.xml | 16 ++- 17 files changed, 555 insertions(+), 224 deletions(-) delete mode 100644 agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/base/BaseEntity.java delete mode 100644 agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/AbstractPageQuery.java delete mode 100644 agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/AbstractQuery.java delete mode 100644 agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/PageDTO.java create mode 100644 agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/exception/ServiceException.java create mode 100644 agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/factory/YmlPropertySourceFactory.java create mode 100644 agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/utils/sql/SqlUtil.java create mode 100644 agileboot-common/wol-common-mybatis/pom.xml create mode 100644 agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/config/MybatisPlusConfiguration.java create mode 100644 agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/domain/BaseEntity.java create mode 100644 agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/page/PageQuery.java create mode 100644 agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/page/TableDataInfo.java create mode 100644 agileboot-common/wol-common-mybatis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 agileboot-common/wol-common-mybatis/src/main/resources/common-mybatis.yml diff --git a/agileboot-common/pom.xml b/agileboot-common/pom.xml index a992a65..667638c 100644 --- a/agileboot-common/pom.xml +++ b/agileboot-common/pom.xml @@ -13,6 +13,7 @@ wol-common-box wol-common-core wol-common-doc + wol-common-mybatis pom diff --git a/agileboot-common/wol-common-box/pom.xml b/agileboot-common/wol-common-box/pom.xml index 045d1d5..a554f36 100644 --- a/agileboot-common/wol-common-box/pom.xml +++ b/agileboot-common/wol-common-box/pom.xml @@ -27,6 +27,11 @@ wol-common-doc ${revision} + + com.agileboot + wol-common-mybatis + ${revision} + diff --git a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/base/BaseEntity.java b/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/base/BaseEntity.java deleted file mode 100644 index 627c65e..0000000 --- a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/base/BaseEntity.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.agileboot.common.core.core.base; - -import com.baomidou.mybatisplus.annotation.FieldFill; -import com.baomidou.mybatisplus.annotation.FieldStrategy; -import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.annotation.TableLogic; -import com.baomidou.mybatisplus.extension.activerecord.Model; -import io.swagger.annotations.ApiModelProperty; -import java.util.Date; -import lombok.Data; -import lombok.EqualsAndHashCode; - -/** - * Entity基类 - * - * @author valarchie - */ -@EqualsAndHashCode(callSuper = true) -@Data -public class BaseEntity> extends Model { - - @ApiModelProperty("创建者ID") - @TableField(value = "creator_id", fill = FieldFill.INSERT) - private Long creatorId; - - @ApiModelProperty("创建时间") - @TableField(value = "create_time", fill = FieldFill.INSERT) - private Date createTime; - - @ApiModelProperty("更新者ID") - @TableField(value = "updater_id", fill = FieldFill.UPDATE, updateStrategy = FieldStrategy.NOT_NULL) - private Long updaterId; - - @ApiModelProperty("更新时间") - @TableField(value = "update_time", fill = FieldFill.UPDATE) - private Date updateTime; - - /** - * deleted字段请在数据库中 设置为tinyInt 并且非null 默认值为0 - */ - @ApiModelProperty("删除标志(0代表存在 1代表删除)") - @TableField("deleted") - @TableLogic - private Boolean deleted; - -} diff --git a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/AbstractPageQuery.java b/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/AbstractPageQuery.java deleted file mode 100644 index b321c48..0000000 --- a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/AbstractPageQuery.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.agileboot.common.core.core.page; - -import cn.hutool.core.util.ObjectUtil; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import jakarta.validation.constraints.Max; -import lombok.Data; -import lombok.EqualsAndHashCode; - -/** - * @author valarchie - */ -@EqualsAndHashCode(callSuper = true) -@Data -public abstract class AbstractPageQuery extends AbstractQuery { - - /** - * 最大分页页数 - */ - public static final int MAX_PAGE_NUM = 200; - /** - * 单页最大大小 - */ - public static final int MAX_PAGE_SIZE = 500; - /** - * 默认分页页数 - */ - public static final int DEFAULT_PAGE_NUM = 1; - /** - * 默认分页大小 - */ - public static final int DEFAULT_PAGE_SIZE = 10; - - @Max(MAX_PAGE_NUM) - protected Integer pageNum; - @Max(MAX_PAGE_SIZE) - protected Integer pageSize; - - public Page toPage() { - pageNum = ObjectUtil.defaultIfNull(pageNum, DEFAULT_PAGE_NUM); - pageSize = ObjectUtil.defaultIfNull(pageSize, DEFAULT_PAGE_SIZE); - return new Page<>(pageNum, pageSize); - } - -} diff --git a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/AbstractQuery.java b/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/AbstractQuery.java deleted file mode 100644 index 4afc09e..0000000 --- a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/AbstractQuery.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.agileboot.common.core.core.page; - -import cn.hutool.core.util.StrUtil; -import com.agileboot.common.core.utils.time.DatePickUtil; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.fasterxml.jackson.annotation.JsonFormat; -import com.fasterxml.jackson.annotation.JsonFormat.Shape; -import java.util.Date; -import lombok.Data; - -/** - * 如果是简单的排序 和 时间范围筛选 可以使用内置的这几个字段 - * @author valarchie - */ -@Data -public abstract class AbstractQuery { - - protected String orderColumn; - - protected String orderDirection; - - protected String timeRangeColumn; - - @JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd") - private Date beginTime; - - @JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd") - private Date endTime; - - private static final String ASC = "ascending"; - private static final String DESC = "descending"; - - /** - * 生成query conditions - * - * @return 添加条件后的QueryWrapper - */ - public QueryWrapper toQueryWrapper() { - QueryWrapper queryWrapper = addQueryCondition(); - addSortCondition(queryWrapper); - addTimeCondition(queryWrapper); - - return queryWrapper; - } - - public abstract QueryWrapper addQueryCondition(); - - public void addSortCondition(QueryWrapper queryWrapper) { - if (queryWrapper == null || StrUtil.isEmpty(orderColumn)) { - return; - } - - Boolean sortDirection = convertSortDirection(); - if (sortDirection != null) { - queryWrapper.orderBy(StrUtil.isNotEmpty(orderColumn), sortDirection, - StrUtil.toUnderlineCase(orderColumn)); - } - } - - public void addTimeCondition(QueryWrapper queryWrapper) { - if (queryWrapper != null - && StrUtil.isNotEmpty(this.timeRangeColumn)) { - queryWrapper - .ge(beginTime != null, StrUtil.toUnderlineCase(timeRangeColumn), - DatePickUtil.getBeginOfTheDay(beginTime)) - .le(endTime != null, StrUtil.toUnderlineCase(timeRangeColumn), DatePickUtil.getEndOfTheDay(endTime)); - } - } - - /** - * 获取前端传来的排序方向 转换成MyBatisPlus所需的排序参数 boolean=isAsc - * @return 排序顺序, null为无排序 - */ - public Boolean convertSortDirection() { - Boolean isAsc = null; - if (StrUtil.isEmpty(this.orderDirection)) { - return isAsc; - } - - if (ASC.equals(this.orderDirection)) { - isAsc = true; - } - if (DESC.equals(this.orderDirection)) { - isAsc = false; - } - - return isAsc; - } - -} diff --git a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/PageDTO.java b/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/PageDTO.java deleted file mode 100644 index feff5f4..0000000 --- a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/core/page/PageDTO.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.agileboot.common.core.core.page; - -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import java.util.List; -import lombok.Data; - -/** - * 分页模型类 - * @author valarchie - */ -@Data -public class PageDTO { - /** - * 总记录数 - */ - private Long total; - - /** - * 列表数据 - */ - private List rows; - - public PageDTO(List list) { - this.rows = list; - this.total = (long) list.size(); - } - - public PageDTO(Page page) { - this.rows = page.getRecords(); - this.total = page.getTotal(); - } - - public PageDTO(List list, Long count) { - this.rows = list; - this.total = count; - } - -} diff --git a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/exception/ServiceException.java b/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/exception/ServiceException.java new file mode 100644 index 0000000..35fdd63 --- /dev/null +++ b/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/exception/ServiceException.java @@ -0,0 +1,67 @@ +package com.agileboot.common.core.exception; + +import cn.hutool.core.text.StrFormatter; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +import java.io.Serial; + +/** + * 业务异常(支持占位符 {} ) + * + * @author ruoyi + */ +@Data +@EqualsAndHashCode(callSuper = true) +@NoArgsConstructor +@AllArgsConstructor +public final class ServiceException extends RuntimeException { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * 错误码 + */ + private Integer code; + + /** + * 错误提示 + */ + private String message; + + /** + * 错误明细,内部调试错误 + */ + private String detailMessage; + + public ServiceException(String message) { + this.message = message; + } + + public ServiceException(String message, Integer code) { + this.message = message; + this.code = code; + } + + public ServiceException(String message, Object... args) { + this.message = StrFormatter.format(message, args); + } + + @Override + public String getMessage() { + return message; + } + + public ServiceException setMessage(String message) { + this.message = message; + return this; + } + + public ServiceException setDetailMessage(String detailMessage) { + this.detailMessage = detailMessage; + return this; + } +} diff --git a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/factory/YmlPropertySourceFactory.java b/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/factory/YmlPropertySourceFactory.java new file mode 100644 index 0000000..d848b49 --- /dev/null +++ b/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/factory/YmlPropertySourceFactory.java @@ -0,0 +1,31 @@ +package com.agileboot.common.core.factory; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.support.DefaultPropertySourceFactory; +import org.springframework.core.io.support.EncodedResource; + +import java.io.IOException; + +/** + * yml 配置源工厂 + * + * @author Lion Li + */ +public class YmlPropertySourceFactory extends DefaultPropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + String sourceName = resource.getResource().getFilename(); + if (StringUtils.isNotBlank(sourceName) && StringUtils.endsWithAny(sourceName, ".yml", ".yaml")) { + YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); + factory.setResources(resource.getResource()); + factory.afterPropertiesSet(); + return new PropertiesPropertySource(sourceName, factory.getObject()); + } + return super.createPropertySource(name, resource); + } + +} diff --git a/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/utils/sql/SqlUtil.java b/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/utils/sql/SqlUtil.java new file mode 100644 index 0000000..1b05861 --- /dev/null +++ b/agileboot-common/wol-common-core/src/main/java/com/agileboot/common/core/utils/sql/SqlUtil.java @@ -0,0 +1,56 @@ +package com.agileboot.common.core.utils.sql; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; + +/** + * sql操作工具类 + * + * @author ruoyi + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class SqlUtil { + + /** + * 定义常用的 sql关键字 + */ + public static String SQL_REGEX = "\u000B|and |extractvalue|updatexml|sleep|exec |insert |select |delete |update |drop |count |chr |mid |master |truncate |char |declare |or |union |like |+|/*|user()"; + + /** + * 仅支持字母、数字、下划线、空格、逗号、小数点(支持多个字段排序) + */ + public static final String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+"; + + /** + * 检查字符,防止注入绕过 + */ + public static String escapeOrderBySql(String value) { + if (StringUtils.isNotEmpty(value) && !isValidOrderBySql(value)) { + throw new IllegalArgumentException("参数不符合规范,不能进行查询"); + } + return value; + } + + /** + * 验证 order by 语法是否符合规范 + */ + public static boolean isValidOrderBySql(String value) { + return value.matches(SQL_PATTERN); + } + + /** + * SQL关键字检查 + */ + public static void filterKeyword(String value) { + if (StringUtils.isEmpty(value)) { + return; + } + String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|"); + for (String sqlKeyword : sqlKeywords) { + if (StringUtils.indexOfIgnoreCase(value, sqlKeyword) > -1) { + throw new IllegalArgumentException("参数存在SQL注入风险"); + } + } + } +} diff --git a/agileboot-common/wol-common-mybatis/pom.xml b/agileboot-common/wol-common-mybatis/pom.xml new file mode 100644 index 0000000..406bfa6 --- /dev/null +++ b/agileboot-common/wol-common-mybatis/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + com.agileboot + agileboot-common + 1.0.0 + + + wol-common-mybatis + + + + com.agileboot + wol-common-core + + + com.baomidou + mybatis-plus-spring-boot3-starter + + + + \ No newline at end of file diff --git a/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/config/MybatisPlusConfiguration.java b/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/config/MybatisPlusConfiguration.java new file mode 100644 index 0000000..d0ccf6d --- /dev/null +++ b/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/config/MybatisPlusConfiguration.java @@ -0,0 +1,24 @@ +package com.agileboot.common.mybatis.config; + +import com.agileboot.common.core.factory.YmlPropertySourceFactory; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; + +@AutoConfiguration +@MapperScan("${mybatis-plus.mapperPackage}") +@PropertySource(value = "classpath:common-mybatis.yml", factory = YmlPropertySourceFactory.class) +public class MybatisPlusConfiguration { + + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor() { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + // 乐观锁插件 + interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); + return interceptor; + } + +} diff --git a/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/domain/BaseEntity.java b/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/domain/BaseEntity.java new file mode 100644 index 0000000..c888c6c --- /dev/null +++ b/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/domain/BaseEntity.java @@ -0,0 +1,70 @@ +package com.agileboot.common.mybatis.core.domain; + +import com.baomidou.mybatisplus.annotation.FieldFill; +import com.baomidou.mybatisplus.annotation.TableField; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Entity基类 + * + * @author Lion Li + */ +@Data +public class BaseEntity implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * 搜索值 + */ + @JsonIgnore + @TableField(exist = false) + private String searchValue; + + /** + * 创建部门 + */ + @TableField(fill = FieldFill.INSERT) + private Long createDept; + + /** + * 创建者 + */ + @TableField(fill = FieldFill.INSERT) + private Long createBy; + + /** + * 创建时间 + */ + @TableField(fill = FieldFill.INSERT) + private Date createTime; + + /** + * 更新者 + */ + @TableField(fill = FieldFill.INSERT_UPDATE) + private Long updateBy; + + /** + * 更新时间 + */ + @TableField(fill = FieldFill.INSERT_UPDATE) + private Date updateTime; + + /** + * 请求参数 + */ + @JsonInclude(JsonInclude.Include.NON_EMPTY) + @TableField(exist = false) + private Map params = new HashMap<>(); + +} diff --git a/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/page/PageQuery.java b/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/page/PageQuery.java new file mode 100644 index 0000000..8fe551d --- /dev/null +++ b/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/page/PageQuery.java @@ -0,0 +1,130 @@ +package com.agileboot.common.mybatis.core.page; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import com.agileboot.common.core.exception.ServiceException; +import com.agileboot.common.core.utils.sql.SqlUtil; +import com.baomidou.mybatisplus.core.metadata.OrderItem; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; + +import java.io.Serial; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +/** + * 分页查询实体类 + * + * @author Lion Li + */ +@Data +@NoArgsConstructor +public class PageQuery implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * 分页大小 + */ + private Integer pageSize; + + /** + * 当前页数 + */ + private Integer pageNum; + + /** + * 排序列 + */ + private String orderByColumn; + + /** + * 排序的方向desc或者asc + */ + private String isAsc; + + /** + * 当前记录起始索引 默认值 + */ + public static final int DEFAULT_PAGE_NUM = 1; + + /** + * 每页显示记录数 默认值 默认查全部 + */ + public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE; + + /** + * 构建分页对象 + */ + public Page build() { + Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM); + Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE); + if (pageNum <= 0) { + pageNum = DEFAULT_PAGE_NUM; + } + Page page = new Page<>(pageNum, pageSize); + List orderItems = buildOrderItem(); + if (CollUtil.isNotEmpty(orderItems)) { + page.addOrder(orderItems); + } + return page; + } + + /** + * 构建排序 + *

+ * 支持的用法如下: + * {isAsc:"asc",orderByColumn:"id"} order by id asc + * {isAsc:"asc",orderByColumn:"id,createTime"} order by id asc,create_time asc + * {isAsc:"desc",orderByColumn:"id,createTime"} order by id desc,create_time desc + * {isAsc:"asc,desc",orderByColumn:"id,createTime"} order by id asc,create_time desc + */ + private List buildOrderItem() { + if (StringUtils.isBlank(orderByColumn) || StringUtils.isBlank(isAsc)) { + return null; + } + String orderBy = SqlUtil.escapeOrderBySql(orderByColumn); + orderBy = StrUtil.toUnderlineCase(orderBy); + + // 兼容前端排序类型 + isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"}); + + String[] orderByArr = orderBy.split(","); + String[] isAscArr = isAsc.split(","); + if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) { + throw new ServiceException("排序参数有误"); + } + + List list = new ArrayList<>(); + // 每个字段各自排序 + for (int i = 0; i < orderByArr.length; i++) { + String orderByStr = orderByArr[i]; + String isAscStr = isAscArr.length == 1 ? isAscArr[0] : isAscArr[i]; + if ("asc".equals(isAscStr)) { + list.add(OrderItem.asc(orderByStr)); + } else if ("desc".equals(isAscStr)) { + list.add(OrderItem.desc(orderByStr)); + } else { + throw new ServiceException("排序参数有误"); + } + } + return list; + } + + @JsonIgnore + public Integer getFirstNum() { + return (pageNum - 1) * pageSize; + } + + public PageQuery(Integer pageSize, Integer pageNum) { + this.pageSize = pageSize; + this.pageNum = pageNum; + } + +} diff --git a/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/page/TableDataInfo.java b/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/page/TableDataInfo.java new file mode 100644 index 0000000..5bd0c12 --- /dev/null +++ b/agileboot-common/wol-common-mybatis/src/main/java/com/agileboot/common/mybatis/core/page/TableDataInfo.java @@ -0,0 +1,107 @@ +package com.agileboot.common.mybatis.core.page; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.http.HttpStatus; +import com.baomidou.mybatisplus.core.metadata.IPage; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serial; +import java.io.Serializable; +import java.util.List; + +/** + * 表格分页数据对象 + * + * @author Lion Li + */ +@Data +@NoArgsConstructor +public class TableDataInfo implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * 总记录数 + */ + private long total; + + /** + * 列表数据 + */ + private List rows; + + /** + * 消息状态码 + */ + private int code; + + /** + * 消息内容 + */ + private String msg; + + /** + * 分页 + * + * @param list 列表数据 + * @param total 总记录数 + */ + public TableDataInfo(List list, long total) { + this.rows = list; + this.total = total; + this.code = HttpStatus.HTTP_OK; + this.msg = "查询成功"; + } + + /** + * 根据分页对象构建表格分页数据对象 + */ + public static TableDataInfo build(IPage page) { + TableDataInfo rspData = new TableDataInfo<>(); + rspData.setCode(HttpStatus.HTTP_OK); + rspData.setMsg("查询成功"); + rspData.setRows(page.getRecords()); + rspData.setTotal(page.getTotal()); + return rspData; + } + + /** + * 根据数据列表构建表格分页数据对象 + */ + public static TableDataInfo build(List list) { + TableDataInfo rspData = new TableDataInfo<>(); + rspData.setCode(HttpStatus.HTTP_OK); + rspData.setMsg("查询成功"); + rspData.setRows(list); + rspData.setTotal(list.size()); + return rspData; + } + + /** + * 构建表格分页数据对象 + */ + public static TableDataInfo build() { + TableDataInfo rspData = new TableDataInfo<>(); + rspData.setCode(HttpStatus.HTTP_OK); + rspData.setMsg("查询成功"); + return rspData; + } + + /** + * 根据原始数据列表和分页参数,构建表格分页数据对象(用于假分页) + * + * @param list 原始数据列表(全部数据) + * @param page 分页参数对象(包含当前页码、每页大小等) + * @return 构造好的分页结果 TableDataInfo + */ + public static TableDataInfo build(List list, IPage page) { + if (CollUtil.isEmpty(list)) { + return TableDataInfo.build(); + } + List pageList = CollUtil.page((int) page.getCurrent() - 1, (int) page.getSize(), list); + return new TableDataInfo<>(pageList, list.size()); + } + +} diff --git a/agileboot-common/wol-common-mybatis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/agileboot-common/wol-common-mybatis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..b7d43c6 --- /dev/null +++ b/agileboot-common/wol-common-mybatis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.agileboot.common.mybatis.config.MybatisPlusConfiguration diff --git a/agileboot-common/wol-common-mybatis/src/main/resources/common-mybatis.yml b/agileboot-common/wol-common-mybatis/src/main/resources/common-mybatis.yml new file mode 100644 index 0000000..855d85d --- /dev/null +++ b/agileboot-common/wol-common-mybatis/src/main/resources/common-mybatis.yml @@ -0,0 +1,28 @@ +# 内置配置 不允许修改 如需修改请在 nacos 上写相同配置覆盖 +# MyBatisPlus配置 +# https://baomidou.com/config/ +mybatis-plus: + # 启动时是否检查 MyBatis XML 文件的存在,默认不检查 + checkConfigLocation: false + configuration: + # 自动驼峰命名规则(camel case)映射 + mapUnderscoreToCamelCase: true + # MyBatis 自动映射策略 + # NONE:不启用 PARTIAL:只对非嵌套 resultMap 自动映射 FULL:对所有 resultMap 自动映射 + autoMappingBehavior: FULL + # MyBatis 自动映射时未知列或未知属性处理策 + # NONE:不做处理 WARNING:打印相关警告 FAILING:抛出异常和详细信息 + autoMappingUnknownColumnBehavior: NONE + # 更详细的日志输出 会有性能损耗 org.apache.ibatis.logging.stdout.StdOutImpl + # 关闭日志记录 (可单纯使用 p6spy 分析) org.apache.ibatis.logging.nologging.NoLoggingImpl + # 默认日志输出 org.apache.ibatis.logging.slf4j.Slf4jImpl + logImpl: org.apache.ibatis.logging.nologging.NoLoggingImpl + global-config: + banner: true # 是否打印 Logo banner + dbConfig: + idType: ASSIGN_ID # 主键类型: AUTO 自增 NONE 空 INPUT 用户输入 ASSIGN_ID 雪花 ASSIGN_UUID 唯一 UUID + logicDeleteValue: 1 # 逻辑已删除值(框架表均使用此值 禁止随意修改) + logicNotDeleteValue: 0 # 逻辑未删除值 + insertStrategy: NOT_NULL + updateStrategy: NOT_NULL + whereStrategy: NOT_NULL diff --git a/pom.xml b/pom.xml index 048a1f2..742dd34 100644 --- a/pom.xml +++ b/pom.xml @@ -39,7 +39,6 @@ 2.6.5 4.1.2 1.6.8 - 3.5.2 @@ -95,9 +94,14 @@ + + + + + com.baomidou - mybatis-plus-boot-starter + mybatis-plus-spring-boot3-starter ${mybatis-plus.version} @@ -293,10 +297,10 @@ org.projectlombok lombok - - com.baomidou - mybatis-plus-boot-starter - + + + + org.springframework.boot spring-boot-configuration-processor