public static String getHql() throws Exception {
  // 获得 Person类上 run方法上的注解
  // 反射获得run方法
  Class personClass = Person.class;

  Field[] fieldlist = personClass.getDeclaredFields();
  StringBuffer hqlSel = new StringBuffer("select ");
  StringBuffer hqlFrom = new StringBuffer(" from ");
  String name, asName;
  String[] nameSpilt;
  Set<String> fromVals = new HashSet();
  for (Field f : fieldlist) {
   MyAnnotation1 myAnnotation1 = f.getAnnotation(MyAnnotation1.class);
   if (myAnnotation1 != null) {
    asName = " as " + f.getName();
    name = myAnnotation1.value();
    nameSpilt = name.split("\\.");
    if (nameSpilt.length == 2) {
     fromVals.add(nameSpilt[0]);
    }
    hqlSel.append(name).append(asName).append(",");
   }
  }
  hqlSel.delete(hqlSel.length() - 1, hqlSel.length());

  for (String fromVal : fromVals) {
   hqlFrom.append(fromVal).append(",");
  }
  hqlFrom.delete(hqlFrom.length() - 1, hqlFrom.length());
  hqlSel.append(hqlFrom);
  System.out.println(hqlSel);
  return hqlSel.toString();
 }

posted on 2017-08-06 15:27  Glimis  阅读(130)  评论(0编辑  收藏  举报