BossSettlementAccount bossAccount = new BossSettlementAccount();
bossAccount = AddAccountID.addId(bossAccount, bossAccount.getClass(), "account", ProjectConfig.INTEGER);
/**
* @Package: pterosaur.account.util
* @Description: 因为数据库唯一id,是由系统创建,所以,统一处理存入id
* @author: liuxin
* @date: 17/4/22 上午11:50
*/
public class AddAccountID {
private static final Logger logger = LoggerFactory.getLogger(AddAccountID.class);
private static final String INTEGER = "integer";
private static final String STRING = "string";
public static <T> T addId(Object target, Class<T> type, String idName, String fieldType) {
T t = type.cast(target);
Object uid = caseType(fieldType);
try {
Field field = t.getClass().getDeclaredField(idName);
field.setAccessible(true);
field.set(t, uid);
} catch (NoSuchFieldException nfe) {
logger.error(nfe.getMessage());
} catch (IllegalAccessException ill) {
logger.error(ill.getMessage());
} catch (Exception e) {
logger.error(e.getMessage());
}
return t;
}
/**
* 根据类型,转换id类型
*
* @param fieldType
* @return
*/
public static Object caseType(String fieldType) {
Object uid = null;
switch (fieldType) {
case INTEGER:
uid = Integer.parseInt((UuidUtil.createId()));
break;
case STRING:
uid = UuidUtil.createId();
break;
}
return uid;
}
/**
* @Package: com.blm.data
* @Description: 数据转换工具
* 将map里面的对象转换为制定对象
* @author: liuxin
* @date: 17/5/15 下午4:44
*/
public static <T> T mapToObject(Map<String, Object> map, Class<T> cls) throws Exception {
Object target=cls.newInstance();
T t = cls.cast(target);
Set<String> keySet = map.keySet();
Iterator<String> keys = keySet.iterator();
Field[] fields = t.getClass().getDeclaredFields();
ArrayList<String> fieldNames = new ArrayList<String>();
for (Field field : fields) {
String fileName = field.getName();
fieldNames.add(fileName);
}
while (keys.hasNext()) {
String fieldName = keys.next();
if (fieldNames.contains(fieldName)) {
Object value = map.get(fieldName);
Field field = t.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(t, value);
}
}
return t;
}
}
网友评论