1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
| package com.yeshimin.unknown.common.config.mybatis;
import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.yeshimin.unknown.common.consts.Common; import com.yeshimin.unknown.domain.base.BaseQueryDto; import com.yeshimin.unknown.domain.base.ConditionBaseEntity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List;
@Slf4j public class QueryHelper<T> {
public static <T> QueryWrapper<T> getQueryWrapper(Object query) { return getQueryWrapper(query, Wrappers.query()); }
public static <T> QueryWrapper<T> getQueryWrapper(Object query, Class<T> clazz) { QueryWrapper<T> wrapper = new QueryWrapper<>(); if (clazz.isInstance(query)) { wrapper.setEntity(clazz.cast(query)); } return getQueryWrapper(query, wrapper); }
public static <T> QueryWrapper<T> getQueryWrapper(Object query, QueryWrapper<T> wrapper) { Query queryAnno = query.getClass().getAnnotation(Query.class); boolean queryEnabled = queryAnno != null && queryAnno.enabled(); boolean queryCustom = queryAnno != null && queryAnno.custom(); if (!queryEnabled) { log.debug("class: [{}], @Query is not enabled, skip", query.getClass().getName()); return wrapper; }
List<Condition> listCondition = parseConditions(queryCustom, query);
Field[] fields = query.getClass().getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(Common.CONDITIONS_FIELD_NAME)) { log.debug("skip 'conditions' field"); continue; }
QueryField queryField = field.getAnnotation(QueryField.class); if (queryField == null) { log.debug("{} is not annotated with @QueryField, skip", field.getName()); continue; }
boolean isAccessible = field.isAccessible(); field.setAccessible(true); try { Object value = field.get(query);
QueryField.ConditionStrategy conditionStrategy = queryField.conditionStrategy(); if (conditionStrategy == QueryField.ConditionStrategy.NOT_BLANK) { if (value == null) { log.debug("{} is null, skip", field.getName()); continue; } if (value instanceof String && StrUtil.isBlank((String) value)) { log.debug("{} is blank, skip", field.getName()); continue; } } else if (conditionStrategy == QueryField.ConditionStrategy.NOT_NULL) { if (value == null) { log.debug("{} is null, skip", field.getName()); continue; } } else { log.warn("conditionStrategy is not supported, skip"); continue; }
QueryField.Type type = queryField.value() == QueryField.Type.DEFAULT ? (queryField.type() == QueryField.Type.DEFAULT ? (QueryField.Type.EQ) : (queryField.type())) : queryField.value();
listCondition.add(new Condition(field.getName(), type, value)); } catch (IllegalAccessException e) { log.error("{}", e.getMessage()); e.printStackTrace(); } finally { field.setAccessible(isAccessible); } }
for (Condition condition : listCondition) { String fieldName = condition.getProperty(); String columnName = StrUtil.toUnderlineCase(fieldName); QueryField.Type operator = condition.getOperator(); Object value = condition.getValue();
switch (operator) { case EQ: wrapper.eq(columnName, value); break; case NE: wrapper.ne(columnName, value); break; case GT: wrapper.gt(columnName, value); break; case GE: wrapper.ge(columnName, value); break; case LT: wrapper.lt(columnName, value); break; case LE: wrapper.le(columnName, value); break; case IN: if (value instanceof Object[]) { if (((Object[]) value).length > 0) { wrapper.in(columnName, (Object[]) value); } else { log.warn("{} is empty, skip", fieldName); } } else if (value instanceof Collection) { if (!((Collection<?>) value).isEmpty()) { wrapper.in(columnName, (Collection<?>) value); } else { log.warn("{} is empty, skip", fieldName); } } break; case NOT_IN: if (value instanceof Object[]) { if (((Object[]) value).length > 0) { wrapper.notIn(columnName, (Object[]) value); } else { log.warn("{} is empty, skip", fieldName); } } else if (value instanceof Collection) { if (!((Collection<?>) value).isEmpty()) { wrapper.notIn(columnName, (Collection<?>) value); } else { log.warn("{} is empty, skip", fieldName); } } break; case IS_NULL: wrapper.isNull(columnName); break; case IS_NOT_NULL: wrapper.isNotNull(columnName); break; case BETWEEN: if (value instanceof Object[]) { Object[] arr = (Object[]) value; if (arr.length == 2) { wrapper.between(columnName, arr[0], arr[1]); } else { log.warn("{} is invalid, skip", fieldName); } } else if (value instanceof Collection) { Collection<?> col = (Collection<?>) value; if (col.size() == 2) { Iterator<?> iterator = col.iterator(); wrapper.between(columnName, iterator.next(), iterator.next()); } else { log.warn("{} is invalid, skip", fieldName); } } else { log.warn("{} is invalid, skip", fieldName); } break; case NOT_BETWEEN: if (value instanceof Object[]) { Object[] arr = (Object[]) value; if (arr.length == 2) { wrapper.notBetween(columnName, arr[0], arr[1]); } else { log.warn("{} is invalid, skip", fieldName); } } else if (value instanceof Collection) { Collection<?> col = (Collection<?>) value; if (col.size() == 2) { Iterator<?> iterator = col.iterator(); wrapper.notBetween(columnName, iterator.next(), iterator.next()); } else { log.warn("{} is invalid, skip", fieldName); } } else { log.warn("{} is invalid, skip", fieldName); } break; case LIKE: wrapper.like(columnName, value); break; case LIKE_LEFT: wrapper.likeLeft(columnName, value); break; case LIKE_RIGHT: wrapper.likeRight(columnName, value); break; case NOT_LIKE: wrapper.notLike(columnName, value); break; case NOT_LIKE_LEFT: wrapper.notLikeLeft(columnName, value); break; case NOT_LIKE_RIGHT: wrapper.notLikeRight(columnName, value); break; default: break; } }
return wrapper; }
private static List<Condition> parseConditions(boolean queryCustom, Object query) { String conditions = null; if (queryCustom) { if (query instanceof BaseQueryDto) { conditions = ((BaseQueryDto) query).getConditions_(); } else if (query instanceof ConditionBaseEntity) { conditions = ((ConditionBaseEntity<?>) query).getConditions_(); } } return conditions == null ? new LinkedList<>() : parseConditions(conditions); }
private static List<Condition> parseConditions(String conditions) { List<Condition> list = new LinkedList<>(); if (StrUtil.isBlank(conditions)) { log.debug("conditions is blank, ignore"); return list; } String[] arr = conditions.split(";"); for (String s : arr) { String[] arr2 = s.split(":"); if (arr2.length != 3) { log.warn("condition [{}] is invalid, ignore", s); continue; } if (StrUtil.isBlank(arr2[0])) { log.warn("condition [{}] -> fieldName is blank, ignore", s); continue; } QueryField.Type operator = QueryField.Type.of(arr2[1]); if (operator == null) { log.warn("condition [{}] -> operator is invalid, ignore", s); continue; }
list.add(new Condition(arr2[0], operator, arr2[2])); } return list; }
@Data @AllArgsConstructor private static class Condition { private String property; QueryField.Type operator; Object value; } }
|