demo1

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;

/**
 * TODO  通过 Lambda 获取对象属性名
 * 使用方法: LambdaTypeInfo.convertToFieldName(Test::getName)
 *
 * @author ws
 * @mail [email protected]
 * @date 2020/4/12 0012 14:31
 */
public class LambdaTypeInfo {

    private static Map<Class, SerializedLambda> CLASS_LAMBDA_CACHE = new ConcurrentHashMap<>();

    /***
     * 转换方法引用为属性名
     * @param fn
     * @return
     */
    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);
        }
        // 截取get/is之后的字符串并转换首字母为小写
        return toLowerCaseFirstOne(methodName.replace(prefix, ""));
    }

    /**
     * 首字母转小写
     *
     * @param s
     * @return
     */
    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 {
                // 提取SerializedLambda并缓存
                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();
    }
}


你可能感兴趣的:(java,servlet,jvm)