package com.example.jimmy_web.annotation;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface QpTable {
String value() default "";
}
package com.example.jimmy_web.util;
import com.example.jimmy_web.annotation.SFunction;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class LambdaTypeInfo {
private static Map<Class, SerializedLambda> CLASS_LAMBDA_CACHE = new ConcurrentHashMap<>();
public static <T> String convertToFieldName(SFunction<T> fn) {
SerializedLambda lambda = getSerializedLambda(fn);
String methodName = lambda.getImplMethodName();
String prefix = null;
if (methodName.startsWith("get")) {
prefix = "get";
} else if (methodName.startsWith("is")) {
prefix = "is";
}
if (prefix == null) {
System.out.println("无效的getter方法: " + methodName);
}
return toLowerCaseFirstOne(methodName.replace(prefix, ""));
}
static String toLowerCaseFirstOne(String s) {
if (Character.isLowerCase(s.charAt(0))) {
return s;
} else {
return Character.toLowerCase(s.charAt(0)) + s.substring(1);
}
}
static SerializedLambda getSerializedLambda(Serializable fn) {
SerializedLambda lambda = CLASS_LAMBDA_CACHE.get(fn.getClass());
if (lambda == null) {
try {
Method method = fn.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
lambda = (SerializedLambda) method.invoke(fn);
CLASS_LAMBDA_CACHE.put(fn.getClass(), lambda);
} catch (Exception e) {
e.printStackTrace();
}
}
return lambda;
}
}
package com.example.jimmy_web.controller;
import com.example.jimmy_web.annotation.QpTable;
import com.example.jimmy_web.entity.UserDo;
import com.example.jimmy_web.util.LambdaTypeInfo;
import com.example.jimmy_web.wrapper.MyWrapper;
import org.apache.catalina.User;
import java.lang.annotation.Annotation;
public class Demo {
public static void main(String[] args) {
String s12 = LambdaTypeInfo.convertToFieldName(UserDo::getId);
String s2 = LambdaTypeInfo.convertToFieldName(UserDo::getUserIdName);
Class clazz = UserDo.class;
QpTable qpTable = (QpTable) clazz.getAnnotation(QpTable.class);
System.err.println(qpTable.value());
System.err.println(s12);
System.err.println(s2);
MyWrapper myWrapper = new MyWrapper()
.eq(UserDo::getId,"123")
.ne(UserDo::getId,"1233333")
.ne(UserDo::getName,"hello")
.eq(UserDo::getName,"hello")
;
System.err.println(myWrapper);
}
}
package com.example.jimmy_web.wrapper;
import com.example.jimmy_web.annotation.SFunction;
import com.example.jimmy_web.util.LambdaTypeInfo;
import lombok.ToString;
public class MyWrapper {
private String sql ="";
private String clounm;
private String sql2;
private String clounm2;
private StringBuilder stringBuilder = new StringBuilder(" where 1 = 1 ");
public <T> MyWrapper eq(SFunction<T> fn, String value) {
String attribute = LambdaTypeInfo.convertToFieldName(fn);
this.stringBuilder = stringBuilder.append(" and ").append(attribute).append("=").append(value);
return this;
}
public <T> MyWrapper ne(SFunction<T> fn, String value) {
String attribute = LambdaTypeInfo.convertToFieldName(fn);
this.stringBuilder = stringBuilder.append(" and ").append(attribute).append("<>").append(value);
return this;
}
@Override
public String toString() {
return this.sql += stringBuilder.toString();
}
}