获取实体类的TableName和TableField @param object @param key1 @return
(Object object, Set<String> key1)
| 193 | * @return |
| 194 | */ |
| 195 | public static HashMap<String, Object> getProxyPojoValue(Object object, Set<String> key1) { |
| 196 | String id = null; |
| 197 | // 返回参数 |
| 198 | HashMap<String, Object> hashMap = new HashMap<>(16); |
| 199 | for (String s : key1) { |
| 200 | List<Field> fieldList = new ArrayList<>(); |
| 201 | Class tempClass = object.getClass(); |
| 202 | while (tempClass != null) { |
| 203 | //当父类为null的时候说明到达了最上层的父类(Object类). |
| 204 | fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields())); |
| 205 | //得到父类,然后赋给自己 |
| 206 | tempClass = tempClass.getSuperclass(); |
| 207 | } |
| 208 | for (Field field : fieldList) { |
| 209 | field.setAccessible(true); |
| 210 | |
| 211 | // 获取表名 |
| 212 | TableName table = object.getClass().getAnnotation(TableName.class); |
| 213 | if (table != null) { |
| 214 | String tableName = table.value(); |
| 215 | hashMap.putIfAbsent("tableName", tableName); |
| 216 | } |
| 217 | // 获取主键id |
| 218 | if (id == null) { |
| 219 | boolean isIdField = field.isAnnotationPresent(TableId.class); |
| 220 | if (isIdField) { |
| 221 | TableField tableField = field.getAnnotation(TableField.class); |
| 222 | if (s.toLowerCase().equals(field.getName().toLowerCase())) { |
| 223 | String tableId = tableField.value(); |
| 224 | hashMap.put(s, tableId); |
| 225 | id = tableId; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // 获取字段的值 |
| 231 | boolean isTableField = field.isAnnotationPresent(TableField.class); |
| 232 | if (isTableField) { |
| 233 | TableField tableField = field.getAnnotation(TableField.class); |
| 234 | if (s.toLowerCase().equals(field.getName().toLowerCase())) { |
| 235 | String fieldValue = tableField.value(); |
| 236 | hashMap.put(s, fieldValue); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | System.out.println(hashMap); |
| 242 | return hashMap; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected