Traversal of Java Object properties

Source: Internet
Author: User

Package Com.cn.mybatis.test;import Java.io.ioexception;import java.io.inputstream;import Java.lang.reflect.field;import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.Method; Import Java.util.date;import Org.apache.ibatis.session.sqlsession;import Org.apache.ibatis.session.sqlsessionfactory;import Org.apache.ibatis.session.sqlsessionfactorybuilder;import Com.cn.mybatis.model.EdiTestTask; Public classMybatistest { Public Static voidMain (string[] args) throws IOException, Nosuchmethodexception, SecurityException, Illegalaccessexception, IllegalArgumentException, invocationtargetexception{//configuration file for MyBatisString conf="Conf.xml"; //load the MyBatis configuration file using the ClassLoader (it also loads the associated mapping file)InputStream is=mybatistest.class. getClassLoader (). getResourceAsStream (conf); //building a factory in sqlsessionSqlsessionfactory sessionfactory=NewSqlsessionfactorybuilder (). Build ( is); Sqlsession Session=sessionfactory.opensession (); String Statement="Com.cn.mybatis.mapper.taskMapper.getAllTask"; Editesttask Editt=session.selectone (statement,"6C61BFFED61141D7A908AF0428AE57FB"); if(editt!=NULL) {field[] Field=Editt.getclass (). Getdeclaredfields ();  for(inti =0; i < field.length; i++) {String name=field[i].getname ();//get the property name of the objectString type=Field[i].getgenerictype (). toString (); System. out. println ("type of property:"+type); Name=name.substring (0,1). toUpperCase () +name.substring (1);//constructs the name of the Get methodMethod M=editt.getclass (). GetMethod ("Get"+name); if(Type.equals ("int")|| Type.equals ("class Java.lang.Integer")){//if type is a class type, it is preceded by "class", followed by the class name                    intVal=(Integer) M.invoke (Editt); System. out. println (name+"values of:"+val); }                if(Type.equals ("class Java.lang.String") ) {String val=(String) M.invoke (Editt); System. out. println (name+"values of:"+val); }                                             if(Type.equals ("class Java.lang.Short") ) {Short value=(short) m.invoke (Editt); if(Value! =NULL) {System. out. println ("attribute value:"+value); }                }                       if(Type.equals ("class Java.lang.Double") ) {Double value=(Double) M.invoke (Editt); if(Value! =NULL) {System. out. println ("attribute value:"+value); }                }                                  if(Type.equals ("class Java.lang.Boolean") ) {Boolean value=(Boolean) M.invoke (Editt); if(Value! =NULL) {System. out. println ("attribute value:"+value); }                }                if(Type.equals ("class Java.util.Date") {Date value=(Date) M.invoke (Editt); if(Value! =NULL) {System. out. println ("attribute value:"+value.tolocalestring ()); }                }                                                           }        }    }}

Traversal of Java Object properties

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.