提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.mybatis.core.query; |
H |
2 |
|
|
3 |
import cn.hutool.core.lang.Assert; |
|
4 |
import com.iailab.framework.mybatis.core.enums.SqlConstants; |
|
5 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
6 |
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; |
|
7 |
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
|
8 |
import org.springframework.util.StringUtils; |
|
9 |
|
|
10 |
import java.util.Collection; |
|
11 |
|
|
12 |
/** |
|
13 |
* 拓展 MyBatis Plus QueryWrapper 类,主要增加如下功能: |
|
14 |
* |
|
15 |
* 1. 拼接条件的方法,增加 xxxIfPresent 方法,用于判断值不存在的时候,不要拼接到条件中。 |
|
16 |
* |
|
17 |
* @param <T> 数据类型 |
|
18 |
*/ |
|
19 |
public class QueryWrapperX<T> extends QueryWrapper<T> { |
|
20 |
|
|
21 |
public QueryWrapperX<T> likeIfPresent(String column, String val) { |
|
22 |
if (StringUtils.hasText(val)) { |
|
23 |
return (QueryWrapperX<T>) super.like(column, val); |
|
24 |
} |
|
25 |
return this; |
|
26 |
} |
|
27 |
|
|
28 |
public QueryWrapperX<T> inIfPresent(String column, Collection<?> values) { |
|
29 |
if (!CollectionUtils.isEmpty(values)) { |
|
30 |
return (QueryWrapperX<T>) super.in(column, values); |
|
31 |
} |
|
32 |
return this; |
|
33 |
} |
|
34 |
|
|
35 |
public QueryWrapperX<T> inIfPresent(String column, Object... values) { |
|
36 |
if (!ArrayUtils.isEmpty(values)) { |
|
37 |
return (QueryWrapperX<T>) super.in(column, values); |
|
38 |
} |
|
39 |
return this; |
|
40 |
} |
|
41 |
|
|
42 |
public QueryWrapperX<T> eqIfPresent(String column, Object val) { |
|
43 |
if (val != null) { |
|
44 |
return (QueryWrapperX<T>) super.eq(column, val); |
|
45 |
} |
|
46 |
return this; |
|
47 |
} |
|
48 |
|
|
49 |
public QueryWrapperX<T> neIfPresent(String column, Object val) { |
|
50 |
if (val != null) { |
|
51 |
return (QueryWrapperX<T>) super.ne(column, val); |
|
52 |
} |
|
53 |
return this; |
|
54 |
} |
|
55 |
|
|
56 |
public QueryWrapperX<T> gtIfPresent(String column, Object val) { |
|
57 |
if (val != null) { |
|
58 |
return (QueryWrapperX<T>) super.gt(column, val); |
|
59 |
} |
|
60 |
return this; |
|
61 |
} |
|
62 |
|
|
63 |
public QueryWrapperX<T> geIfPresent(String column, Object val) { |
|
64 |
if (val != null) { |
|
65 |
return (QueryWrapperX<T>) super.ge(column, val); |
|
66 |
} |
|
67 |
return this; |
|
68 |
} |
|
69 |
|
|
70 |
public QueryWrapperX<T> ltIfPresent(String column, Object val) { |
|
71 |
if (val != null) { |
|
72 |
return (QueryWrapperX<T>) super.lt(column, val); |
|
73 |
} |
|
74 |
return this; |
|
75 |
} |
|
76 |
|
|
77 |
public QueryWrapperX<T> leIfPresent(String column, Object val) { |
|
78 |
if (val != null) { |
|
79 |
return (QueryWrapperX<T>) super.le(column, val); |
|
80 |
} |
|
81 |
return this; |
|
82 |
} |
|
83 |
|
|
84 |
public QueryWrapperX<T> betweenIfPresent(String column, Object val1, Object val2) { |
|
85 |
if (val1 != null && val2 != null) { |
|
86 |
return (QueryWrapperX<T>) super.between(column, val1, val2); |
|
87 |
} |
|
88 |
if (val1 != null) { |
|
89 |
return (QueryWrapperX<T>) ge(column, val1); |
|
90 |
} |
|
91 |
if (val2 != null) { |
|
92 |
return (QueryWrapperX<T>) le(column, val2); |
|
93 |
} |
|
94 |
return this; |
|
95 |
} |
|
96 |
|
|
97 |
public QueryWrapperX<T> betweenIfPresent(String column, Object[] values) { |
|
98 |
if (values!= null && values.length != 0 && values[0] != null && values[1] != null) { |
|
99 |
return (QueryWrapperX<T>) super.between(column, values[0], values[1]); |
|
100 |
} |
|
101 |
if (values!= null && values.length != 0 && values[0] != null) { |
|
102 |
return (QueryWrapperX<T>) ge(column, values[0]); |
|
103 |
} |
|
104 |
if (values!= null && values.length != 0 && values[1] != null) { |
|
105 |
return (QueryWrapperX<T>) le(column, values[1]); |
|
106 |
} |
|
107 |
return this; |
|
108 |
} |
|
109 |
|
|
110 |
// ========== 重写父类方法,方便链式调用 ========== |
|
111 |
|
|
112 |
@Override |
|
113 |
public QueryWrapperX<T> eq(boolean condition, String column, Object val) { |
|
114 |
super.eq(condition, column, val); |
|
115 |
return this; |
|
116 |
} |
|
117 |
|
|
118 |
@Override |
|
119 |
public QueryWrapperX<T> eq(String column, Object val) { |
|
120 |
super.eq(column, val); |
|
121 |
return this; |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public QueryWrapperX<T> orderByDesc(String column) { |
|
126 |
super.orderByDesc(true, column); |
|
127 |
return this; |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public QueryWrapperX<T> last(String lastSql) { |
|
132 |
super.last(lastSql); |
|
133 |
return this; |
|
134 |
} |
|
135 |
|
|
136 |
@Override |
|
137 |
public QueryWrapperX<T> in(String column, Collection<?> coll) { |
|
138 |
super.in(column, coll); |
|
139 |
return this; |
|
140 |
} |
|
141 |
|
|
142 |
/** |
|
143 |
* 设置只返回最后一条 |
|
144 |
* |
|
145 |
* TODO iailab:不是完美解,需要在思考下。如果使用多数据源,并且数据源是多种类型时,可能会存在问题:实现之返回一条的语法不同 |
|
146 |
* |
|
147 |
* @return this |
|
148 |
*/ |
|
149 |
public QueryWrapperX<T> limitN(int n) { |
|
150 |
Assert.notNull(SqlConstants.DB_TYPE, "获取不到数据库的类型"); |
|
151 |
switch (SqlConstants.DB_TYPE) { |
|
152 |
case ORACLE: |
|
153 |
case ORACLE_12C: |
|
154 |
super.le("ROWNUM", n); |
|
155 |
break; |
|
156 |
case SQL_SERVER: |
|
157 |
case SQL_SERVER2005: |
|
158 |
super.select("TOP " + n + " *"); // 由于 SQL Server 是通过 SELECT TOP 1 实现限制一条,所以只好使用 * 查询剩余字段 |
|
159 |
break; |
|
160 |
default: |
|
161 |
super.last("LIMIT " + n); |
|
162 |
} |
|
163 |
return this; |
|
164 |
} |
|
165 |
|
|
166 |
} |