自定义joinQueryWrapper实现

This commit is contained in:
张松
2025-02-20 14:18:32 +08:00
parent c4f0c9e6d4
commit d8935700f2
4 changed files with 82 additions and 4 deletions

View File

@@ -0,0 +1,69 @@
package com.czg.utils;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.query.*;
import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.core.util.LambdaGetter;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import static com.mybatisflex.core.util.LambdaUtil.getFieldName;
/**
* 关联查询的queryWrapper
* @author Administrator
*/
public class JoinQueryWrapper extends QueryWrapper {
public JoinQueryWrapper() {
super();
}
private static SerializedLambda getSerializedLambda(Serializable getter) {
try {
Method method = getter.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
return (SerializedLambda) method.invoke(getter);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String getImplClassName(SerializedLambda lambda) {
String type = lambda.getInstantiatedMethodType();
return type.substring(2, type.indexOf(";"));
}
private static Class<?> getImplClass0(SerializedLambda lambda) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String implClass = getImplClassName(lambda);
try {
return Class.forName(implClass.replace("/", "."), true, classLoader);
} catch (ClassNotFoundException e) {
throw FlexExceptions.wrap(e);
}
}
public <T> String getColum(LambdaGetter<T> column) {
SerializedLambda lambda = getSerializedLambda(column);
Class<?> entityClass = getImplClass0(lambda);
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
return tableInfo.getTableName() + "." + StrUtil.toUnderlineCase(getFieldName(column));
}
@Override
public <T> JoinQueryWrapper eq(LambdaGetter<T> column, Object value) {
eq(getColum(column), value);
return this;
}
@Override
public <T> JoinQueryWrapper orderBy(LambdaGetter<T> column, Boolean asc) {
orderBy(getColum(column), asc);
return this;
}
}