更改了WrapperUtils中的allEqAndTimeIntervalQueryWrapper方法,解决了Project实体类中关于逻辑删除的问题
parent
912bf97e3f
commit
fa82a39ee1
|
@ -1,9 +1,9 @@
|
|||
package cn.edu.hfut.rmdjzz.projectmanagement.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
@ -33,7 +33,9 @@ public class Project {
|
|||
private String projectArea;
|
||||
private String projectCompany;
|
||||
private String projectDescription;
|
||||
@TableField("is_completed")
|
||||
private Boolean completed;
|
||||
@TableField("is_deleted")
|
||||
@TableLogic
|
||||
private Boolean deleted;
|
||||
private Integer projectCreator;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cn.edu.hfut.rmdjzz.projectmanagement.utils;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
@ -22,40 +23,57 @@ public class WrapperUtils {
|
|||
* - 对于时间类({@link LocalDateTime}或{@link LocalDate}或{@link LocalTime}),
|
||||
* 会按照key的后缀处理,若为Start则处理为column >= value,End则处理为column <= value
|
||||
*
|
||||
* @param null2IsNull 若为真,将value为null的值在SQL中生成为IS NULL;否则值为null的参数不会在SQL中生成
|
||||
* @param camel2Underline 若为真,则将key的小驼峰命名转为下划线命名风格
|
||||
* @param boolLeftAppendIs 若为真,则将value为boolean型的变量的key前添加IS
|
||||
* @param null2IsNull 若为真,将value为null的值在SQL中生成为IS NULL;否则值为null的参数不会在SQL中生成
|
||||
*/
|
||||
public static <T> QueryWrapper<T> allEqAndTimeIntervalQueryWrapper(Map<String, Object> paramMap, boolean null2IsNull) {
|
||||
public static <T> QueryWrapper<T> allEqAndTimeIntervalQueryWrapper(
|
||||
Map<String, Object> paramMap, boolean camel2Underline, boolean boolLeftAppendIs, boolean null2IsNull
|
||||
) {
|
||||
QueryWrapper<T> wrapper = new QueryWrapper<>();
|
||||
paramMap.forEach((key, value) -> {
|
||||
if (value == null) {
|
||||
if (null2IsNull) {
|
||||
wrapper.isNull(key);
|
||||
String column = camel2Underline ? StringUtils.camelToUnderline(key) : key;
|
||||
wrapper.isNull(column);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (isTimeClass(value.getClass())) {
|
||||
if (key.endsWith("Start")) {
|
||||
wrapper.ge(key.substring(0, key.length() - 5), value);
|
||||
} else if (key.endsWith("End")) {
|
||||
wrapper.le(key.substring(0, key.length() - 3), value);
|
||||
if (isBoolean(value.getClass()) && boolLeftAppendIs) {
|
||||
key = "is" + Character.toUpperCase(key.charAt(0)) + key.substring(1);
|
||||
}
|
||||
String column = camel2Underline ? StringUtils.camelToUnderline(key) : key;
|
||||
if (isTime(value.getClass())) {
|
||||
if (column.endsWith("Start")) {
|
||||
wrapper.ge(column.substring(0, column.length() - 5), value);
|
||||
} else if (column.endsWith("End")) {
|
||||
wrapper.le(column.substring(0, column.length() - 3), value);
|
||||
}
|
||||
return;
|
||||
}
|
||||
wrapper.eq(key, value);
|
||||
wrapper.eq(column, value);
|
||||
});
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link WrapperUtils#allEqAndTimeIntervalQueryWrapper(Map, boolean, boolean, boolean)}的简化调用方法
|
||||
* ,默认开启camel2Underline、boolLeftAppendIs与null2IsNull
|
||||
*/
|
||||
public static <T> QueryWrapper<T> allEqAndTimeIntervalQueryWrapper(Map<String, Object> paramMap) {
|
||||
return allEqAndTimeIntervalQueryWrapper(paramMap, true, true, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从参数map中获取去掉了value为时间类的entry的map
|
||||
* <p>
|
||||
* **若无特殊需求请使用{@link WrapperUtils#allEqAndTimeIntervalQueryWrapper(Map, boolean)}**直接获取wrapper
|
||||
* **若无特殊需求请使用{@link WrapperUtils#allEqAndTimeIntervalQueryWrapper(Map, boolean, boolean, boolean)}**直接获取wrapper
|
||||
*/
|
||||
@Deprecated
|
||||
public static Map<String, Object> getMapWithoutTime(Map<String, Object> map) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
map.forEach((key, value) -> {
|
||||
if (!isTimeClass(value.getClass())) {
|
||||
if (!isTime(value.getClass())) {
|
||||
result.put(key, value);
|
||||
}
|
||||
});
|
||||
|
@ -65,7 +83,11 @@ public class WrapperUtils {
|
|||
/**
|
||||
* 判断参数class是否为时间类({@link LocalDateTime}或{@link LocalDate}或{@link LocalTime})
|
||||
*/
|
||||
public static boolean isTimeClass(Class<?> clazz) {
|
||||
public static boolean isTime(Class<?> clazz) {
|
||||
return LocalDateTime.class.equals(clazz) || LocalDate.class.equals(clazz) || LocalTime.class.equals(clazz);
|
||||
}
|
||||
|
||||
public static boolean isBoolean(Class<?> clazz) {
|
||||
return boolean.class.equals(clazz) || Boolean.class.equals(clazz);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package cn.edu.hfut.rmdjzz.projectmanagement;
|
||||
|
||||
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Project;
|
||||
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Staff;
|
||||
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectService;
|
||||
import cn.edu.hfut.rmdjzz.projectmanagement.service.IStaffService;
|
||||
import cn.edu.hfut.rmdjzz.projectmanagement.utils.WrapperUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
|
@ -8,12 +10,12 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.SneakyThrows;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
|
@ -25,6 +27,8 @@ public class MybatisPlusTests {
|
|||
|
||||
@Resource
|
||||
private IStaffService staffService;
|
||||
@Autowired
|
||||
private IProjectService projectService;
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
|
@ -37,12 +41,26 @@ public class MybatisPlusTests {
|
|||
@Test
|
||||
public void wrapperTest() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("time", "aaa");
|
||||
/*map.put("time", "aaa");
|
||||
map.put("stimeStart", LocalDateTime.now());
|
||||
map.put("etimeEnd", LocalDate.now());*/
|
||||
map.put("null", null);
|
||||
map.put("etimeEnd", LocalDate.now());
|
||||
QueryWrapper<String> wrapper = WrapperUtils.<String>allEqAndTimeIntervalQueryWrapper(map, true);
|
||||
map.put("deleted", true);
|
||||
QueryWrapper<String> wrapper = WrapperUtils.allEqAndTimeIntervalQueryWrapper(map, true, true, true);
|
||||
System.out.println(wrapper.getCustomSqlSegment());
|
||||
System.out.println(objectMapper.writeValueAsString(wrapper.getParamNameValuePairs()));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Test
|
||||
public void nullTest() {
|
||||
System.out.println(objectMapper.writeValueAsString(staffService.list(null)));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Test
|
||||
public void logicDeleteTest() {
|
||||
List<Project> projects = projectService.list();
|
||||
System.out.println(objectMapper.writeValueAsString(projects));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue