🔨 service 改为继承ExtendService, mapper继承ExtendMapper, 分页查询条件构造下沉至dao层并移除 Controller层和 service层对 IPage 的依赖
This commit is contained in:
@@ -16,6 +16,11 @@
|
||||
<groupId>com.hccake</groupId>
|
||||
<artifactId>ballcat-common-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hccake</groupId>
|
||||
<artifactId>ballcat-extend-mybatis-plus</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.hccake</groupId>
|
||||
<artifactId>ballcat-extend-dingtalk</artifactId>
|
||||
|
||||
@@ -1,12 +1,21 @@
|
||||
package com.hccake.ballcat.common.conf.mybatis;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
|
||||
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.hccake.extend.mybatis.plus.injector.CustomSqlInjector;
|
||||
import com.hccake.extend.mybatis.plus.methods.SelectByPage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hccake
|
||||
* @date 2020/04/19 默认配置MybatisPlus分页插件,通过conditional注解达到覆盖效用
|
||||
@@ -37,4 +46,19 @@ public class MybatisPlusConfig {
|
||||
return new FillMetaObjectHandle();
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义批量插入方法注入
|
||||
* @return ISqlInjector
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ISqlInjector.class)
|
||||
public ISqlInjector customSqlInjector() {
|
||||
List<AbstractMethod> list = new ArrayList<>();
|
||||
// 对于只在更新时进行填充的字段不做插入处理
|
||||
list.add(new InsertBatchSomeColumn(t -> t.getFieldFill() != FieldFill.UPDATE));
|
||||
// 分页查询 返回 VO 对象
|
||||
list.add(new SelectByPage());
|
||||
return new CustomSqlInjector(list);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.hccake.ballcat.common.conf.web;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.hccake.ballcat.common.core.domain.PageParam;
|
||||
import com.hccake.ballcat.common.core.exception.SqlCheckedException;
|
||||
import com.hccake.ballcat.common.core.result.BaseResultCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -22,7 +21,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
* 解决Mybatis Plus Order By SQL注入问题
|
||||
*/
|
||||
@Slf4j
|
||||
public class SqlFilterArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
public class PageParamArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private final static String[] KEYWORDS = { "master", "truncate", "insert", "select", "delete", "update", "declare",
|
||||
"alter", "drop", "sleep" };
|
||||
@@ -34,7 +33,7 @@ public class SqlFilterArgumentResolver implements HandlerMethodArgumentResolver
|
||||
*/
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return parameter.getParameterType().equals(Page.class);
|
||||
return parameter.getParameterType().equals(PageParam.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,12 +56,12 @@ public class SqlFilterArgumentResolver implements HandlerMethodArgumentResolver
|
||||
String sortField = request.getParameter("sortField");
|
||||
String sortAsc = request.getParameter("sortAsc");
|
||||
|
||||
Page<?> page = new Page<>();
|
||||
PageParam pageParam = new PageParam();
|
||||
if (StrUtil.isNotBlank(current)) {
|
||||
page.setCurrent(Long.parseLong(current));
|
||||
pageParam.setCurrent(Long.parseLong(current));
|
||||
}
|
||||
if (StrUtil.isNotBlank(size)) {
|
||||
page.setSize(Long.parseLong(size));
|
||||
pageParam.setSize(Long.parseLong(size));
|
||||
}
|
||||
|
||||
if (StrUtil.isNotEmpty(sortField)) {
|
||||
@@ -72,11 +71,11 @@ public class SqlFilterArgumentResolver implements HandlerMethodArgumentResolver
|
||||
sortField = StrUtil.toUnderlineCase(sortField);
|
||||
// 正序/倒序
|
||||
boolean isAsc = (StrUtil.isNotBlank(sortAsc) && Boolean.parseBoolean(sortAsc));
|
||||
OrderItem orderItem = isAsc ? OrderItem.asc(sortField) : OrderItem.desc(sortField);
|
||||
|
||||
page.addOrder(orderItem);
|
||||
pageParam.setSortAsc(isAsc);
|
||||
pageParam.setSortField(sortField);
|
||||
}
|
||||
return page;
|
||||
|
||||
return pageParam;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,7 +22,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
*/
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
argumentResolvers.add(new SqlFilterArgumentResolver());
|
||||
argumentResolvers.add(new PageParamArgumentResolver());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -62,5 +62,13 @@
|
||||
<groupId>org.jsoup</groupId>
|
||||
<artifactId>jsoup</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.hccake.ballcat.common.core.domain;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 分页查询参数
|
||||
*
|
||||
* @author Hccake 2021/1/18
|
||||
* @version 1.0
|
||||
*/
|
||||
@Data
|
||||
@ApiModel("分页查询参数")
|
||||
public class PageParam {
|
||||
|
||||
/**
|
||||
* 当前页
|
||||
*/
|
||||
@ApiModelProperty(value = "当前页码,从 1 开始", required = true, example = "1")
|
||||
@NotNull(message = "当前页码不能为空")
|
||||
@Min(value = 1, message = "当前页不能小于 1")
|
||||
private long current = 1;
|
||||
|
||||
/**
|
||||
* 每页显示条数,默认 10
|
||||
*/
|
||||
@ApiModelProperty(value = "每页条数,最大值为 100", required = true, example = "10")
|
||||
@NotNull(message = "每页条数不能为空")
|
||||
@Range(min = 1, max = 100, message = "条数范围为 [1, 100]")
|
||||
private long size = 10;
|
||||
|
||||
/**
|
||||
* 排序字段
|
||||
*/
|
||||
@ApiModelProperty(value = "排序字段")
|
||||
private String sortField;
|
||||
|
||||
/**
|
||||
* 是否正序排序
|
||||
*/
|
||||
@ApiModelProperty(value = "是否正序排序")
|
||||
private boolean sortAsc;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.hccake.ballcat.common.core.domain;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页返回结果
|
||||
*
|
||||
* @author Hccake 2021/1/18
|
||||
* @version 1.0
|
||||
*/
|
||||
@Data
|
||||
@ApiModel("分页返回结果")
|
||||
public class PageResult<T> {
|
||||
|
||||
/**
|
||||
* 查询数据列表
|
||||
*/
|
||||
@ApiModelProperty(value = "分页数据", required = true)
|
||||
protected List<T> records = Collections.emptyList();
|
||||
|
||||
/**
|
||||
* 总数
|
||||
*/
|
||||
@ApiModelProperty(value = "数据总量", required = true)
|
||||
protected Long total = 0L;
|
||||
|
||||
public PageResult() {
|
||||
}
|
||||
|
||||
public PageResult(long total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public PageResult(List<T> records, long total) {
|
||||
this.records = records;
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.hccake.ballcat.common.core.domain;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 下拉框所对应的视图类
|
||||
*
|
||||
* @author Hccake
|
||||
*/
|
||||
@Data
|
||||
@ApiModel("下拉框数据")
|
||||
public class SelectData<T> {
|
||||
|
||||
/**
|
||||
* 显示的数据
|
||||
*/
|
||||
@ApiModelProperty(value = "显示的数据", required = true)
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 选中获取的属性
|
||||
*/
|
||||
@ApiModelProperty(value = "选中获取的属性", required = true)
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* 是否被选中
|
||||
*/
|
||||
@ApiModelProperty(value = "是否被选中")
|
||||
private String selected;
|
||||
|
||||
/**
|
||||
* 是否禁用
|
||||
*/
|
||||
@ApiModelProperty(value = "是否禁用")
|
||||
private String disabled;
|
||||
|
||||
/**
|
||||
* 分组标识
|
||||
*/
|
||||
@ApiModelProperty(value = "分组标识")
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 扩展对象
|
||||
*/
|
||||
@ApiModelProperty(value = "扩展对象")
|
||||
private T extendObj;
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package com.hccake.ballcat.common.core.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 下拉框所对应的视图类
|
||||
*
|
||||
* @author Hccake
|
||||
*/
|
||||
@Data
|
||||
public class SelectData<T> {
|
||||
|
||||
/**
|
||||
* 显示的数据
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 选中获取的属性
|
||||
*/
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* 是否被选中
|
||||
*/
|
||||
private String selected;
|
||||
|
||||
/**
|
||||
* 是否禁用
|
||||
*/
|
||||
private String disabled;
|
||||
|
||||
/**
|
||||
* 分组标识
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 扩展对象
|
||||
*/
|
||||
private T extendObj;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user