Talk not much, directly on the dry!
Packagecn.test;ImportJava.lang.reflect.Method;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;ImportJava.sql.ResultSetMetaData;ImportJava.sql.Timestamp;Importorg.junit.Test;ImportCn.core.domain.User; Public classUsemybites {/*** Simulation mybites dynamically create objects based on SQL. * @throwsException*/@Test Public voidSimulation ()throwsexception{//classname should be a pass-through or a specified type read from the configuration file, which is written to the test directly before it is dead. String classname= "Cn.core.domain.User"; //load driver, here is a piece of code for JDBCClass.forName ("Com.mysql.jdbc.Driver"); Connection Conn=drivermanager.getconnection ("Jdbc:mysql://192.168.1.68/base", "root", "root"); String SQL= "SELECT * from Base_user where user_id=?"; PreparedStatement PS=conn.preparestatement (SQL); Ps.setstring (1, "00a8ea6b8b524205bc0af1c3249abe54"); ResultSet RS=Ps.executequery (); //Loop through the data for each row while(Rs.next ()) {//each row represents an object, and the entity class is created by reflectionObject object=Class.forName (className). newinstance (); //gets the original column name of the query statement, the total number of columns, and the type of the column by ResultSetMetaData this objectResultSetMetaData Data=Rs.getmetadata (); //get total number of columns intCount=Data.getcolumncount (); //loop to get column names and column types for(inti = 1; I <= count; i++) {String column=data.getcolumnname (i);//Get column nameString Typename=data.getcolumntypename (i);//Get column typeString setmethodname=bulidsetmethod (column);//gets the name of the set method in the concrete entity class//According to the column type to get the column data, the specific reference database type will be corresponding to the RS.GETXXX value can be if("VARCHAR". Equals (TypeName)) {String value=rs.getstring (column);//Take value//to get a concrete method object by reflection by the method nameMethod Method=object.getclass (). GetMethod (setmethodname,string.class); //execute this method and the corresponding field to assign the valueMethod.invoke (object, value); }Else if("DATETIME". Equals (TypeName)) {//operation as above, except that the type has been replaced by a time typeTimestamp value=rs.gettimestamp (column); Method Method=object.getclass (). GetMethod (Setmethodname,timestamp.class); Method.invoke (object, value); } //The type of the database is listed, the remaining and the above is almost not written out .... } //The following is the actual process that we use to return a single object or list based on business needs .User user=(User) object; SYSTEM.OUT.PRINTLN (user); } } /*** Converts the column name to the corresponding set method name. * @paramcolumnName Column Name *@returnSet method Name*/ Publicstring Bulidsetmethod (String columnName) {string C1= "Set"; String C2=columnname.substring (0,1). toUpperCase (); String C3=columnname.substring (1); returnc1+c2+C3; } }
Ok,java all of the persistence framework in the bottom of all the JDBC to implement, then through the JDBC simulation of a mybites is mainly through the Java reflection mechanism to achieve, through the reflection of SQL column name to get a specific set method name, by executing the Set method to inject specific values to the field, So the framework itself is not difficult, as long as the point is figured out, in fact, the implementation of a few lines of code just.
Simulating implementation of entity class object functionality in Mybites through SQL