JAVA註解拼接SQL,並對字段進行排序

package com.example.annotation;java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;sql

/**
 * 項目名稱:
 * 類描述:
 * 建立人:
 * 建立時間:2015/12/15 19:20
 * 修改人:
 * 修改時間:2015/12/15 19:20
 * 修改備註:
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataBaseTable {
    public String tableName();
}ide

 

package com.example.annotation;.net

/**
 * 項目名稱:
 * 類描述:
 * 建立人:
 * 建立時間:2015/12/15 19:23
 * 修改人:
 * 修改時間:2015/12/15 19:23
 * 修改備註:
 */
@DataBaseTable(tableName = "CustomTable")
public class CustomModel {
    @ColumnsName(fieldName = "userId",number = 1)
    public String mImUserId;排序

    @ColumnsName(fieldName = "UserCustomList",number = 3)
    public byte[] mUserCustomList;get

    @ColumnsName(fieldName = "datatype",number = 2)
    public int mType;
}
 io

package com.example.annotation;table

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;class

/**
 * 項目名稱:
 * 類描述:
 * 建立人:
 * 建立時間:2015/12/15 19:21
 * 修改人:
 * 修改時間:2015/12/15 19:21
 * 修改備註:
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ColumnsName {
    String fieldName() default "";
    int number() default 0;
}import

 

package com.example.annotation;

import java.util.Comparator;

/**
 * 項目名稱:MyApplication
 * 類描述:
 * 建立人:
 * 建立時間:2016/3/7 9:59
 * 修改人:
 * 修改時間:2016/3/7 9:59
 * 修改備註:
 */
public class ColumnsComparator implements Comparator<ColumnsName>{

    @Override
    public int compare(ColumnsName c1, ColumnsName c2) {
        if(c1.number() > c2.number()){
            return 1;
        }
        if(c1.number() <= c2.number()){
            return -1;
        }
        return 0;
    }
    
}

package com.example.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 項目名稱:
 * 類描述:
 * 建立人:
 * 建立時間:2015/12/15 19:26
 * 修改人:
 * 修改時間:2015/12/15 19:26
 * 修改備註:
 */
public class AnnotationTest {

    /**
     * 運行註解,拼出sql
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Field[] fields = CustomModel.class.getFields();
        DataBaseTable tableModel = (DataBaseTable) CustomModel.class
                .getAnnotation(DataBaseTable.class);
        String tableName = tableModel.tableName();
        List<ColumnsName> list = new ArrayList<ColumnsName>(fields.length);
        String sql = "CREATE TABLE IF NOT EXISTS " + tableName + "(";
        for (int i = 0; i < fields.length; i++) {
            ColumnsName tabFeild = fields[i].getAnnotation(ColumnsName.class);
            list.add(tabFeild);
            if (tabFeild != null) {
                if (i == 0) {
                    sql = sql + tabFeild.fieldName() + " "
                            + getColumnType(fields[i].getType());
                } else {
                    sql = sql + " ," + tabFeild.fieldName() + " "
                            + getColumnType(fields[i].getType());
                }
            }
        }
        sql = sql + ");";
        System.out.println(sql);
        //對反射進行排序
        Collections.sort(list, new ColumnsComparator());
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).number() + "\t" + list.get(i).fieldName() );
        }
    }

    /**
     * 獲得type
     * 
     * @param type
     * @return
     */
    public static String getColumnType(Type type) {
        String colums = "TEXT";
        if (type == Long.class || (type == Long.TYPE)) {

        } else if (Integer.class == type || (type == Integer.TYPE)) {
            colums = "INTEGER";
        } else if (type == String.class) {

        } else if (type == byte[].class) {
            colums = "BLOB";
        }

        return colums;
    }


    
}
 

 

歡迎你們提出修改意見

相關文章
相關標籤/搜索