Map into a bean in Java

Source: Internet
Author: User

The Map collection class is used to store element pairs (called "Keys" and "values"), where each key is mapped to a value that is often used in Java programming. But when we are dealing with business logic or manipulating databases, we often apply our own defined bean or VO to pass and store data, which requires us to apply a public method to convert the data stored in the map to the corresponding Bean or VO, mainly in the Java reflection technology.

The following is the tool class that I used in the project, which contains the data types that we commonly use, the code is as follows:

Package test;

Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import Java.math.BigDecimal;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.Map;
//map conversion to Bean
public class Maptovoutil {
public static SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
public static <T> Object Refelctbean (map map, T t) {
Object obj = null;
Class clazz = T.getclass ();
try {
obj = Clazz.newinstance ();
Method[] methods = Clazz.getdeclaredmethods ();
for (Method method:methods) {
if (Method.getname (). StartsWith ("set")) {
String key = Method.getname (). Replace ("Set", "");
Key = key.substring (0, 1). toLowerCase (). Concat (key.substring (1));
Object value = Map.get (key);
if (Value==null | | value.equals ("n/A")) continue;
class<?>[] Paramtype = Method.getparametertypes ();
//Perform the corresponding set method according to the parameter type to assign VO to the value
if (paramtype[0] = = String.class) {
Method.invoke (obj, string.valueof (value));
Continue
}else if (paramtype[0] = = Bigdecimal.class) {
Method.invoke (obj, New BigDecimal (Value.tostring ()));
Continue
}else if (paramtype[0] = = Double.class) {
Method.invoke (obj, double.parsedouble (value.tostring ()));
Continue
}else if (paramtype[0] = = Date.class) {
Method.invoke (obj, Stringtodate (value.tostring ()));
Continue
}else if (paramtype[0] = = Int.class | | paramtype[0] = = Integer.class) {
Method.invoke (obj, integer.valueof (value.tostring ()));
Continue
}else if (paramtype[0] = = Boolean.class) {
Method.invoke (obj, Boolean.parseboolean (value.tostring ()));
Continue
}else if (paramtype[0] = = Char.class | | paramtype[0] = = Character.class) {
Method.invoke (obj, value.tostring (). CharAt (0));
Continue
}
}
}
} catch (Instantiationexception e) {
E.printstacktrace ();
} catch (Illegalaccessexception e) {
E.printstacktrace ();
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (InvocationTargetException e) {
E.printstacktrace ();
}
return obj;
}

//string to date
    public static date  stringtodate (String str) {
        date Date = null;
        try {
            date = Sdf.parse (str);
        } catch (ParseException e) {
             e.printstacktrace ();
        }
        return date;
    }

  Date to String
public static String datetostring (date date) {
String str = NULL;
str = Sdf.format (date);
return str;
}
}

In the actual project we may also encounter some Vo will inherit some Basevo, at this time if you want to pass map to the properties in Basevo, then we will add the following code in the tool class:

Class Superclazz = T.getclass (). Getsuperclass ();

method[] Supermethods = Clazz.getdeclaredmethods ();

It then traverses, executes the set method in Basevo, and assigns a value to the parent class property. At this point, you can add a flag flag to the parameter, and the caller decides whether to execute the above code based on whether the incoming VO has an inherited custom Basevo.

Map into a bean in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.