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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
| import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Predicate; import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.SharedString; import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; import com.baomidou.mybatisplus.core.toolkit.Assert; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import com.github.yulichang.wrapper.interfaces.Query; import com.mysd.framework.mybatis.wrapper.interfaces.QuickJoinFunction;
import lombok.Data; import lombok.Getter;
/** * 魔改 mybatis-plus-join * * * 允许两张表 允许同时 join 多次 * selectAll 忽略 select = false 的字段 */ public class JoinWrapper<T> extends JoinAbstractLambdaWrapper<T, JoinWrapper<T>> implements Query<JoinWrapper<T>>, LambdaJoin<JoinWrapper<T>> {
/** * */ private static final long serialVersionUID = 1L;
/** * 查询字段 sql */ private SharedString sqlSelect = new SharedString();
/** * 查询表 */ private final SharedString from = new SharedString();
/** * 主表别名 */ private final SharedString alias = new SharedString(Constant.TABLE_ALIAS);
/** * 查询的字段 */ private final List<SelectColumn> selectColumns = new ArrayList<>();
/** * 忽略查询的字段 */ private final List<SelectColumn> ignoreColumns = new ArrayList<>();
/** * 是否 select distinct */ private boolean selectDistinct = false;
/** * 表序号 */ private int tableIndex = 1;
/** * ON sql wrapper集合 */ private final List<JoinWrapper<?>> onWrappers = new ArrayList<>();
/** * 连表关键字 on 条件 func 使用 */ @Getter private String keyWord;
/** * 连表实体类 on 条件 func 使用 */ @Getter private Class<?> joinClass;
/** * 连表别名 */ @Getter private String tableAlias;
/** * 不建议直接 new 该实例,使用 JoinWrapper.<UserDO>lambdaQuery() */ public JoinWrapper() { super.initNeed(); }
/** * 不建议直接 new 该实例,使用 JoinWrapper.<UserDO>lambdaQuery() */ JoinWrapper(T entity, Class<T> entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments, SharedString lastSql, SharedString sqlComment, SharedString sqlFirst, Map<Class<?>, Integer> subTable, String keyWord, Class<?> joinClass, String tableAlias) { super.setEntity(entity); super.setEntityClass(entityClass); this.paramNameSeq = paramNameSeq; this.paramNameValuePairs = paramNameValuePairs; this.expression = mergeSegments; this.sqlSelect = sqlSelect; this.lastSql = lastSql; this.sqlComment = sqlComment; this.sqlFirst = sqlFirst; this.subTable = subTable; this.keyWord = keyWord; this.joinClass = joinClass; this.tableAlias = tableAlias; }
/** * sql去重 select distinct */ public JoinWrapper<T> distinct() { this.selectDistinct = true; return typedThis; }
@Override @SafeVarargs public final <S> JoinWrapper<T> select(SFunction<S, ?>... columns) { if (ArrayUtils.isNotEmpty(columns)) { for (SFunction<S, ?> s : columns) { selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(s), getCache(s).getColumn())); } } return typedThis; }
@Override public <E> JoinWrapper<T> select(Class<E> entityClass, Predicate<TableFieldInfo> predicate) { TableInfo info = TableInfoHelper.getTableInfo(entityClass); Assert.notNull(info, "table can not be find"); info.getFieldList().stream().filter(predicate).collect(Collectors.toList()) .forEach(i -> selectColumns.add(SelectColumn.of(entityClass, i.getColumn()))); return typedThis; }
public <S, X> JoinWrapper<T> selectAs(String tableAlias, SFunction<S, ?> column, SFunction<X, ?> alias) { selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), LambdaUtils.getName(alias), tableAlias)); return typedThis; }
@Override public <S> JoinWrapper<T> selectAs(SFunction<S, ?> column, String alias) { selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), alias)); return typedThis; }
public <S> JoinWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction<S, ?> column, String alias) { if (condition) { selectColumns.add( SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), alias, funcEnum)); } return typedThis; }
@Override public JoinWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, Object column, String alias) { if (condition) { selectColumns.add(SelectColumn.of(null, column.toString(), alias, funcEnum)); } return typedThis; }
public final JoinWrapper<T> selectAll(Class<?> clazz) { TableInfo info = TableInfoHelper.getTableInfo(clazz); Assert.notNull(info, "table can not be find -> %s", clazz); if (info.havePK()) { selectColumns.add(SelectColumn.of(clazz, info.getKeyColumn())); } info.getFieldList().forEach(c -> { if (c.isSelect()) selectColumns.add(SelectColumn.of(clazz, c.getColumn())); }); return typedThis; }
@Override @SafeVarargs public final <S> JoinWrapper<T> selectIgnore(SFunction<S, ?>... columns) { if (ArrayUtils.isNotEmpty(columns)) { for (SFunction<S, ?> s : columns) { ignoreColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(s), getCache(s).getColumn())); } } return typedThis; }
/** * 查询条件 SQL 片段 */ @Override public String getSqlSelect() { if (StringUtils.isBlank(sqlSelect.getStringValue())) { if (CollectionUtils.isNotEmpty(ignoreColumns)) { selectColumns.removeIf(c -> c.getFuncEnum() == null && ignoreColumns.stream().anyMatch( i -> i.getClazz() == c.getClazz() && Objects.equals(c.getColumnName(), i.getColumnName()))); } String s = selectColumns.stream().map(i -> { String str = Constant.TABLE_ALIAS + getDefault( i.getTableAlias() != null ? tableAliasMap.get(i.getTableAlias()) : subTable.get(i.getClazz())) + StringPool.DOT + i.getColumnName(); return (i.getFuncEnum() == null ? str : String.format(i.getFuncEnum().getSql(), str)) + (StringUtils.isBlank(i.getAlias()) ? StringPool.EMPTY : (Constant.AS + i.getAlias())); }).collect(Collectors.joining(StringPool.COMMA)); sqlSelect.setStringValue(s); } return sqlSelect.getStringValue(); }
/** * 获取连表部分语句 */ public String getFrom() { if (StringUtils.isBlank(from.getStringValue())) { StringBuilder value = new StringBuilder(); for (JoinWrapper<?> wrapper : onWrappers) { String tableName = TableInfoHelper.getTableInfo(wrapper.getJoinClass()).getTableName();
String tableNameDefAlias = Constant.TABLE_ALIAS + subTable.get(wrapper.getJoinClass()); String tableNameAlias = Constant.TABLE_ALIAS + (wrapper.getTableAlias() != null ? tableAliasMap.get(wrapper.getTableAlias()) : subTable.get(wrapper.getJoinClass()));
String whereSql = wrapper.getExpression().getNormal().getSqlSegment(); // 为了实现 多次关联的表 可以写在 左边 也可以写在 右边 使用 replace 进行补救 if (!tableNameDefAlias.equals(tableNameAlias)) whereSql = whereSql.replaceAll(tableNameDefAlias + StringPool.BACK_SLASH + StringPool.DOT, tableNameAlias + StringPool.BACK_SLASH + StringPool.DOT);
value.append(wrapper.getKeyWord()).append(tableName).append(StringPool.SPACE).append(tableNameAlias) .append(Constant.ON).append(whereSql); } from.setStringValue(value.toString()); } return from.getStringValue(); }
public String getAlias() { return alias.getStringValue(); }
public boolean getSelectDistinct() { return selectDistinct; }
/** * 用于生成嵌套 sql * <p> * 故 sqlSelect 不向下传递 * </p> */ @Override protected JoinWrapper<T> instance() { return instance(null, null, null); }
protected JoinWrapper<T> instance(String keyWord, Class<?> joinClass, String tableAlias) { return new JoinWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs, new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), this.subTable, keyWord, joinClass, tableAlias); }
@Override public void clear() { super.clear(); sqlSelect.toNull(); from.toNull(); selectColumns.clear(); ignoreColumns.clear(); subTable.clear(); tableAliasMap.clear(); }
@Override public <R> JoinWrapper<T> join(String keyWord, boolean condition, Class<R> clazz, QuickJoinFunction function, String tableAlias) { if (condition) { JoinWrapper<?> apply = function.apply(instance(keyWord, clazz, tableAlias)); onWrappers.add(apply); subTable.put(clazz, tableIndex); if (tableAlias != null) tableAliasMap.put(tableAlias, tableIndex); tableIndex++; } return typedThis; } public JoinWrapper<T> limit(Integer num){ return last("limit " + num); }
/** * select字段 */ @Data private static class SelectColumn {
/** * 字段实体类 */ private Class<?> clazz;
/** * 表 别名 */ private String tableAlias;
/** * 数据库字段名 */ private String columnName;
/** * 字段别名 */ private String alias;
/** * 字段函数 */ private BaseFuncEnum funcEnum;
/** * 自定义函数填充参数 */ private List<SFunction<?, ?>> funcArgs;
private SelectColumn(Class<?> clazz, String columnName, String alias, String tableAlias, BaseFuncEnum funcEnum) { this.clazz = clazz; this.columnName = columnName; this.alias = alias; this.tableAlias = tableAlias; this.funcEnum = funcEnum; }
public static SelectColumn of(Class<?> clazz, String columnName) { return new SelectColumn(clazz, columnName, null, null, null); }
public static SelectColumn of(Class<?> clazz, String columnName, String alias) { return new SelectColumn(clazz, columnName, alias, null, null); }
public static SelectColumn of(Class<?> clazz, String columnName, String alias, BaseFuncEnum funcEnum) { return new SelectColumn(clazz, columnName, alias, null, funcEnum); }
public static SelectColumn of(Class<?> clazz, String columnName, String alias, String tableAlias) { return new SelectColumn(clazz, columnName, alias, tableAlias, null); } } }
|