获取映射注解,查询数据库字段的信息

//通过循环 可以获得 注解中的name值@Column(name="EO_XXX"),本方法获取的是 get方法的注解

Method[] methods= EoOrderMaterielModel.class.getMethods();
  Map<String,String> modelColumns=new HashMap<String, String>();
  for(Method m:methods){
   Annotation ats[]=m.getAnnotations();
   if(ats!=null&&ats.length>0){
           if(ats[0] instanceof Column){
            Column colInt = (Column) ats[0];
            String columnName = colInt.name();
            String value=lowerKey(m.toGenericString().substring(m.toGenericString().lastIndexOf(".")+4, m.toGenericString().length()-2));
            modelColumns.put(columnName,value);
           }
          }
  }

//获取private int id的注解

Field field = EoOrderMaterielModel.class.getDeclaredField(modelColumns.get(validators[j]));
     field.setAccessible(true);

Annotation ats[]=field.getAnnotations();
   if(ats!=null&&ats.length>0){
           if(ats[0] instanceof Column){
            Column colInt = (Column) ats[0];
            String columnName = colInt.name();
            }

    }

//实体类 实例

@Table(name = "T_EO_ORDER")

public class EoOrderMaterielModel{

    @Column(name = "EO_ID")

    private int id;

    @Column(name = "EO_ID")

    public Long getId() {
      return id;
     }

     public void setId(Long id) {
      this.id= id;
     }

 

//获取表名 @Table(name="T_EO_ORDER")
  Table table = EoOrderMaterielModel.class.getAnnotation(Table.class);  

 

// 通过字段值和表名 可以查询其备注信息(数据库中的注释信息)

String sqlColumn=" ";
  for(String name:columnName){
   sqlColumn+=" ‘ "+name+" ‘, ";
  }
  return "SELECT  a1.column_name AS name," +
     "CASE"+
     " WHEN a2.comments IS NULL THEN ‘未知‘ "+
     " ELSE a2.comments "+
     " END  AS comments "+
     " FROM    USER_TAB_COLUMNS         a1 "+
     " LEFT JOIN USER_COL_COMMENTS      a2 "+
     " ON  a1.table_name = a2.table_name "+
     " AND a1.column_name = a2.column_name "+
     " WHERE   a1.table_name = upper(‘ "+
     tableName+
     " ‘ ) and a1.column_name in("+
     sqlColumn.substring(0, sqlColumn.length()-1)+
     ")";

获取映射注解,查询数据库字段的信息,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。