【JAVA】实现注解拼接SQL+字段进行排序

来源:互联网 发布:天津绫致时装淘宝真假 编辑:程序博客网 时间:2024/05/17 02:32

代码如下:

package com.example.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface DataBaseTable {    public String tableName();} package com.example.annotation;@DataBaseTable(tableName = "CustomTable")public class CustomModel {    @ColumnsName(fieldName = "userId",number = 1)    public String mImUserId;    @ColumnsName(fieldName = "UserCustomList",number = 3)    public byte[] mUserCustomList;    @ColumnsName(fieldName = "datatype",number = 2)    public int mType;} package com.example.annotation;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;@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ColumnsName {    String fieldName() default "";    int number() default 0;} package com.example.annotation;import java.util.Comparator;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;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;    }    }


原创粉丝点击