提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.mybatis.core.query; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.ArrayUtil; |
|
4 |
import cn.hutool.core.util.ObjectUtil; |
|
5 |
import com.iailab.framework.common.util.collection.ArrayUtils; |
|
6 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
7 |
import com.baomidou.mybatisplus.core.toolkit.support.SFunction; |
|
8 |
import org.springframework.util.StringUtils; |
|
9 |
|
|
10 |
import java.util.Collection; |
|
11 |
|
|
12 |
/** |
|
13 |
* 拓展 MyBatis Plus QueryWrapper 类,主要增加如下功能: |
|
14 |
* <p> |
|
15 |
* 1. 拼接条件的方法,增加 xxxIfPresent 方法,用于判断值不存在的时候,不要拼接到条件中。 |
|
16 |
* |
|
17 |
* @param <T> 数据类型 |
|
18 |
*/ |
|
19 |
public class LambdaQueryWrapperX<T> extends LambdaQueryWrapper<T> { |
|
20 |
|
|
21 |
public LambdaQueryWrapperX<T> likeIfPresent(SFunction<T, ?> column, String val) { |
|
22 |
if (StringUtils.hasText(val)) { |
|
23 |
return (LambdaQueryWrapperX<T>) super.like(column, val); |
|
24 |
} |
|
25 |
return this; |
|
26 |
} |
|
27 |
|
|
28 |
public LambdaQueryWrapperX<T> inIfPresent(SFunction<T, ?> column, Collection<?> values) { |
|
29 |
if (ObjectUtil.isAllNotEmpty(values) && !ArrayUtil.isEmpty(values)) { |
|
30 |
return (LambdaQueryWrapperX<T>) super.in(column, values); |
|
31 |
} |
|
32 |
return this; |
|
33 |
} |
|
34 |
|
|
35 |
public LambdaQueryWrapperX<T> inIfPresent(SFunction<T, ?> column, Object... values) { |
|
36 |
if (ObjectUtil.isAllNotEmpty(values) && !ArrayUtil.isEmpty(values)) { |
|
37 |
return (LambdaQueryWrapperX<T>) super.in(column, values); |
|
38 |
} |
|
39 |
return this; |
|
40 |
} |
|
41 |
|
|
42 |
public LambdaQueryWrapperX<T> eqIfPresent(SFunction<T, ?> column, Object val) { |
|
43 |
if (ObjectUtil.isNotEmpty(val)) { |
|
44 |
return (LambdaQueryWrapperX<T>) super.eq(column, val); |
|
45 |
} |
|
46 |
return this; |
|
47 |
} |
|
48 |
|
|
49 |
public LambdaQueryWrapperX<T> neIfPresent(SFunction<T, ?> column, Object val) { |
|
50 |
if (ObjectUtil.isNotEmpty(val)) { |
|
51 |
return (LambdaQueryWrapperX<T>) super.ne(column, val); |
|
52 |
} |
|
53 |
return this; |
|
54 |
} |
|
55 |
|
|
56 |
public LambdaQueryWrapperX<T> gtIfPresent(SFunction<T, ?> column, Object val) { |
|
57 |
if (val != null) { |
|
58 |
return (LambdaQueryWrapperX<T>) super.gt(column, val); |
|
59 |
} |
|
60 |
return this; |
|
61 |
} |
|
62 |
|
|
63 |
public LambdaQueryWrapperX<T> geIfPresent(SFunction<T, ?> column, Object val) { |
|
64 |
if (val != null) { |
|
65 |
return (LambdaQueryWrapperX<T>) super.ge(column, val); |
|
66 |
} |
|
67 |
return this; |
|
68 |
} |
|
69 |
|
|
70 |
public LambdaQueryWrapperX<T> ltIfPresent(SFunction<T, ?> column, Object val) { |
|
71 |
if (val != null) { |
|
72 |
return (LambdaQueryWrapperX<T>) super.lt(column, val); |
|
73 |
} |
|
74 |
return this; |
|
75 |
} |
|
76 |
|
|
77 |
public LambdaQueryWrapperX<T> leIfPresent(SFunction<T, ?> column, Object val) { |
|
78 |
if (val != null) { |
|
79 |
return (LambdaQueryWrapperX<T>) super.le(column, val); |
|
80 |
} |
|
81 |
return this; |
|
82 |
} |
|
83 |
|
|
84 |
public LambdaQueryWrapperX<T> betweenIfPresent(SFunction<T, ?> column, Object val1, Object val2) { |
|
85 |
if (val1 != null && val2 != null) { |
|
86 |
return (LambdaQueryWrapperX<T>) super.between(column, val1, val2); |
|
87 |
} |
|
88 |
if (val1 != null) { |
|
89 |
return (LambdaQueryWrapperX<T>) ge(column, val1); |
|
90 |
} |
|
91 |
if (val2 != null) { |
|
92 |
return (LambdaQueryWrapperX<T>) le(column, val2); |
|
93 |
} |
|
94 |
return this; |
|
95 |
} |
|
96 |
|
|
97 |
public LambdaQueryWrapperX<T> betweenIfPresent(SFunction<T, ?> column, Object[] values) { |
|
98 |
Object val1 = ArrayUtils.get(values, 0); |
|
99 |
Object val2 = ArrayUtils.get(values, 1); |
|
100 |
return betweenIfPresent(column, val1, val2); |
|
101 |
} |
|
102 |
|
|
103 |
// ========== 重写父类方法,方便链式调用 ========== |
|
104 |
|
|
105 |
@Override |
|
106 |
public LambdaQueryWrapperX<T> eq(boolean condition, SFunction<T, ?> column, Object val) { |
|
107 |
super.eq(condition, column, val); |
|
108 |
return this; |
|
109 |
} |
|
110 |
|
|
111 |
@Override |
|
112 |
public LambdaQueryWrapperX<T> eq(SFunction<T, ?> column, Object val) { |
|
113 |
super.eq(column, val); |
|
114 |
return this; |
|
115 |
} |
|
116 |
|
|
117 |
@Override |
|
118 |
public LambdaQueryWrapperX<T> orderByDesc(SFunction<T, ?> column) { |
|
119 |
super.orderByDesc(true, column); |
|
120 |
return this; |
|
121 |
} |
|
122 |
|
|
123 |
@Override |
|
124 |
public LambdaQueryWrapperX<T> last(String lastSql) { |
|
125 |
super.last(lastSql); |
|
126 |
return this; |
|
127 |
} |
|
128 |
|
|
129 |
@Override |
|
130 |
public LambdaQueryWrapperX<T> in(SFunction<T, ?> column, Collection<?> coll) { |
|
131 |
super.in(column, coll); |
|
132 |
return this; |
|
133 |
} |
|
134 |
|
|
135 |
} |