Skip to content

Commit

Permalink
修改逻辑删除对Example的处理逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
abel533 committed Nov 25, 2018
1 parent ff81144 commit cb634b2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 26 deletions.
15 changes: 2 additions & 13 deletions core/src/main/java/tk/mybatis/mapper/mapperhelper/SqlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,6 @@ public static String whereVersion(Class<?> entityClass) {
*/
public static String whereLogicDelete(Class<?> entityClass, boolean isDeleted) {
String value = logicDeleteColumnEqualsValue(entityClass, isDeleted);

return "".equals(value) ? "" : " AND " + value;
}

Expand All @@ -685,12 +684,10 @@ public static String logicDeleteColumnEqualsValue(Class<?> entityClass, boolean
String result = "";
for (EntityColumn column : columnSet) {
hasLogicDelete = isLogicDeleteColumn(entityClass, column, hasLogicDelete);

if (hasLogicDelete) {
result = logicDeleteColumnEqualsValue(column, isDeleted);
}
}

return result;
}

Expand All @@ -711,7 +708,6 @@ public static String logicDeleteColumnEqualsValue(EntityColumn column, boolean i
if (column.getEntityField().isAnnotationPresent(LogicDelete.class)) {
result = column.getColumn() + " = " + getLogicDeletedValue(column, isDeleted);
}

return result;
}

Expand All @@ -725,13 +721,10 @@ public static int getLogicDeletedValue(EntityColumn column, boolean isDeleted) {
if (!column.getEntityField().isAnnotationPresent(LogicDelete.class)) {
throw new LogicDeleteException(column.getColumn() + " 没有 @LogicDelete 注解!");
}

LogicDelete logicDelete = column.getEntityField().getAnnotation(LogicDelete.class);

if (isDeleted) {
return logicDelete.isDeletedValue();
}

return logicDelete.notDeletedValue();
}

Expand All @@ -753,15 +746,12 @@ public static EntityColumn getLogicDeleteColumn(Class<?> entityClass) {
EntityColumn logicDeleteColumn = null;
Set<EntityColumn> columnSet = EntityHelper.getColumns(entityClass);
boolean hasLogicDelete = false;

for (EntityColumn column: columnSet) {
hasLogicDelete = isLogicDeleteColumn(entityClass, column, hasLogicDelete);

if (hasLogicDelete) {
logicDeleteColumn = column;
}
}

return logicDeleteColumn;
}

Expand All @@ -779,7 +769,6 @@ public static boolean isLogicDeleteColumn(Class<?> entityClass, EntityColumn col
}
hasLogicDelete = true;
}

return hasLogicDelete;
}

Expand Down Expand Up @@ -910,7 +899,7 @@ public static String exampleCheck(Class<?> entityClass) {
public static String exampleWhereClause() {
return "<if test=\"_parameter != null\">" +
"<where>\n" +
" ${@tk.mybatis.mapper.util.OGNL@andNotLogicDelete(oredCriteria)}" +
" ${@tk.mybatis.mapper.util.OGNL@andNotLogicDelete(_parameter)}" +
" <trim prefix=\"(\" prefixOverrides=\"and |or \" suffix=\")\">\n" +
" <foreach collection=\"oredCriteria\" item=\"criteria\">\n" +
" <if test=\"criteria.valid\">\n" +
Expand Down Expand Up @@ -950,7 +939,7 @@ public static String exampleWhereClause() {
*/
public static String updateByExampleWhereClause() {
return "<where>\n" +
" ${@tk.mybatis.mapper.util.OGNL@andNotLogicDelete(example.oredCriteria)}" +
" ${@tk.mybatis.mapper.util.OGNL@andNotLogicDelete(example)}" +
" <trim prefix=\"(\" prefixOverrides=\"and |or \" suffix=\")\">\n" +
" <foreach collection=\"example.oredCriteria\" item=\"criteria\">\n" +
" <if test=\"criteria.valid\">\n" +
Expand Down
17 changes: 4 additions & 13 deletions core/src/main/java/tk/mybatis/mapper/util/OGNL.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,39 +229,30 @@ public static String andOr(Object parameter) {

/**
* 拼接逻辑删除字段的未删除查询条件
* @param criteriaList
*
* @param parameter
* @return
*/
public static String andNotLogicDelete(Object parameter) {

if (parameter instanceof List) {
if (parameter instanceof Example) {
try {
// 自定义的example暂时没想到合适的处理方法
List<Example.Criteria> criteriaList = (List<Example.Criteria>) parameter;

List<Example.Criteria> criteriaList = ((Example)parameter).getOredCriteria();
if (criteriaList != null && criteriaList.size() != 0) {
// 随便拿一个得到propertyMap,判断是否有逻辑删除注解的字段
Example.Criteria tempCriteria = criteriaList.get(0);

Map<String, EntityColumn> propertyMap = tempCriteria.getPropertyMap();

for (Map.Entry<String, EntityColumn> entry: propertyMap.entrySet()) {
EntityColumn column = entry.getValue();

if (column.getEntityField().isAnnotationPresent(LogicDelete.class)) {

// 未逻辑删除的条件
return column.getColumn() + " = " + SqlHelper.getLogicDeletedValue(column, false) + " and ";
}
}

}
} catch (ClassCastException e) {
return "";
}

}

return "";
}

Expand Down

0 comments on commit cb634b2

Please sign in to comment.