模拟实现hibernate原理,采用java反射机制

本案例采用MySql数据进行测试

1.建立相应的数据库和表

create database hibernate;

use hibernate;

create table _student (_id int primary key, _name varchar(20), _age int);


2.先编写实体类

package com.newland.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;
}

}


3.模拟Session功能

package com.newland.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 com.newland.hibernate.model.Student;

public class Session {
private String tableName = "_student";
private Map<String, String> cfs = new HashMap<String, String>();

private 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:3306/hibernate", "root", "123321");
PreparedStatement ps = conn.prepareStatement(sql);
for(int i=0; i<cfs.size(); i++) {
Method m = s.getClass().getMethod(methodNames[i]);
Class r = m.getReturnType();
if(r.getName().equals("java.lang.String")) {
String tmp = (String)m.invoke(s);
ps.setString(i+1, tmp);
}
if(r.getName().equals("int")) {
Integer tmp = (Integer)m.invoke(s);
ps.setInt(i+1, tmp);
}
System.out.println(m.getName() + "|" + r.getName());
}
ps.executeUpdate();
ps.close();
conn.close();
}

private String createSQL() {
String keyStr = "";
int index = 0;
for(String key : cfs.keySet()) {
String v = cfs.get(key);
v = "get" + Character.toUpperCase(v.charAt(0)) + v.substring(1);
methodNames[index] = v;
keyStr += key + ",";
index ++;
}
keyStr = keyStr.substring(0, keyStr.length()-1);

String valueStr = "";
for(int i=0; i<cfs.size(); i++) {
valueStr += "?,";
}
valueStr = valueStr.substring(0, valueStr.length()-1);

String sql = "insert into " + tableName + "(" + keyStr + ") values (" +
valueStr + ")";
System.out.println(sql);
return sql;
}

}


4.编写测试类

package com.newland.hibernate.test;

import com.newland.hibernate.model.Student;

public class StudentTest {

public static void main(String[] args) throws Exception {
Student s = new Student();
s.setId(1);
s.setName("s1");
s.setAge(10);

Session session = new Session();

session.save(s);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值