美文网首页
java 自定义注解项目实战(慕课网)

java 自定义注解项目实战(慕课网)

作者: 百炼 | 来源:发表于2019-06-11 09:55 被阅读0次

date[2019-6-11]

Table

package edu.anno.sql;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Table {
    String value();
}

Column

package edu.anno.sql;

import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Column {
    String value();
}

Employee

package edu.anno.sql;

@Table("EMPLOYEE")
public class Employee {
    @Column("id")
    private int id;

    @Column("user_name")
    private String userName;

    @Column("address")
    private String address;

    @Column("email")
    private String email;

    @Column("salary")
    private double salary;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

AnnotionParseTest

package edu.anno.sql;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AnnotionParseTest {
    public static void main(String[] args) {
        Employee employee1 = new Employee();
        employee1.setId(0);

        Employee employee2 = new Employee();
        employee2.setAddress("henan");

        Employee employee3 = new Employee();
        employee3.setSalary(10000.0);

        Employee employee4 = new Employee();
        employee4.setAddress("henan");
        employee4.setSalary(10.0);
        employee4.setEmail("admin@qq.com,admin@163.com");

        System.out.println(query(employee1));
        System.out.println(query(employee2));
        System.out.println(query(employee3));
        System.out.println(query(employee4));

    }

    public static String query(Employee e) {
        StringBuilder stringBuilder = new StringBuilder();

        Class<? extends Employee> eClazz = e.getClass();
        boolean annotationPresent = eClazz.isAnnotationPresent(Table.class);
        if (!annotationPresent) {
            return null;
        }

        Table annotation = eClazz.getAnnotation(Table.class);
        stringBuilder.append("SELECT * FROM ").append(annotation.value()).append(" WHERE 1 = 1 ");

        final Field[] declaredFields = eClazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            boolean fExits = declaredField.isAnnotationPresent(Column.class);
            if (!fExits) {
                continue;
            }
            Column column = declaredField.getAnnotation(Column.class);
            String columnValue = column.value();

            String name = declaredField.getName();
            String getMethodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
            Object filedValue = null;
            Method getMethod;
            try {
                getMethod = eClazz.getMethod(getMethodName);
                filedValue = getMethod.invoke(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }

            if (filedValue == null || (filedValue instanceof Integer && (Integer) filedValue == 0)) {
                continue;
            }
            stringBuilder.append(" AND ").append(columnValue);
            if (filedValue instanceof String) {
                if (((String) filedValue).contains(",")) {
                    String[] tokens = ((String) filedValue).split(",");
                    stringBuilder.append(" IN (");
                    for (String token : tokens) {
                        stringBuilder.append(token).append(",");
                    }
                    stringBuilder.replace(stringBuilder.lastIndexOf(","), stringBuilder.length(), "");
                    stringBuilder.append(")");
                } else {
                    stringBuilder.append(" = '").append(filedValue).append("'");
                }
            } else {
                stringBuilder.append("=").append(filedValue);
            }
        }
        return stringBuilder.toString();
    }
}

运行结果

SELECT * FROM EMPLOYEE WHERE 1 = 1  AND salary=0.0
SELECT * FROM EMPLOYEE WHERE 1 = 1  AND address = 'henan' AND salary=0.0
SELECT * FROM EMPLOYEE WHERE 1 = 1  AND salary=10000.0
SELECT * FROM EMPLOYEE WHERE 1 = 1  AND address = 'henan' AND email IN (admin@qq.com,admin@163.com) AND salary=10.0

相关文章

网友评论

      本文标题:java 自定义注解项目实战(慕课网)

      本文链接:https://www.haomeiwen.com/subject/qdlffctx.html