对hibernate的几天学习大概了解了hibernate的工作原理,本着作为程序员的严谨(为了记忆而已),在此就重现hibernate简易实现的全过程!!
首先,我们知道hibernate是符合ORM对象关系映射的!!所以我们从测试类出发,默认简易的hibernate已实现;
代码如下
1 package com.hibernate.Exp150705; 2 3 import org.junit.Test; 4 5 public class TestDemo { 6 7 8 @Test 9 public void test() throws Exception{ 10 11 Children child = new Children(); 12 child.setId(1); 13 child.setName("小元"); 14 child.setHeight(176); 15 //基于hibernate的工作原理,自己创建一个MySession,用于完成实体类到数据库的映射 16 MySession ms = new MySession(); 17 //创建的Mysession 具有save方法完成映射; 18 ms.save(child); 19 } 20 21 }
实体类children类也已经实现,代码如下:
1 package com.hibernate.Exp150705; 2 3 public class Children { 4 5 private int id; 6 private String name; 7 private int height; 8 9 10 public int getId() { 11 return id; 12 } 13 public void setId(int id) { 14 this.id = id; 15 } 16 public String getName() { 17 return name; 18 } 19 public void setName(String name) { 20 this.name = name; 21 } 22 public int getHeight() { 23 return height; 24 } 25 public void setHeight(int height) { 26 this.height = height; 27 } 28 29 30 }
hibernate的简易实现类 MySession ,代码如下
1 package com.hibernate.Exp150705; 2 3 import java.lang.reflect.Method; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.util.HashMap; 8 import java.util.Map; 9 10 11 public class MySession { 12 13 private String table = "children"; //表示需要插入数据的表 14 Map<String,String> map = new HashMap<String,String>();//用于保存表列名与实体类属性 (列名,类属性) 15 String [] mn;//children类方法名 16 17 public MySession(){ 18 19 map.put("id", "id"); 20 map.put("name", "name"); 21 map.put("height", "height"); 22 23 mn = new String[map.size()]; 24 } 25 26 public void save(Children child) throws Exception { 27 // TODO Auto-generated method stub 28 String sql = createSql(table);//创建插入语句 29 System.out.println(sql); 30 Class.forName("com.mysql.jdbc.Driver"); 31 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/hibernate","root","TAN19911104"); 32 PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql); 33 for(int i = 0 ; i < mn.length ; i++){ 34 35 Method method = child.getClass().getMethod(mn[i]); //利用反射机制获取children类中的指定名称的方法 36 String returnTypeName = method.getReturnType().getName();//获取该方法的返回值 37 38 if(returnTypeName.equals("java.lang.String")){ 39 40 String returnValue = (String) method.invoke(child); //获取返回值 41 ps.setString(i+1, returnValue); 42 43 } 44 45 if(returnTypeName.equals("int")){ 46 Integer returnValue = (Integer)method.invoke(child); 47 ps.setInt(i+1, returnValue); 48 } 49 50 } 51 52 ps.executeUpdate(); 53 ps.close(); 54 55 } 56 57 private String createSql(String table) { 58 // TODO Auto-generated method stub 59 String str1 = ""; 60 int index = 0; 61 for(String m : map.keySet()){//获取方法名并以getId()形式存入mn数组中 62 63 mn[index] = "get"+ Character.toUpperCase(m.charAt(0)) + m.substring(1); 64 str1 += m+","; 65 index++; 66 } 67 str1 = str1.substring(0, str1.length()-1); 68 69 String str2 = ""; 70 for(int i=0 ; i<map.size() ; i++){ //map.size() 即等于表属性个数 71 str2+="?,"; 72 } 73 str2 = str2.substring(0, str2.length()-1); //去掉末尾逗号 74 75 String sql = "insert into "+table +"( " + str1 +" ) values ( " +str2 +" )";//insert into table (id,name....) values (?,?,?....)格式类型 76 System.out.println(sql); 77 return sql; 78 79 } 80 81 }
时间: 2024-12-21 22:24:38