forked from sfms3.0/sfms3.0
3 changed files with 139 additions and 0 deletions
@ -0,0 +1,83 @@ |
|||||
|
package com.win.framework.mybatis.core.util; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
|
import com.google.common.base.CaseFormat; |
||||
|
import com.win.framework.mybatis.core.vo.ConditionVo; |
||||
|
import com.win.framework.mybatis.core.vo.FilterVo; |
||||
|
|
||||
|
/** |
||||
|
* QueryWrapper工具类 |
||||
|
*/ |
||||
|
public class QueryWrapperUtils { |
||||
|
|
||||
|
/** |
||||
|
* 根据参数动态组装QueryWrapper |
||||
|
* @param conditionVo filters {column: "userName", action: "==", value: "wenyin"} sorting userName DESC |
||||
|
* @param <T> |
||||
|
* @return |
||||
|
*/ |
||||
|
public static <T> QueryWrapper<T> structure(ConditionVo<T> conditionVo) { |
||||
|
QueryWrapper<T> queryWrapper = new QueryWrapper<>(); |
||||
|
String sorting = conditionVo.getSort(); |
||||
|
if(sorting != null && !sorting.equals("")) { |
||||
|
sorting = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, sorting); |
||||
|
if(conditionVo.getBy() != null && conditionVo.getBy().equalsIgnoreCase("DESC")) { |
||||
|
queryWrapper.orderByDesc(sorting); |
||||
|
} else { |
||||
|
queryWrapper.orderByAsc(sorting); |
||||
|
} |
||||
|
} |
||||
|
if(conditionVo.getFilters() == null || conditionVo.getFilters().size() == 0) { |
||||
|
return queryWrapper; |
||||
|
} |
||||
|
for(FilterVo filterVo : conditionVo.getFilters()) { |
||||
|
String column = filterVo.getColumn(); |
||||
|
column = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, column); |
||||
|
switch (filterVo.getAction()) { |
||||
|
case "==" : |
||||
|
queryWrapper.eq(column, filterVo.getValue()); |
||||
|
break; |
||||
|
case "!=" : |
||||
|
queryWrapper.ne(column, filterVo.getValue()); |
||||
|
break; |
||||
|
case ">" : |
||||
|
queryWrapper.gt(column, filterVo.getValue()); |
||||
|
break; |
||||
|
case "<" : |
||||
|
queryWrapper.lt(column, filterVo.getValue()); |
||||
|
break; |
||||
|
case ">=" : |
||||
|
queryWrapper.ge(column, filterVo.getValue()); |
||||
|
break; |
||||
|
case "<=" : |
||||
|
queryWrapper.le(column, filterVo.getValue()); |
||||
|
break; |
||||
|
case "like" : |
||||
|
queryWrapper.like(column, filterVo.getValue()); |
||||
|
break; |
||||
|
case "in" : |
||||
|
queryWrapper.in(column, filterVo.getValue()); |
||||
|
break; |
||||
|
case "notin" : |
||||
|
queryWrapper.notIn(column, filterVo.getValue()); |
||||
|
break; |
||||
|
case "betweeen" : |
||||
|
String[] strs = filterVo.getValue().split(","); |
||||
|
if(strs.length != 2) { |
||||
|
continue; |
||||
|
} |
||||
|
queryWrapper.between(column, strs[0], strs[1]); |
||||
|
break; |
||||
|
case "isNull" : |
||||
|
queryWrapper.isNull(column); |
||||
|
break; |
||||
|
case "isNotNull" : |
||||
|
queryWrapper.isNotNull(column); |
||||
|
break; |
||||
|
default: |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
return queryWrapper; |
||||
|
} |
||||
|
} |
@ -0,0 +1,35 @@ |
|||||
|
package com.win.framework.mybatis.core.vo; |
||||
|
|
||||
|
import com.win.framework.common.pojo.PageParam; |
||||
|
import io.swagger.v3.oas.annotations.media.Schema; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import lombok.ToString; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 自定义查询条件 |
||||
|
*/ |
||||
|
@Schema(description = "自定义查询条件") |
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@ToString(callSuper = true) |
||||
|
public class ConditionVo<T> extends PageParam { |
||||
|
|
||||
|
/** |
||||
|
* 排序字段 |
||||
|
*/ |
||||
|
private String sort; |
||||
|
|
||||
|
/** |
||||
|
* 正序倒序 |
||||
|
*/ |
||||
|
private String by; |
||||
|
|
||||
|
/** |
||||
|
* 检索字段 |
||||
|
*/ |
||||
|
private List<FilterVo> filters; |
||||
|
|
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
package com.win.framework.mybatis.core.vo; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class FilterVo { |
||||
|
|
||||
|
/** |
||||
|
* 类型,==,!=,>,<,>=,<=,like,in,notIn,betweeen,isNull,isNotNull |
||||
|
*/ |
||||
|
private String action; |
||||
|
/** |
||||
|
* 字段 |
||||
|
*/ |
||||
|
private String column; |
||||
|
/** |
||||
|
* 值 |
||||
|
*/ |
||||
|
private String value; |
||||
|
|
||||
|
} |
Loading…
Reference in new issue