Javabean-commonutils Tool Class

Source: Internet
Author: User

 PackageCn.code.domain; Public classPerson {PrivateString name; Private intAge ; PrivateString Gender; @Override PublicString toString () {return"Person [name=] + name +", age= "+ Age +", gender= "+Gender+ "]"; }     PublicPerson () {Super(); //TODO auto-generated Constructor stub    }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString Getgender () {returngender; }     Public voidSetgender (String gender) { This. Gender =gender; }     PublicPerson (String name,intAge , String gender) {        Super();  This. Name =name;  This. Age =Age ;  This. Gender =gender; }    }
 PackageCn.code.domain;ImportJava.util.HashMap;ImportJava.util.Map;Importorg.apache.commons.beanutils.BeanUtils;Importorg.junit.Test; Public classDemo1 {@Test Public voidFUN1 ()throwsexception{String className= "Cn.code.domain.Person"; Class Clazz=Class.forName (className); Object P=clazz.newinstance (); Beanutils.setproperty (P,"Name", "Zhang San"); Beanutils.setproperty (P,"Age", "23"); Beanutils.setproperty (P,"Gender", "Boy");    System.out.println (P.tostring ()); } @Test Public voidFun2 ()throwsexception{Map<string,string>map =NewHashmap<string, string>(); Map.put ("username", "Killy"); Map.put ("Password", "123"); User User=NewUser ();        Beanutils.populate (user, map);    SYSTEM.OUT.PRINTLN (user); }}
 PackageCn.code.domain; Public classUser {PrivateString username; Private intpassword; @Override PublicString toString () {return"User [username=" + Username + ", password=" + password + "]"; }     PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     Public intGetPassword () {returnpassword; }     Public voidSetPassword (intpassword) {         This. Password =password; }     PublicUser () {Super(); //TODO auto-generated Constructor stub    }     PublicUser (String username,intpassword) {        Super();  This. Username =username;  This. Password =password; }    }
 Packagecn.code.CommonUtils;ImportJava.util.Map;ImportJava.util.UUID;Importorg.apache.commons.beanutils.BeanUtils; Public classCommonutils { Public StaticString uuid () {returnUuid.randomuuid (). toString (). Replace ("-", ""). toUpperCase (); }     Public Static<T> T Tobean (Map map,class<t>clazz) {        Try{T Bean=clazz.newinstance ();            Beanutils.populate (BEAN,MAP); returnBean; }Catch(Exception e) {Throw NewRuntimeException (e); }    }}

Dependent jar Package: Apache_commons_beanutils commmons_logging

Javabean-commonutils Tool Class

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.