更改了WrapperUtils中的allEqAndTimeIntervalQueryWrapper方法,解决了Project实体类中关于逻辑删除的问题

master
ArgonarioD 2022-06-30 17:45:39 +08:00
parent 912bf97e3f
commit fa82a39ee1
3 changed files with 60 additions and 18 deletions

View File

@ -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;

View File

@ -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}
* keyStartcolumn >= valueEndcolumn <= value
*
* @param camel2Underline key线
* @param boolLeftAppendIs valuebooleankeyIS
* @param null2IsNull valuenullSQLIS NULLnullSQL
*/
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)}
* camel2UnderlineboolLeftAppendIsnull2IsNull
*/
public static <T> QueryWrapper<T> allEqAndTimeIntervalQueryWrapper(Map<String, Object> paramMap) {
return allEqAndTimeIntervalQueryWrapper(paramMap, true, true, false);
}
/**
* mapvalueentrymap
* <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);
}
}

View File

@ -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));
}
}