获取一个实体的所有字段,不带类名

待我称王封你为后i 2022-03-07 08:18 356阅读 0赞

查询数据库时需要查所有字段,又不想用select * form 一个一个取字段难免太麻烦,而且该了表结构就得动sql

  1. public static String getAllAttrsNoClassName(@SuppressWarnings("rawtypes") Class c) {
  2. StringBuilder s = new StringBuilder();
  3. Field[] fs = c.getDeclaredFields();
  4. for (int i = 0; i < fs.length; i++) {
  5. Field f = fs[i];
  6. Annotation annotation = f.getAnnotation(TableField.class);
  7. if (annotation == null) {// 非表字段
  8. continue;
  9. }
  10. f.setAccessible(true); // 设置些属性是可以访问的
  11. s.append(",");
  12. s.append(f.getName());
  13. }
  14. return s.toString().substring(1, s.length());
  15. }

发表评论

表情:
评论列表 (有 0 条评论,356人围观)

还没有评论,来说两句吧...

相关阅读