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