天天看點

注解:java 自定義注解應用執行個體

1.定義 Table 注解

package test;  

import java.lang.annotation.Documented;  

import java.lang.annotation.ElementType;  

import java.lang.annotation.Inherited;  

import java.lang.annotation.Retention;  

import java.lang.annotation.RetentionPolicy;  

import java.lang.annotation.Target;  

@Inherited  

@Target({ElementType.TYPE})  

@Retention(RetentionPolicy.RUNTIME)  

@Documented  

public @interface Table {  

    String value() default "";  

}  

2.定義 Column 注解

@Target({ElementType.FIELD})  

public @interface Column {  

3.定義使用注解的實體

@Table("tb_test")  

public class TestDto {  

    @Deprecated  

    private String tt;  

    @Column("_id")  

    private String id;  

    @Column("username")  

    private String name;  

    public TestDto(String id, String name) {  

        super();  

        this.id = id;  

        this.name = name;  

    }  

    public String getId() {  

        return id;  

    public void setId(String id) {  

    public String getName() {  

        return name;  

    public void setName(String name) {  

4.測試注解

import java.lang.reflect.Field;  

import java.lang.reflect.Method;  

public class Test {  

    public static void main(String[] args) {  

        TestDto testDto = new TestDto("123", "34");  

        TestDto testDto1 = new TestDto("123", "test1");  

        TestDto testDto2 = new TestDto("", "test1,test2,test3,test4");  

        String sql = assembleSqlFromObj(testDto);  

        String sql1 = assembleSqlFromObj(testDto1);  

        String sql2 = assembleSqlFromObj(testDto2);  

        System.out.println(sql);  

        System.out.println(sql1);  

        System.out.println(sql2);  

    /** 

     * 通過注解來組裝查詢條件,生成查詢語句 

     *  

     * @param obj 

     * @return 

     */  

    public static String assembleSqlFromObj(Object obj) {  

        Table table = obj.getClass().getAnnotation(Table.class);  

        StringBuffer sbSql = new StringBuffer();  

        String tableName = table.value();  

        sbSql.append("select * from " + tableName + " where 1=1 ");  

        Field[] fileds = obj.getClass().getDeclaredFields();  

        for (Field f : fileds) {  

            String fieldName = f.getName();  

            String methodName = "get" + fieldName.substring(0, 1).toUpperCase()  

                    + fieldName.substring(1);  

            try {  

                Column column = f.getAnnotation(Column.class);  

                if (column != null) {  

                    Method method = obj.getClass().getMethod(methodName);  

                    String value = (String) method.invoke(obj);  

                    if (value != null && !value.equals("")) {  

                        if (!isNum(column.value()) && !isNum(value)) {  

                            // 判斷參數是不是 in 類型參數 1,2,3  

                            if (value.contains(",")) {  

                                sbSql.append(" and " + column.value() + " in (" + value + ") ");  

                            } else {  

                                sbSql.append(" and " + column.value() + " like '%" + value + "%' ");  

                            }  

                        } else {  

                            sbSql.append(" and " + column.value() + "=" + value + " ");  

                        }  

                    }  

                }  

            } catch (Exception e) {  

                e.printStackTrace();  

            }  

        }  

        return sbSql.toString();  

     * 檢查給定的值是不是 id 類型 1.檢查字段名稱 2.檢查字段值 

     * @param target 

    public static boolean isNum(String target) {  

        boolean isNum = false;  

        if (target.toLowerCase().contains("id")) {  

            isNum = true;  

        if (target.matches("\\d+")) {  

        return isNum;  

測試結果:

select * from tb_test where 1=1  and _id=123  and username=34 

select * from tb_test where 1=1  and _id=123  and username like '%test1%' 

select * from tb_test where 1=1  and username in (test1,test2,test3,test4)