【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; } }
阅读全文
0 0
- 【JAVA】实现注解拼接SQL+字段进行排序
- 使用Java注解实现拼接sql语句的功能
- java注解:完成sql语句的拼接
- SQL中使用FOR XML PATH对字段进行拼接
- SQL字段拼接显示
- JAVA中基于json串进行sql语句拼接实现select方法
- sql 拼接 格式化日期 拼接原来字段
- 字段排序 拼接key value
- sql 字段值的拼接
- sql如何拼接字段值
- sql中的字段、字符串拼接
- 编写注解处理器拼接SQL
- 利用Java程序将字符串进行排序与拼接
- SQL 按多个字段排序的实现
- mysql 实现拼接表中的字段字段
- SQL字符型字段按数字型字段排序实现方法
- SQL字符型字段按数字型字段排序实现方法
- SQL字符型字段按数字型字段排序实现方法
- Web前端知识体系精简
- 几种常用的设计模式介绍
- mybatis 自增主键配置
- 23种设计模式-访问者模式
- java-File
- 【JAVA】实现注解拼接SQL+字段进行排序
- 【数据结构基础】校园网布线问题
- struts2学习之---输入校验
- c++11 & c++ <valarray>
- [bzoj1934]: [ZJOI2009]狼和羊的故事
- 教你 Debug 的正确姿势——记一次 CoreMotion 的 Crash
- 23种设计模式-命令模式
- PHP入门之局域网多站点访问、带参数请求、Json数据、Xml数据
- Ten Simple Rules for Effective Statistical Practice