模拟hibernate 理解内部实现原理
View Code
create table _teacher (_id int primary key, _name varchar(20), _title varchar(10)); Teacher.java package org.fp.hibernate.model; public class Teacher { private int id; private String name; private String title; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } package org.fp.hibernate.test; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.util.HashMap; import java.util.Map; import org.fp.hibernate.model.Teacher; public class Session { String tableName = "_Teacher"; // 数据库中的字段名(也就是列名)和类中的属性名称一一对应columnField所以定义为一个Map Map<String, String> cf = new HashMap<String, String>(); String[] methodNames =null; // 现在假设我们已经从配置文件中读出来了 public Session() { cf.put("_id", "id"); cf.put("_name", "name"); cf.put("_title", "title"); methodNames = new String[cf.size()]; } public void save(Teacher t) throws Exception { String sql = createSql(); Class.forName("org.gjt.mm.mysql.Driver"); //Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/hibernate", "root", "root"); PreparedStatement ps = conn.prepareStatement(sql); for (int i = 0; i < this.methodNames.length; i++) { Method m = t.getClass().getMethod(this.methodNames[i]); Class r = m.getReturnType(); if (r.getName().equals("java.lang.String")) { String returnValue = (String) m.invoke(t); ps.setString(i+1, returnValue); } if (r.getName().equals("int")) { Integer returnValue = (Integer) m.invoke(t); ps.setInt(i+1, returnValue); } System.out.println(m.getName()+"|"+r.getName()); } ps.executeUpdate(); ps.close(); conn.close(); } private String createSql() { // TODO Auto-generated method stub // String sql= insert into tableName(_id,_name,_title) values(?,?,?); // String sql= insert into tableName(str1) values(str2); // 拼(_id,_name,_title) String str1 = ""; int index =0; /* * for (int i = 0; i < cf.keySet().size(); i++) { str1+=cf.keySet()+","; * } */ for (String s : cf.keySet()) { String v = cf.get(s); v= Character.toUpperCase(v.charAt(0))+v.substring(1);//将Key对应的首字母大写 this.methodNames[index]="get"+v; str1 += s + ","; index++; } str1 = str1.substring(0, str1.length() - 1); System.out.println("str1=" + str1); // 拼(?,?,?) String str2 = ""; for (int i = 0; i < cf.size(); i++) { str2 += "?,"; } str2 = str2.substring(0, str2.length() - 1); System.out.println("str2=" + str2); String sql = "insert into " + tableName + "(" + str1 + ")" + " values(" + str2 + ")"; System.out.println("sql=" + sql); return sql; } } package org.fp.hibernate.test; import org.fp.hibernate.model.Teacher; public class TeacherTest { public static void main(String[] args)throws Exception { // TODO Auto-generated method stub Teacher t = new Teacher(); t.setId(3); t.setName("t3"); t.setTitle("3333"); Session session = new Session(); session.save(t); } } /*str1=_id,_name,_title str2=?,?,? sql=insert into _Teacher(_id,_name,_title) values(?,?,?) getId|int getName|java.lang.String getTitle|java.lang.String*/
不要让昨天的沮丧 让今天的梦想黯然失色
成功的人总是修改方法而不修改目标
微信公众号:javenlife