package com.utils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set; public class CommonUtils {
/**
* 将map中的值赋值到po中
*
* @param map
* 前台传过来的值封装成的map对象
* @param obj
* po实体类
*/
public static void getLatestObjectByMap(Map<String, ?> map, Object obj) {
Set<String> keys = map.keySet();
for (String key : keys) {
Object value = map.get(key);
Class<? extends Object> clazz = obj.getClass();
try {
Field field = clazz.getDeclaredField(key);
field.setAccessible(true);
field.set(obj, getValue(key, field.getType().getName(), value));
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
} /**
* 将fieldValue转为dataType类型的值
*
* @param key
* @param dataType
* @param fieldValue
* @return
*/
private static Object getValue(String key, String dataType,
Object fieldValue) {
String errorMessage = key + "为[" + dataType + "]而设置的值为["
+ fieldValue.getClass().getName() + "]";
String numberFormatErrorMessage = key + " [" + fieldValue + "]转换为["
+ dataType + "]出错";
System.out.println(dataType);
if ("java.sql.Date".equals(dataType)) {
if (fieldValue instanceof java.sql.Date) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
String formatString = "yyyy-MM-dd";
try {
SimpleDateFormat sdf = new SimpleDateFormat(formatString);
Date date = sdf.parse((String) fieldValue);
return new java.sql.Date(date.getTime());
} catch (Exception e) {
throw new RuntimeException(key + "[" + fieldValue
+ "]格式错误,正确格式为[" + formatString + "]");
}
} else {
throw new RuntimeException(errorMessage);
}
} else if ("java.util.Date".equals(dataType)) {
if (fieldValue instanceof java.util.Date) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
String formatString = "yyyy-MM-dd";
try {
SimpleDateFormat sdf = new SimpleDateFormat(formatString);
java.util.Date date = sdf.parse((String) fieldValue);
return date;
} catch (Exception e) {
throw new RuntimeException(key + "[" + fieldValue
+ "]格式错误,正确格式为[" + formatString + "]");
}
} else {
throw new RuntimeException(errorMessage);
}
} else if ("java.sql.Timestamp".equals(dataType)) {
if (fieldValue instanceof java.sql.Timestamp) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
String formatString = "yyyy-MM-dd HH:mm:ss";
try {
SimpleDateFormat sdf = new SimpleDateFormat(formatString);
Date date = sdf.parse((String) fieldValue);
return new Timestamp(date.getTime());
} catch (Exception e) {
throw new RuntimeException(key + "[" + fieldValue
+ "]格式错误,正确格式为[" + formatString + "]");
}
} else {
throw new RuntimeException(errorMessage);
}
} else if ("java.lang.String".equals(dataType)) {
if (fieldValue instanceof java.lang.String) {
return fieldValue;
} else {
throw new RuntimeException(errorMessage);
}
} else if ("java.math.BigDecimal".equals(dataType)) {
if (fieldValue instanceof java.math.BigDecimal) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
try {
return new BigDecimal(String.valueOf(fieldValue));
} catch (Exception e) {
throw new RuntimeException(numberFormatErrorMessage);
}
} else {
throw new RuntimeException(errorMessage);
}
} else if (("java.lang.Integer".equals(dataType))
|| ("int".equals(dataType))) {
if (fieldValue instanceof java.lang.Integer) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
try {
return new Integer(String.valueOf(fieldValue));
} catch (Exception e) {
throw new RuntimeException(numberFormatErrorMessage);
}
} else {
throw new RuntimeException(errorMessage);
}
} else if (("java.lang.Long".equals(dataType))
|| ("long".equals(dataType))) {
if (fieldValue instanceof java.lang.Long) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
try {
return new Long(String.valueOf(fieldValue));
} catch (Exception e) {
throw new RuntimeException(numberFormatErrorMessage);
}
} else {
throw new RuntimeException(errorMessage);
} } else if (("java.lang.Short".equals(dataType))
|| ("short".equals(dataType))) {
if (fieldValue instanceof java.lang.Short) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
try {
return new Short(String.valueOf(fieldValue));
} catch (Exception e) {
throw new RuntimeException(numberFormatErrorMessage);
}
} else {
throw new RuntimeException(errorMessage);
}
} else if (("java.lang.Byte".equals(dataType))
|| ("byte".equals(dataType))) {
if (fieldValue instanceof java.lang.Byte) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
try {
return new Byte(String.valueOf(fieldValue));
} catch (Exception e) {
throw new RuntimeException(numberFormatErrorMessage);
}
} else {
throw new RuntimeException(errorMessage);
}
} else if (("java.lang.Float".equals(dataType))
|| ("float".equals(dataType))) {
if (fieldValue instanceof java.lang.Float) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
try {
return new Float(String.valueOf(fieldValue));
} catch (Exception e) {
throw new RuntimeException(numberFormatErrorMessage);
}
} else {
throw new RuntimeException(errorMessage);
}
} else if (("java.lang.Double".equals(dataType))
|| ("double".equals(dataType))) {
if (fieldValue instanceof java.lang.Double) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
try {
return new Double(String.valueOf(fieldValue));
} catch (Exception e) {
throw new RuntimeException(numberFormatErrorMessage);
}
} else {
throw new RuntimeException(errorMessage);
}
} else if (("java.lang.Boolean".equals(dataType))
|| ("boolean".equals(dataType))) {
if (fieldValue instanceof java.lang.Boolean) {
return fieldValue;
} else if (fieldValue instanceof java.lang.String) {
if ("true".equals(fieldValue)) {
return new Boolean(true);
} else if ("false".equals(fieldValue)) {
return new Boolean(false);
} else {
throw new RuntimeException(numberFormatErrorMessage);
}
} else {
throw new RuntimeException(errorMessage);
}
} else if (("java.lang.Character".equals(dataType))
|| ("char".equals(dataType))) {
{
return new Character(String.valueOf(fieldValue).charAt(0));
}
}
return fieldValue;
}
}

CommonUtils的更多相关文章

  1. cas 单点登录出现org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接 Connection refused

    cas 单点登录出现org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接 Connection refused 环境: ...

  2. CommonUtils.java

    package com.vcredit.framework.utils; import java.lang.reflect.InvocationTargetException;import java. ...

  3. 网上图书商城项目学习笔记-036工具类之CommonUtils及日期转换器

    1.CommonUtils.java package cn.itcast.commons; import java.util.Map; import java.util.UUID; import or ...

  4. 浅谈Java工具类CommonUtils的使用

    package com.xushouwei.cn; import java.util.HashMap; import java.util.Map; import org.junit.Test; imp ...

  5. 工具类封装之--CommonUtils

    /** * @file_name : CommonUtils.java * @author : * @date : 2018年3月15日 * Description: */ package cn.xx ...

  6. JavaWeb网上图书商城完整项目-CommonUtils(1生成uuid,2Map转换成JavaBean)

    java工程中添加上面的jar包 CommonUtils类就两个方法: l  String uuid():生成长度32的随机字符,通常用来做实体类的ID.底层使用了UUID类完成: l  T toBe ...

  7. Java多线程基础——对象及变量并发访问

    在开发多线程程序时,如果每个多线程处理的事情都不一样,每个线程都互不相关,这样开发的过程就非常轻松.但是很多时候,多线程程序是需要同时访问同一个对象,或者变量的.这样,一个对象同时被多个线程访问,会出 ...

  8. [Java Collection]List分组之简单应用.

    前言 今天有一个新需求, 是对一个List进行分组, 于是便百度到一些可用的代码以及我们项目使用的一些tools, 在这里总结下方便以后查阅. 一: 需求 现在我们一个数据库表t_series_val ...

  9. Restful 介绍及SpringMVC+restful 实例讲解

    restful不是一个框架,称为一种编码更烦更贴切吧,其核心类位于spring-web.jar中,即RestTemplate.class restful是rpc通过http协议的一种实现方式,和web ...

随机推荐

  1. Lua与C++相互调用

    {--1.环境--} 为了快速入手,使用了小巧快速的vc++6.0编译器 以及在官网下载了Lua安装包..官网地址{--http://10.21.210.18/seeyon/index.jsp--} ...

  2. 论文笔记之:Speed Up Tracking by Ignoring Features

    Speed Up Tracking by Ignoring Features CVPR 2014 Abstract:本文提出一种特征选择的算法,来实现用最"精简"的特征以进行目标跟 ...

  3. 一步步构建自己的AngularJS(2)——scope之$watch及$digest

    在上一节项目初始化中,我们最终得到了一个可以运行的基础代码库,它的基本结构如下: 其中node_modules文件夹存放项目中的第三方依赖模块,src存放我们的项目代码源文件,test存放测试用例文件 ...

  4. CompositeConfiguration的用法

    public class Mytest { private static ApplicationContext applicationContext; public static void main( ...

  5. 【freemaker】之判断是否为空,表达式的使用

    测试代码 @Test public void test05(){ try { freemakerUtil.fprint(root, "05.ftl",fn+"05.htm ...

  6. HTML标签_01

    <!DOCTYPE html> <html> <body> <h1>我的第一个标题</h1> <p>我的第一个段落.</p ...

  7. FileUploadInterceptor拦截器的笔记

    当请求表单中包含一个文件file,FileUploadInterception拦截器会自动应用于这个文件. 表单: <s:form namespace="/xxx" acti ...

  8. mycat 插入语句导致的一个Dobbo问题

    2017-01-03 11:11:52.621 [com.alib] (): [DUBBO] Send heartbeat to remote channel /121.43.177.8:20192, ...

  9. ZYB's Premutation POJ5592

    Problem Description ZYBZYBZYB has a premutation PPP,but he only remeber the reverse log of each pref ...

  10. uboot和内核波特率不同

    uboot和内核波特率不同,在uboot启动后,修改uboot参数: set bootargs 'noinitrd root=/dev/mtdblock3 init=/linuxrc console= ...