本例子是不使用Hibernate的jar包,单纯地使用java反射机制、JDBC模拟Hibernate的Session实现对象保存的功能。该程序体现了hibernate在于数据库交互过程中生成SQL语句,识别Getter,Setter方法向数据库插入数据的原理。
Student.java
package com.zenoh.bean; public class Student { private int id ; private int age ; private String title ; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
DBUtil.java
package com.zenoh.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { private static Connection conn = null ; private static PreparedStatement ps = null ; private ResultSet rs = null ; public static Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver") ; conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hibernate","root","root") ; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn ; } public static PreparedStatement createStatement(String sql) { try { conn = getConnection() ; ps = conn.prepareStatement(sql) ; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ps ; } public static void close() { try { ps.close() ; conn.close() ; } catch (Exception e) { e.printStackTrace() ; } } }
Session.java
package com.zenoh.session; import java.lang.reflect.Method; import java.sql.PreparedStatement; import java.util.HashMap; import java.util.Map; import com.zenoh.bean.Student; import com.zenoh.db.DBUtil; public class Session { //先假定数据表名称 String tableName = "_student" ; //保存数据库列名和类的属性名 Map<String , String> map = new HashMap<String,String>() ; //类的属性方法名保存为字符串 String[] methodNames ; public void save(Student s) { PreparedStatement ps = null ; map.put("_id", "id") ; map.put("_age" , "age") ; map.put("_title", "title") ; methodNames = new String[map.size()] ;//根据map的大小创建属性方法的字符串的个数 String sql = createSQL() ;//创建SQl语句 try { ps = DBUtil.createStatement(sql) ; for(int i=0; i<methodNames.length; i++){ //利用Method的反射机制获取类的方法 Method m = s.getClass().getMethod(methodNames[i]) ; //获得方法的返回值类型 Class type = m.getReturnType() ; System.out.println(m.getClass() + " || " + m.getReturnType() ); //根据返回值类型匹配,设置PreparedStatement的值 if(type.getName().equals("java.lang.String")){ String returnValue = (String)m.invoke(s) ; ps.setString(i+1,returnValue) ; } if(type.getName().equals("int")){ Integer returnValue = (Integer)m.invoke(s) ; ps.setInt(i+1,returnValue) ; } } ps.executeUpdate() ; } catch (Exception e) { e.printStackTrace() ; } } private String createSQL() { String str1 = "" ; String str2 = "" ; int index = 0 ; for(String s : map.keySet()){ String v = map.get(s) ; //构造方法名称 methodNames[index] = "get" + Character.toUpperCase(v.charAt(0)) + v.substring(1) ; str1 += s + "," ; index++ ; } str1 = str1.substring(0,str1.length()-1) ; for(int i=0; i<map.size() ; i++){ str2 += "?," ; } str2 = str2.substring(0, str2.length()-1) ; String sql = "insert into " + tableName + " (" + str1 + " ) values (" + str2 +")" ; return sql; } }
测试类
package com.zenoh.test; import com.zenoh.bean.Student; import com.zenoh.session.Session; public class SessionTest { public static void main(String args[]) { Student s = new Student() ; s.setAge(20) ; s.setTitle("hello") ; Session session = new Session() ; session.save(s) ; } }
SQl语句
create table _student(_id int primary key , _age int , _title varchar(20))