商品模块代码提交
This commit is contained in:
@@ -1,95 +1,65 @@
|
||||
package com.czg.core.page;
|
||||
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import io.micrometer.common.util.StringUtils;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页数据
|
||||
*
|
||||
* @author cashier
|
||||
*/
|
||||
@Data
|
||||
public class PageDomain {
|
||||
/**
|
||||
* 当前记录起始索引
|
||||
*/
|
||||
private Integer pageNum;
|
||||
private Integer page;
|
||||
|
||||
/**
|
||||
* 每页显示记录数
|
||||
*/
|
||||
private Integer pageSize;
|
||||
private Integer size;
|
||||
|
||||
/**
|
||||
* 排序列
|
||||
* 排序列,多个用逗号隔开
|
||||
*/
|
||||
private String orderByColumn;
|
||||
private String orderField;
|
||||
|
||||
/**
|
||||
* 排序的方向desc或者asc
|
||||
* 排序方式,多个用逗号分隔,asc:升序,desc:降序
|
||||
*/
|
||||
private String isAsc = "asc";
|
||||
private String order;
|
||||
|
||||
/**
|
||||
* 分页参数合理化
|
||||
* 组合后的排序规则
|
||||
*/
|
||||
private Boolean reasonable = true;
|
||||
private String orderBy;
|
||||
|
||||
|
||||
public String getOrderBy() {
|
||||
if (StrUtil.isEmpty(orderByColumn)) {
|
||||
if (StrUtil.isEmpty(orderField)) {
|
||||
return "";
|
||||
}
|
||||
return StrUtil.toUnderlineCase(orderByColumn) + " " + isAsc;
|
||||
}
|
||||
|
||||
public Integer getPageNum() {
|
||||
return pageNum;
|
||||
}
|
||||
|
||||
public void setPageNum(Integer pageNum) {
|
||||
this.pageNum = pageNum;
|
||||
}
|
||||
|
||||
public Integer getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
public void setPageSize(Integer pageSize) {
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
|
||||
public String getOrderByColumn() {
|
||||
return orderByColumn;
|
||||
}
|
||||
|
||||
public void setOrderByColumn(String orderByColumn) {
|
||||
this.orderByColumn = orderByColumn;
|
||||
}
|
||||
|
||||
public String getIsAsc() {
|
||||
return isAsc;
|
||||
}
|
||||
|
||||
public void setIsAsc(String isAsc) {
|
||||
if (StringUtils.isNotEmpty(isAsc)) {
|
||||
// 兼容前端排序类型
|
||||
if ("ascending".equals(isAsc)) {
|
||||
isAsc = "asc";
|
||||
} else if ("descending".equals(isAsc)) {
|
||||
isAsc = "desc";
|
||||
}
|
||||
this.isAsc = isAsc;
|
||||
if (StrUtil.isEmpty(orderBy)) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean getReasonable() {
|
||||
if (reasonable == null) {
|
||||
return Boolean.TRUE;
|
||||
String[] orderFields = orderField.split(",");
|
||||
String[] orders = orderBy.split(",");
|
||||
if (orderFields.length != orders.length) {
|
||||
return "";
|
||||
}
|
||||
return reasonable;
|
||||
List<String> orderBy = new ArrayList<>();
|
||||
for (int i = 0; i < orderFields.length; i++) {
|
||||
String orderField = orderFields[i];
|
||||
String order = orders[i];
|
||||
orderBy.add(StrUtil.toUnderlineCase(orderField) + " " + order);
|
||||
}
|
||||
return CollUtil.join(orderBy, ",");
|
||||
}
|
||||
|
||||
public void setReasonable(Boolean reasonable) {
|
||||
this.reasonable = reasonable;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,10 +61,11 @@ public class PageQuery implements Serializable {
|
||||
|
||||
/**
|
||||
* 构造分页查询参数
|
||||
*
|
||||
* @param <T>
|
||||
* @return
|
||||
* @param <T>
|
||||
*/
|
||||
public static <T> Page<T> build() {
|
||||
public static <T> Page<T> build() {
|
||||
Integer pageNum = Convert.toInt(TableSupport.getHttpServletRequest().getParameter(PAGE_NUM), DEFAULT_PAGE_NUM);
|
||||
Integer pageSize = Convert.toInt(TableSupport.getHttpServletRequest().getParameter(PAGE_SIZE), DEFAULT_PAGE_SIZE);
|
||||
if (pageNum <= 0) {
|
||||
|
||||
@@ -11,58 +11,49 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
*
|
||||
* @author Cashier
|
||||
*/
|
||||
public class TableSupport
|
||||
{
|
||||
public class TableSupport {
|
||||
/**
|
||||
* 当前记录起始索引
|
||||
*/
|
||||
public static final String PAGE_NUM = "pageNum";
|
||||
public static final String PAGE = "page";
|
||||
|
||||
/**
|
||||
* 每页显示记录数
|
||||
*/
|
||||
public static final String PAGE_SIZE = "pageSize";
|
||||
public static final String SIZE = "size";
|
||||
|
||||
/**
|
||||
* 排序列
|
||||
* 排序字段,多个用逗号分隔
|
||||
*/
|
||||
public static final String ORDER_BY_COLUMN = "orderByColumn";
|
||||
public static final String ORDER_FIELD = "orderField";
|
||||
|
||||
/**
|
||||
* 排序的方向 "desc" 或者 "asc".
|
||||
* 排序方式,多个用逗号分隔,asc:升序,desc:降序
|
||||
*/
|
||||
public static final String IS_ASC = "isAsc";
|
||||
public static final String ORDER = "order";
|
||||
|
||||
/**
|
||||
* 分页参数合理化
|
||||
*/
|
||||
public static final String REASONABLE = "reasonable";
|
||||
|
||||
/**
|
||||
* 封装分页对象
|
||||
*/
|
||||
public static PageDomain getPageDomain()
|
||||
{
|
||||
public static PageDomain getPageDomain() {
|
||||
PageDomain pageDomain = new PageDomain();
|
||||
pageDomain.setPageNum(Convert.toInt(getHttpServletRequest().getParameter(PAGE_NUM), 1));
|
||||
pageDomain.setPageSize(Convert.toInt(getHttpServletRequest().getParameter(PAGE_SIZE), 10));
|
||||
pageDomain.setOrderByColumn(getHttpServletRequest().getParameter(ORDER_BY_COLUMN));
|
||||
pageDomain.setIsAsc(getHttpServletRequest().getParameter(IS_ASC));
|
||||
pageDomain.setReasonable(Convert.toBool(getHttpServletRequest().getParameter(REASONABLE)));
|
||||
pageDomain.setPage(Convert.toInt(getHttpServletRequest().getParameter(PAGE), 1));
|
||||
pageDomain.setPage(Convert.toInt(getHttpServletRequest().getParameter(SIZE), 10));
|
||||
pageDomain.setOrderField(getHttpServletRequest().getParameter(ORDER_FIELD));
|
||||
pageDomain.setOrder(getHttpServletRequest().getParameter(ORDER));
|
||||
return pageDomain;
|
||||
}
|
||||
|
||||
public static PageDomain buildPageRequest()
|
||||
{
|
||||
public static PageDomain buildPageRequest() {
|
||||
return getPageDomain();
|
||||
}
|
||||
|
||||
public static HttpServletRequest getHttpServletRequest() {
|
||||
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||
if(requestAttributes == null){
|
||||
if (requestAttributes == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ((ServletRequestAttributes) requestAttributes).getRequest();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,54 +1,12 @@
|
||||
package com.czg.core.service;
|
||||
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
import com.mybatisflex.core.row.Db;
|
||||
import com.mybatisflex.core.service.IService;
|
||||
import com.mybatisflex.core.util.ClassUtil;
|
||||
import com.mybatisflex.core.util.SqlUtil;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 自定义的服务基类接口
|
||||
*
|
||||
* @author dataprince数据小王子
|
||||
* @author Cashier
|
||||
*/
|
||||
public interface IBaseService<T> extends IService<T> {
|
||||
|
||||
/**
|
||||
* <p>带主键保存实体类对象数据,适用于中间表有联合主键场合但是不通过主键生成器生成主键值,而是程序自己提供主键值。
|
||||
*
|
||||
* @param entity 实体类对象
|
||||
* @return 受影响的行数
|
||||
* @apiNote 默认调用的是 {@link BaseMapper#insertSelectiveWithPk(Object)} 方法,忽略实体类
|
||||
* {@code null} 属性的数据,使数据库配置的默认值生效。
|
||||
*/
|
||||
default int saveWithPk(T entity) {
|
||||
return getMapper().insertSelectiveWithPk(entity);
|
||||
}
|
||||
/**
|
||||
* <p>带主键批量保存实体类对象数据,适用于中间表有联合主键场合但是不通过主键生成器生成主键值,而是程序自己提供主键值,例如sys_role_menu。
|
||||
*
|
||||
* @param entities 实体类对象
|
||||
* @return {@code true} 保存成功,{@code false} 保存失败。
|
||||
* @apiNote 默认调用的是 {@link BaseMapper#insertSelectiveWithPk(Object)} 方法,忽略实体类
|
||||
* {@code null} 属性的数据,使数据库配置的默认值生效。
|
||||
*/
|
||||
default boolean saveBatchWithPk(Collection<T> entities) {
|
||||
return saveBatchWithPk(entities, DEFAULT_BATCH_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>带主键批量保存实体类对象数据,适用于中间表有联合主键场合但是不通过主键生成器生成主键值,而是程序自己提供主键值,例如sys_role_menu。
|
||||
*
|
||||
* @param entities 实体类对象
|
||||
* @param batchSize 每次保存切分的数量
|
||||
* @return {@code true} 保存成功,{@code false} 保存失败。
|
||||
* @apiNote 默认调用的是 {@link BaseMapper#insertSelectiveWithPk(Object)} 方法,忽略实体类
|
||||
* {@code null} 属性的数据,使数据库配置的默认值生效。
|
||||
*/
|
||||
default boolean saveBatchWithPk(Collection<T> entities, int batchSize) {
|
||||
Class<BaseMapper<T>> usefulClass = (Class<BaseMapper<T>>) ClassUtil.getUsefulClass(getMapper().getClass());
|
||||
return SqlUtil.toBool(Db.executeBatch(entities, batchSize, usefulClass, BaseMapper::insertSelectiveWithPk));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
/**
|
||||
* 自定义的服务基类接口实现
|
||||
*
|
||||
* @author dataprince数据小王子
|
||||
* @author Cashier
|
||||
*/
|
||||
public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M , T> implements IBaseService<T> {
|
||||
|
||||
@@ -20,7 +20,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M ,
|
||||
* 构造基本查询条件
|
||||
* @return QueryWrapper
|
||||
*/
|
||||
protected QueryWrapper buildBaseQueryWrapper(){
|
||||
protected QueryWrapper buildPageQueryWrapper(){
|
||||
QueryWrapper queryWrapper = query();
|
||||
PageDomain pageDomain = TableSupport.buildPageRequest();
|
||||
if (StrUtil.isNotEmpty(pageDomain.getOrderBy())) {
|
||||
|
||||
Reference in New Issue
Block a user