【JAVA】实现注解拼接SQL+字段进行排序
2017-06-26 09:42
447 查看
代码如下:
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) @Documented public @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; } }
相关文章推荐
- JAVA_WEB项目之Lucene实现检索结果排序和关键字在索引库中多字段查询结果进行高亮显示
- java中list里面存放map,根据map中的某一个字段进行排序 lambda
- Java按照List内存储的对象的某个字段进行排序的实例
- Java 中 Map 根据键值 (key) 或者值 (value) 进行排序实现
- java 实现List对象中按某种方式进行排序
- 解决Mysql数据库实现多表联合查询及按照其中一个字段进行排序问题
- 【java】实体类中 按照特定的字段 进行升序/降序 排序
- [Java]实现冒泡算法,对数组元素进行排序
- java list按照元素对象的指定多个字段属性进行排序
- 利用Java程序将字符串进行排序与拼接
- java当中对对象当中某表字段排序实现
- 对公司员工年龄进行排序,java实现
- java list按照元素对象的指定多个字段属性进行排序
- java中list里面存放map,根据map中的某一个字段进行排序
- java list按照 对象 指定多个字段属性进行排序
- Java中Map根据键值(key)或者值(value)进行排序实现
- java 使用面向对象方式实现录入学生信息,取出成绩最大值、最小值、平均值、对其进行排序
- java list按照元素对象的指定多个字段属性进行排序
- Java——对Java的Map的Value字段进行排序
- 【Java学习笔记】实现Comparator接口来进行字符串逆向排序