After learning about the Hibernate simulation, we don't need the lib of HIbernate to simulate the hibernate working process by ourselves. In fact, we are assembling the data transmitted by the save () method in java into SQL statements by ourselves. Then execute the SQL statement. Now let's take a look. First, let's copy the previous project and name it Hibernate_0200_OR_Mapping_Simulation. First, Let's explain OR Mapping, Object Data ing (ORMORM-Object/Relational Mapper, that is, "object-relational data ing component ". For O/R (Object) and Relational (Relational data), it means that object-oriented and Relational data must be used for development at the same time. We remove hibernate lib and Teacher. Now we need to write our own Session. Create a Session class and create the save () method in it. StudentTest. java
package com.tfj.test; import com.tfj.hibernate.model.Student; public class StudentTest { public static void main(String args[]) { Student s = new Student(); s.setId(4); s.setName("s1"); s.setAge(1); Session session = new Session(); session.save(s); } }
About Session
package com.tfj.test; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import com.tfj.hibernate.model.Student; public class Session { String tableName = "student"; Map<String, String> cfs = new HashMap<String, String>(); String[] methodNames; public Session() { cfs.put("_id", "id"); cfs.put("_name", "name"); cfs.put("_age", "age"); methodNames = new String[cfs.size()]; } public void save(Student s) throws Exception { String sql = createSQL(); Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/hibernate", "root", "111111"); PreparedStatement ps = conn.prepareStatement(sql); for (int i = 0; i < methodNames.length; i++) { Method m = s.getClass().getMethod(methodNames[i]); Class r = m.getReturnType(); if (r.getName().equals("java.lang.String")) { String returnValue = (String) m.invoke(s); ps.setString(i + 1, returnValue); } if (r.getName().equals("int")) { Integer returnValue = (Integer) m.invoke(s); ps.setInt(i + 1, returnValue); } System.out.println(m.getName() + "|" + r.getName()); } ps.executeUpdate(); ps.close(); conn.close(); } private String createSQL() { String str1 = ""; String str2 = ""; int index = 0; for (String s : cfs.keySet()) { String v = cfs.get(s); v = Character.toUpperCase(v.charAt(0)) + v.substring(1); methodNames[index] = "get" + v; str1 += s + ","; index++; } str1 = str1.substring(0, str1.length() - 1); System.out.println(str1); for (int i = 0; i < cfs.size(); i++) { str2 += "?,"; } str2 = str2.substring(0, str2.length() - 1); System.out.println(str2); String sql = "insert into " + tableName + "(" + str1 + ")" + " values (" + str2 + ")"; System.out.println(sql); return sql; } }
Let's analyze the Code. There is a save () method in the Session, so we first want to build an SQL statement and create the createSQL () method. The expected SQL statement is insert into student (_ id, _ name, _ age) values (?,?,?); Associate the table with the program and use a Map for storage. The corresponding key and value are the key names of the tables in the database, and the value is the student attribute name in java. Next, we get _ id, _ name, _ student, cyclically get it from the map, and then extract the string (get ?,?,?) Similarly. In this way, our SQL is constructed. Then, convert the obtained SQL statements into question mark (?), question mark (?), and question mark ?,?,? Replace (use ps. setXXX (I + 1, getXXX () Next we will encounter the problem that, due to the various data types, we need to determine the data type and method name. Reflection is used here, and the class is obtained through s (s is a student. Various get methods (such as getName (), getAge (), and getId () can be obtained through string combination. setXXX () is also used to determine the data type returned by the reflection method () (For example, setString (I + 1, getId (), setInt (I + 1, getId ())).