更改了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; 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;

View File

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

View File

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