数据库字段格式处理
来源:互联网 发布:上海软件中心 编辑:程序博客网 时间:2024/06/05 15:22
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.newcross.vsg.platform.dto.annotation.DtoField;
import com.newcross.vsg.platform.model.BaseModel;
import com.newcross.vsg.platform.model.DeviceGroup;
public class ReflectUtil {
@SuppressWarnings("unchecked")
public static void setFieldValue(Object target, String fname, Class ftype,
Object fvalue) {
if (target == null
|| fname == null
|| "".equals(fname)
|| (fvalue != null && !ftype
.isAssignableFrom(fvalue.getClass()))) {
return;
}
Class clazz = target.getClass();
try {
Method method = clazz.getDeclaredMethod(
"get" + Character.toUpperCase(fname.charAt(0))
+ fname.substring(1), ftype);
if (!Modifier.isPublic(method.getModifiers())) {
method.setAccessible(true);
}
method.invoke(target, fvalue);
} catch (Exception me) {
try {
Field field = clazz.getDeclaredField(fname);
if (!Modifier.isPublic(field.getModifiers())) {
field.setAccessible(true);
}
field.set(target, fvalue);
} catch (Exception fe) {
Logger.error(fe.getMessage() + fe.getMessage(), fe);
}
}
}
public static Field[] getAllFields(Class klass) {
List<Field> fields = new ArrayList<Field>();
fields.addAll(Arrays.asList(klass.getDeclaredFields()));
if (klass.getSuperclass() != null) {
fields.addAll(Arrays.asList(getAllFields(klass.getSuperclass())));
}
return fields.toArray(new Field[] {});
}
public static Map<String, Field> getAllFieldsToMap(Class clazz) {
Field[] fields = getAllFields(clazz);
Map<String, Field> result = new HashMap<String, Field>();
if (fields != null) {
for (Field field : fields) {
result.put(field.getName(), field);
}
}
return result;
}
public static List<Map> buildMapFromObject(Object obj,String fun_cde,String userId) {
List<Map> result = new ArrayList<Map>();
if(obj != null) {
Class clazz = obj.getClass();
Field[] fields = getAllFields(clazz);
try {
for(Field field : fields){
DtoField dtoFieldAnnt = field.getAnnotation(DtoField.class);
if (dtoFieldAnnt != null) {
Map<String, String> row = new HashMap<String, String>();
String key = dtoFieldAnnt.field();
field.setAccessible(true);
Object value = field.get(obj);
if(value==null){
value="";
}
if(value instanceof Date){
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
value = dateFormatter.format(value);
};
row.put("mod_cde","VMS");
row.put("fun_cde",fun_cde);
row.put("userId",userId);
row.put("key", key);
row.put("val", value.toString());
result.add(row);
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static Object buildObjectFromMap(List<Map> list, Class clazz) {
Object result = null;
Field[] fields = getAllFields(clazz);
try {
result = clazz.newInstance();
for(Field field : fields) {
DtoField dtoFieldAnnt = field.getAnnotation(DtoField.class);
Object obj = field.getType();
String key = dtoFieldAnnt.field();
Map map = getRelativeField(list, key);
if(map !=null){
Object value = map.get("PARAM_VAL");
if(obj.equals(Boolean.class)){
value = Boolean.parseBoolean(value.toString());
}
if(obj.equals(Date.class)){
//value = DateFormat.getDateInstance().parse(value.toString());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
value = format.parse(value.toString());
}
if(obj.equals(BigDecimal.class)){
BigDecimal bigDecimal = new BigDecimal(value.toString());
value = bigDecimal.setScale(0, BigDecimal.ROUND_HALF_UP);
}
if(obj.equals(Integer.class)){
value = Integer.parseInt(value.toString());
}
field.setAccessible(true);
field.set(result, value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws Exception {
DeviceGroup deviceGroup = new DeviceGroup();
deviceGroup.setVersionId(1L);
Field[] fields = getAllFields(DeviceGroup.class);
int size = fields.length;
for (int i = 0; i < size; i++) {
System.out.println(fields[i].getName());
}
Class clazz = BaseModel.class;
Field versionField = clazz.getDeclaredField("versionId");
// Field versionField = clazz.getField("versionId");
versionField.setAccessible(true);
Object versionId = versionField.get(deviceGroup);
System.out.println(versionId);
}
public static Map getRelativeField(List<Map> list, String fieldName) {
for(Map map : list) {
if(map.get("PARAM_KEY").toString().equalsIgnoreCase(fieldName)) {
return map;
}
}
return null;
}
}
- 数据库字段格式处理
- hibernate 处理数据库字段默认值
- 转换数据库时间字段格式函数
- mysql数据库字段格式统一转换方法
- 能够处理任何数据库字段的Panel
- 如何处理数据库字段是bigint型?
- 数据库字段空值处理的方法
- 能够处理任何数据库字段的Panel
- VC 处理数据库datetime字段的方法
- php读取xml 数据库字段超长处理
- 数据库字段中存在单引号的处理
- VC 处理数据库datetime字段的方法
- oracle数据库处理boolean型字段
- java 处理数据库clob类型的字段
- android GreenDao 数据库升级特殊字段处理
- 数据库与JavaBean字段名不一致处理
- 对于mysql数据库中日期 格式字段的存取
- sql 读取数据库中的datetime字段,输出的各种格式
- 分段下载ASIHTTP
- ASP.NET MVC表单处理
- jquery radio,checkbox,select,radio
- MYSQL 数据类型
- malloc 碎片整理吐核故障初步解决
- 数据库字段格式处理
- GEF firePropertyChange()响应注意地方
- MYSQL 常用命令
- GNU C和C99标准中的可变参数宏(variadic macros)
- shell 程序设计入门 比较两个数的大小
- javascript笔记:通过对作用域链和执行环境的深入理解所得出的提高javascript代码性能的建议
- 第4条:通过私有构造器强化不可实例化的能力
- Oracle与MySQL的几点区别
- SQL适配器的执行过程