com.tmsps.ne4spring.orm.model.DataModel Maven / Gradle / Ivy
package com.tmsps.ne4spring.orm.model;
import java.io.Serializable;
import java.lang.reflect.Field;
import com.alibaba.fastjson.JSON;
import com.tmsps.ne4spring.orm.ClassUtil;
/**
*
* @author zhangwei [email protected]
*
*/
@SuppressWarnings("serial")
public class DataModel implements Serializable {
public String toJsonString() {
return JSON.toJSONString(this);
}
public Object getPK() {
// 获取对象中ID属性字段
Field idField = ClassUtil.getIdField(this.getClass());
// 获取ID的值
Object idVal = ClassUtil.getClassVal(idField, this);
return idVal;
}
/**
* @Description: 根据字段名称获取字段的值.
* getDeclaredFiled 仅能获取类本身的属性成员(包括私有、共有、保护).
* getField 仅能获取类(及其父类) public属性成员.
* @author: zhangwei(Administrator)[email protected]
* @date: 2022/08/22
*/
public Object getFieldValByName(String fieldName) {
try {
Field field = this.getClass().getDeclaredField(fieldName);
Object fieldVal = ClassUtil.getClassVal(field, this);
return fieldVal;
} catch (NoSuchFieldException | SecurityException e) {
e.printStackTrace();
return null;
}
}
public String getTableName() {
return ClassUtil.getTableName(getClass());
}
}