hibernate模拟实现

Java知识分享网-免费Java资源下载

大致想法:

写一个配置文件,将表与实体一一对应;

将配置文件内容度出,自己设定一个session对象;

session调用save 方法时,会根据配置文件读出对应的类的相应内容,拼成SQL语句,执行到数据库中。

因为是模拟hibearnate,所以不使用hibernate的jar包,只需要mysql驱动包。

测试驱动开发

具体开发:

建表_student

写Student.java实体类

package com.lch.hibernate.model;

public class Student {

    private int id; // 编号
    private String name; // 姓名
    private int age; // 年龄
    
    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;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}

写Session.java类

package com.lch.hibernate.model;

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;

public class Session {
    
    // 假设已经读到配置文件的内容了
    String tableName = "_Student";
    // 字段与属性一一对应
    Map<String , String> cfs =  new HashMap<String, String>();
    
    String[] methodNames ;
    
    public Session(){
        cfs.put("_id", "id");
        cfs.put("_name", "name");
        cfs.put("_age", "age");
        
        methodNames = new String[cfs.size()];
    }
    
    public void save(Student s) throws Exception{
        String sql = createSQL();
        
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/hibernate","root","hjj");
        PreparedStatement pstm = conn.prepareStatement(sql);
        for(int i=0; i<methodNames.length; i++) {
            Method m = s.getClass().getMethod(methodNames[i]); // 使用反射拿到方法原型
            Class r = m.getReturnType(); // 拿到方法返回值类型
            
            if(r.getName().equals("java.lang.String")){
                String returnValue = (String)m.invoke(s); // 拿到返回值
                pstm.setString(i+1, returnValue);
            }
            if(r.getName().equals("int")){
                Integer returnValue = (Integer)m.invoke(s); // 拿到返回值
                pstm.setInt(i+1, returnValue);
            }
            
            System.out.println(m.getName() +"|"+ r.getName());
        }
        /*for(int i=0; i<cfs.size(); i++) {
            pstm.setxxx(i+1, s.getxx);
        }*/
        
        pstm.executeUpdate();
        pstm.close();
        conn.close();
    }

    private String createSQL() {
        
        String str1 = "";
        int index = 0;
        for(String s : cfs.keySet()){
            String v = cfs.get(s);
            v = Character.toUpperCase(v.charAt(0)) + v.substring(1); // 拿到第一个字母转为大写
            methodNames[index] = "get" + v; // 拼getter方法
            str1 += s + ",";
            index ++;
        }
        str1 = str1.substring(0, str1.length()-1);
        System.out.println(str1);
        
        String str2 = "";
        for(int i=0; i<cfs.size(); i++){
            str2 += "?,";
        }
        str2 = str2.substring(0, str2.length()-1);
        System.out.println(str2);
        
        String sql = "insert into "+ tableName + "(" + str1 + ")" + "values (" + str2 + ")";
        System.out.println(sql);
        return sql;
    }

}

该类的用途是通过配置文件,拼凑SQL语句并执行。

测试类StudentTest.java

package com.lch.hibernate.model;


public class StudentTest {

    public static void main(String[] args) throws Exception{
        Student s = new Student(); // 创建student对象
        s.setId(2);
        s.setName("s2");
        s.setAge(2);
        
        Session session = new Session(); // new 一个自己的session
        
        session.save(s);
        
    }
}

运行结果:

原文地址:https://www.cnblogs.com/ligui989/p/3442121.html