1313import static io .mybatis .mapper .example .ExampleProvider .UPDATE_BY_EXAMPLE_WHERE_CLAUSE ;
1414
1515/**
16+ * 支持逻辑删除的provider实现
17+ * <p>NOTE: 使用时需要在实体类字段上声明@LogicalColumn注解</p>
18+ *
1619 * @author hzw
1720 */
1821public class LogicalProvider {
@@ -35,17 +38,7 @@ private interface LogicalSqlScript extends SqlScript {
3538
3639 default String logicalNotEqualCondition (EntityTable entity ) {
3740 EntityColumn logicalColumn = getLogicalColumn (entity );
38- return " AND " + columnNotEqualsValue (logicalColumn , deleteValue (logicalColumn )) + LF ;
39- }
40-
41-
42- default String ifParameterNotNull (LRSupplier notNullContent , LRSupplier logicalContent ) {
43- return String .format ("<choose><when test=\" _parameter != null\" >%s%n</when><otherwise>%s%n</otherwise></choose> " , notNullContent .getWithLR (), logicalContent .getWithLR ());
44- }
45-
46- @ Override
47- default String ifParameterNotNull (LRSupplier content ) {
48- return String .format ("<if test=\" _parameter != null\" >%s%n</if> " , content .getWithLR ());
41+ return " AND " + columnNotEqualsValueCondition (logicalColumn , deleteValue (logicalColumn )) + LF ;
4942 }
5043 }
5144
@@ -57,11 +50,9 @@ public static String select(ProviderContext providerContext) {
5750 public String getSql (EntityTable entity ) {
5851 return "SELECT " + entity .baseColumnAsPropertyList ()
5952 + " FROM " + entity .table ()
60- + where (
61- () -> logicalNotEqualCondition (entity ) +
62- entity .whereColumns ().stream ().map (column ->
63- ifTest (column .notNullTest (), () -> "AND " + column .columnEqualsProperty ())
64- ).collect (Collectors .joining (LF )))
53+ + where (() -> entity .whereColumns ().stream ()
54+ .map (column -> ifTest (column .notNullTest (), () -> "AND " + column .columnEqualsProperty ()))
55+ .collect (Collectors .joining (LF )) + logicalNotEqualCondition (entity ))
6556 + entity .groupByColumn ().orElse ("" )
6657 + entity .havingColumn ().orElse ("" )
6758 + entity .orderByColumn ().orElse ("" );
@@ -83,12 +74,11 @@ public String getSql(EntityTable entity) {
8374 + choose (() -> whenTest ("fns != null and fns.isNotEmpty()" , () -> "${fns.baseColumnAsPropertyList()}" )
8475 + otherwise (() -> entity .baseColumnAsPropertyList ()))
8576 + " FROM " + entity .tableName ()
86- + trim ("WHERE" , "" , "WHERE |OR |AND " , "" , () -> ifParameterNotNull (() ->
87- where (() ->
88- entity .whereColumns ().stream ().map (column ->
89- ifTest (column .notNullTest ("entity." ), () -> "AND " + column .columnEqualsProperty ("entity." ))
90- ).collect (Collectors .joining (LF )))
91- ) + logicalNotEqualCondition (entity ))
77+ + trim ("WHERE" , "" , "WHERE |OR |AND " , "" , () ->
78+ ifParameterNotNull (() -> where (() -> entity .whereColumns ().stream ()
79+ .map (column -> ifTest (column .notNullTest ("entity." ), () -> "AND " + column .columnEqualsProperty ("entity." )))
80+ .collect (Collectors .joining (LF ))))
81+ + logicalNotEqualCondition (entity ))
9282 + entity .groupByColumn ().orElse ("" )
9383 + entity .havingColumn ().orElse ("" )
9484 + entity .orderByColumn ().orElse ("" );
@@ -154,11 +144,11 @@ public static String selectByPrimaryKey(ProviderContext providerContext) {
154144 return SqlScript .caching (providerContext , new LogicalSqlScript () {
155145 @ Override
156146 public String getSql (EntityTable entity ) {
157- EntityColumn logicalColumn = getLogicalColumn (entity );
158147 return "SELECT " + entity .baseColumnAsPropertyList ()
159148 + " FROM " + entity .table ()
160- + where (() -> logicalNotEqualCondition (entity ) + entity .idColumns ().stream ().map (EntityColumn ::columnEqualsProperty ).collect (Collectors .joining (" AND " ))
161- + " AND " + columnNotEqualsValue (logicalColumn , deleteValue (logicalColumn )));
149+ + where (() -> entity .idColumns ().stream ().map (EntityColumn ::columnEqualsProperty ).collect (Collectors .joining (" AND " )))
150+ // 如果将条件拼接where()中,将会依赖idColumns()的实现,要求其必须返回非空值
151+ + logicalNotEqualCondition (entity );
162152 }
163153 });
164154 }
@@ -176,10 +166,9 @@ public static String selectCount(ProviderContext providerContext) {
176166 public String getSql (EntityTable entity ) {
177167 return "SELECT COUNT(*) FROM " + entity .table () + LF
178168 + where (() ->
179- logicalNotEqualCondition (entity ) +
180- entity .whereColumns ().stream ().map (column ->
181- ifTest (column .notNullTest (), () -> "AND " + column .columnEqualsProperty ())
182- ).collect (Collectors .joining (LF )));
169+ entity .whereColumns ().stream ().map (column ->
170+ ifTest (column .notNullTest (), () -> "AND " + column .columnEqualsProperty ())
171+ ).collect (Collectors .joining (LF )) + logicalNotEqualCondition (entity ));
183172 }
184173 });
185174 }
@@ -276,7 +265,8 @@ public static String updateByPrimaryKey(ProviderContext providerContext) {
276265 public String getSql (EntityTable entity ) {
277266 return "UPDATE " + entity .table ()
278267 + " SET " + entity .updateColumns ().stream ().map (EntityColumn ::columnEqualsProperty ).collect (Collectors .joining ("," ))
279- + where (() -> logicalNotEqualCondition (entity ) + entity .idColumns ().stream ().map (EntityColumn ::columnEqualsProperty ).collect (Collectors .joining (" AND " )));
268+ + where (() -> entity .idColumns ().stream ().map (EntityColumn ::columnEqualsProperty ).collect (Collectors .joining (" AND " )))
269+ + logicalNotEqualCondition (entity );
280270 }
281271 });
282272 }
@@ -297,7 +287,8 @@ public String getSql(EntityTable entity) {
297287 entity .updateColumns ().stream ().map (column ->
298288 ifTest (column .notNullTest (), () -> column .columnEqualsProperty () + "," )
299289 ).collect (Collectors .joining (LF )))
300- + where (() -> logicalNotEqualCondition (entity ) + entity .idColumns ().stream ().map (EntityColumn ::columnEqualsProperty ).collect (Collectors .joining (" AND " )));
290+ + where (() -> entity .idColumns ().stream ().map (EntityColumn ::columnEqualsProperty ).collect (Collectors .joining (" AND " )))
291+ + logicalNotEqualCondition (entity );
301292 }
302293 });
303294 }
@@ -319,9 +310,9 @@ public String getSql(EntityTable entity) {
319310 choose (() ->
320311 whenTest ("fns != null and fns.fieldNames().contains('" + column .property () + "')" , () -> column .columnEqualsProperty ("entity." ) + "," )
321312 + whenTest (column .notNullTest ("entity." ), () -> column .columnEqualsProperty ("entity." ) + "," ))
322-
323313 ).collect (Collectors .joining (LF )))
324- + where (() -> logicalNotEqualCondition (entity ) + entity .idColumns ().stream ().map (column -> column .columnEqualsProperty ("entity." )).collect (Collectors .joining (" AND " )));
314+ + where (() -> entity .idColumns ().stream ().map (column -> column .columnEqualsProperty ("entity." )).collect (Collectors .joining (" AND " )))
315+ + logicalNotEqualCondition (entity );
325316 }
326317 });
327318 }
@@ -337,17 +328,16 @@ public String getSql(EntityTable entity) {
337328 * @return cacheKey
338329 */
339330 public static String delete (ProviderContext providerContext ) {
340- return SqlScript .caching (providerContext , new SqlScript () {
331+ return SqlScript .caching (providerContext , new LogicalSqlScript () {
341332 @ Override
342333 public String getSql (EntityTable entity ) {
343334 EntityColumn logicColumn = getLogicalColumn (entity );
344335 return "UPDATE " + entity .table ()
345336 + " SET " + columnEqualsValue (logicColumn , deleteValue (logicColumn ))
346337 + parameterNotNull ("Parameter cannot be null" )
347- + where (() ->
348- entity .columns ().stream ().map (column ->
349- ifTest (column .notNullTest (), () -> "AND " + column .columnEqualsProperty ())
350- ).collect (Collectors .joining (LF )));
338+ + where (() -> entity .columns ().stream ()
339+ .map (column -> ifTest (column .notNullTest (), () -> "AND " + column .columnEqualsProperty ()))
340+ .collect (Collectors .joining (LF )) + logicalNotEqualCondition (entity ));
351341 }
352342 });
353343 }
@@ -359,12 +349,17 @@ public String getSql(EntityTable entity) {
359349 * @return cacheKey
360350 */
361351 public static String deleteByPrimaryKey (ProviderContext providerContext ) {
362- return SqlScript .caching (providerContext , entity -> {
363- EntityColumn logicColumn = getLogicalColumn (entity );
364- return "UPDATE " + entity .table ()
365- + " SET " + columnEqualsValue (logicColumn , deleteValue (logicColumn ))
366- + " WHERE " + entity .idColumns ().stream ().map (EntityColumn ::columnEqualsProperty ).collect (Collectors .joining (" AND " ));
367- });
352+ return SqlScript .caching (providerContext , new LogicalSqlScript () {
353+ @ Override
354+ public String getSql (EntityTable entity ) {
355+ EntityColumn logicColumn = getLogicalColumn (entity );
356+ return "UPDATE " + entity .table ()
357+ + " SET " + columnEqualsValue (logicColumn , deleteValue (logicColumn ))
358+ + " WHERE " + entity .idColumns ().stream ().map (EntityColumn ::columnEqualsProperty ).collect (Collectors .joining (" AND " ))
359+ + logicalNotEqualCondition (entity );
360+ }
361+ }
362+ );
368363 }
369364
370365 /**
@@ -383,7 +378,7 @@ public static String deleteByExample(ProviderContext providerContext) {
383378 //是否允许空条件,默认允许,允许时不检查查询条件
384379 + (entity .getPropBoolean ("deleteByExample.allowEmpty" , true ) ?
385380 "" : util .variableIsFalse ("_parameter.isEmpty()" , "Example Criteria cannot be empty" ))
386- + EXAMPLE_WHERE_CLAUSE
381+ + EXAMPLE_WHERE_CLAUSE + " AND " + columnNotEqualsValueCondition ( logicColumn , deleteValue ( logicColumn ))
387382 + util .ifTest ("endSql != null and endSql != ''" , () -> "${endSql}" );
388383 });
389384 }
@@ -400,12 +395,16 @@ private static String deleteValue(EntityColumn logicColumn) {
400395 return logicColumn .field ().getAnnotation (LogicalColumn .class ).delete ();
401396 }
402397
403- private static String columnEqualsValue (EntityColumn c , String value ) {
398+ private static String columnEqualsValueCondition (EntityColumn c , String value ) {
404399 return " " + c .column () + choiceEqualsOperator (value ) + value + " " ;
405400 }
406401
407- private static String columnNotEqualsValue (EntityColumn c , String value ) {
408- return " " + c .column () + choiceNotEqualsOperator (value ) + value + " " ;
402+ private static String columnEqualsValue (EntityColumn c , String value ) {
403+ return " " + c .column () + " = " + value + " " ;
404+ }
405+
406+ private static String columnNotEqualsValueCondition (EntityColumn c , String value ) {
407+ return " " + c .column () + choiceNotEqualsOperator (value ) + value ;
409408 }
410409
411410 private static String choiceEqualsOperator (String value ) {
0 commit comments