首页 > Web开发 > 详细

Hibernate 原理的简单模拟

时间:2015-10-27 16:47:19      阅读:224      评论:0      收藏:0      [点我收藏+]
技术分享
 1 package com.wk.model;
 2 
 3 public class Student {
 4     private int id;
 5     private String name ;
 6     private String age;
 7     public int getId() {
 8         return id;
 9     }
10     public void setId(int id) {
11         this.id = id;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public String getAge() {
20         return age;
21     }
22     public void setAge(String age) {
23         this.age = age;
24     }
25     
26 
27 }
View Code

首先需要一个Student Bean ,可以是任意Bean ,做测试的话,越简单越好

主要模拟的事  session.save(object);  当 session.save(object); 被执行以后,数据就直接保存在数据库中,实现的底层代码的封装

第二步创建一个 Session 类   

技术分享
  1 package com.wk.test;
  2 
  3 import java.lang.reflect.Method;
  4 import java.sql.DriverManager;
  5 import java.util.HashMap;
  6 import java.util.Map;
  7 
  8 import com.mysql.jdbc.Connection;
  9 import com.mysql.jdbc.PreparedStatement;
 10 import com.wk.model.Student;
 11 
 12 /**
 13  * @author 所谓
 14  * 
 15  */
 16 public class Session {
 17 
 18     String tableName = "_Student";
 19     Map<String, String> cfs = new HashMap<String, String>();
 20     
 21     String [] methodNames;
 22     public Session() {
 23         cfs.put("_id", "id");
 24         cfs.put("_name", "name");
 25         cfs.put("_age", "age");
 26         methodNames = new String[cfs.size()];
 27     }
 28 
 29     /**
 30      * 假设已经读取到了配置文件 再去做后面的事情
 31      * 
 32      * @param student
 33      */
 34     public void save(Student student) throws Exception {
 35         
 36         // 创建好SQL 语句 ,接下来就要连接数据了.
 37         
 38         String sql = createSQL();
 39 
 40         Class.forName("com.mysql.jdbc.Driver");
 41         System.out.println("数据库驱动加载成功");
 42         Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/hibernate", "root", "123456");
 43         System.out.println("可以进行数据操作");
 44         
 45         //  使用 PrepareStatement 设置值  难点
 46         
 47         PreparedStatement  ps  = (PreparedStatement) connection.prepareStatement(sql);
 48         
 49         /**
 50          *     难点  [利用Java的反射机制]  
 51          * 
 52          *  反射  [详解] http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html
 53          */
 54         for (int i=0;i<methodNames.length;i++) {
 55             Method m= student.getClass().getMethod(methodNames[i]);
 56             Class r = m.getReturnType();
 57             System.out.println(m.getName()+"|"+r.getName());  // 获取应该设置的方法名
 58             if (r.getName().equals("int")) {
 59                 Integer returnValue =(Integer) m.invoke(student);
 60                 ps.setInt(i+1, returnValue);
 61             }
 62             if (r.getName().equals("java.lang.String")) {
 63                 String returnValue =(String) m.invoke(student);
 64                 ps.setString(i+1, returnValue);
 65             }
 66             if (r.getName().equals("java.lang.String")) {
 67                 String returnValue =(String) m.invoke(student);
 68                 ps.setString(i+1, returnValue);
 69             }
 70         }
 71         
 72         for (int i = 0; i < cfs.size(); i++) {
 73             
 74             
 75         }
 76         
 77         
 78         ps.executeUpdate();
 79         
 80         /** 假设值已经设置成功,最后执行操作
 81          *  难点  [利用Java的反射机制]
 82          */
 83         
 84         ps.close();
 85         connection.close();
 86         /**
 87          *     流执行完以后要记得关闭
 88          */
 89         
 90         
 91     }
 92 
 93     private String createSQL() {
 94         
 95         /**
 96          *  str2 设置的事表中的字段名,str1 设置的表中的 需要插入的值
 97          */
 98         String str2 = "", str1 = "";
 99         /**
100          *  循环得到是 str1 的值
101          */
102         
103         int index =0;
104         for (String s : cfs.keySet()) {
105             String v  = cfs.get(s);
106             v=Character.toUpperCase(v.charAt(0))+v.substring(1);
107             methodNames[index] ="get"+v;
108             str1 += s + ",";
109             index++;
110         }
111 
112         str1 = str1.substring(0, str1.length() - 1);
113         /**
114          *  循环得到 str2 的值     获取value中的 值也就是 ? 号了.
115          */
116         for (int i = 0; i < cfs.size(); i++) {
117             str2 += "?,";
118         }
119         str2 = str2.substring(0, str2.length() - 1);
120         System.out.println(str2);
121         String sql = "insert into " + tableName + "(" + str1 + ")" + "values("
122                 + str2 + ")";
123         System.out.println(sql);
124         return sql;
125 
126     }
127 
128 }
View Code

第三步: 测试类:

 

 1 package com.wk.test;
 2 
 3 import com.wk.model.Student;
 4 
 5 /**
 6  * @author 所谓   测试驱动开发:   Hibernate 模拟实现 :
 7  *
 8  */
 9 public class StudentTest {
10     /**
11      * 假设什么事情已经完成了,再去做某件事;
12      * @param args
13      * @throws Exception 
14      */
15     public static void main(String[] args) throws Exception {
16         Student  student  = new  Student();
17         student.setId(1);
18         student.setName("zhangsan");
19         student.setAge("12");
20         Session  session  = new Session();
21         /**
22          * 模拟 Hibernate 传递一个学生对象,调用一个保存的方法,数据就可以保存到数据库中
23          * 
24          */
25         session.save(student);
26         
27     }
28 
29 }

最后,感谢下,尚学堂的马老师,听了他的课以后整理出来的...

 

Hibernate 原理的简单模拟

原文:http://www.cnblogs.com/suowei/p/4914329.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!