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