hibernate实现原理模拟

来源:互联网 发布:淘宝联盟登不上 编辑:程序博客网 时间:2024/05/21 09:10
1.在数据库中新建表:user

CREATE TABLE `user` (
`id`  int(10) NOT NULL DEFAULT 0 ,
`username`  varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`pwd`  varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)

2.新建实体类:User

package com.hibernate.bean;/** * 实体类 * @author dada * */public class User {private int id;private String username;private String pwd;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}}

3.模拟Session类:

package com.hibernate.bean;import java.lang.reflect.Method;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.HashMap;import java.util.Map;import com.mysql.jdbc.PreparedStatement;/** * Session类 * @author dada * */public class Session {String tableName = "user";Map<String ,String > map = new HashMap<String ,String >();String methodNames[];public Session (){//定义在数据库中的表名与实体类的对应关系,相当于是解析hibernate的配置文件xxx.hbm.xml//key对应数据库中的表的属性列。value 就是在实体类中的属性类,使用map将他们与数据库中属性列进行一一对应。map.put("id", "id");map.put("username", "username");map.put("pwd", "pwd");methodNames = new String[map.size()];}public void save(User user) {String sql = createSql();System.out.println(sql);try {//加载数据库驱动:相当于解析hibernate.cfg.xml文件Class.forName("com.mysql.jdbc.Driver");Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","dada");PreparedStatement state =  (PreparedStatement) con.prepareStatement(sql);for(int i=0;i<methodNames.length;i++){//得到每一个方法的对象Method method = user.getClass().getMethod(methodNames[i]);//得到他的返回类型Class cla = method.getReturnType();//根据返回类型来设置插入数据库中的每个属性值。if(cla.getName().equals("java.lang.String")){String returnValue = (String)method.invoke(user);state.setString(i+1, returnValue);}else if(cla.getName().equals("int")){Integer returnValue = (Integer) method.invoke(user);state.setInt(i+1, returnValue);}System.out.println(method.getName() + "--" + method.getReturnType());}state.executeUpdate();state.close();con.close();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} }/** * 得到sql语句 * @return 返回sql语句 */private String createSql() {// TODO Auto-generated method stub//str1代表数据库中表中的属性列。并将其连接起来。String str1 = "";int index=0;for(String key :map.keySet()){str1 +=key+",";String v = map.get(key);//下面是得到对应的bean中的get方法。也就是得到User类中的getId()、getUsername()、getPwd()方法。//注意每个属性的get后面的第一个字母是大写,需要转换。v = "get" + Character.toUpperCase(v.charAt(0)) + v.substring(1);methodNames[index] = v; //将方法保存在数组中。index++;}str1  = str1.substring(0, str1.length()-1);System.out.println(str1);//得到插入数据的表达式,也就是?,?,?String str2 = "";for(int i=0;i<map.size();i++)str2 +="?,";str2 = str2.substring(0,str2.length()-1);System.out.println(str2);String sql = "insert into " + tableName +"(" + str1 + ")" + " values (" + str2 + ")"; return sql;}}


4.测试类:

package com.hibernate.bean;public class Test {public static void main(String[] args) {User user = new User();user.setId(1);user.setUsername("dada");user.setPwd("123456");Session session = new Session();session.save(user);}}

 

原创粉丝点击