您的位置:首页 > 其它

简单模拟Hibernate写自己的Session类

2016-04-10 11:42 357 查看
1.先写一个实体类:Student类:

package com.buaa.hibernate.bean;

public class Student {
private int id;
private String name;
private String school;
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 String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}

}
2.定义自己的Session类:

package com.buaa.hibernate.bean;

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 table = "_student";
Map<String,String> cfs = new HashMap<String,String>();
String[] methodNames;
public Session(){
cfs.put("_id", "id");
cfs.put("_name", "name");
cfs.put("_school", "school");
methodNames = new String[cfs.size()];
}

public void save(Student s) throws Exception{

String sql = createSql();

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/test";
String userName = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url,userName,password);
PreparedStatement ps = 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("int")){
//调用方法,获得返回值类型
Integer returnValue = (Integer) m.invoke(s);
System.out.println(returnValue);
ps.setInt(i+1, returnValue);
}
if(r.getName().equals("java.lang.String")){
String returnValue = (String) m.invoke(s);
System.out.println(returnValue);
ps.setString(i+1, returnValue);
}
System.out.println(m.getName() + "|" + r.getName());

}
ps.executeUpdate();
ps.close();
conn.close();

}

/*
* 获得sql语句
*/
public String createSql(){
String str1 = "";
int index = 0;
for(String s : cfs.keySet()){
String methodName = cfs.get(s);
//取得方法名:getXXX()
methodNames[index] = "get" + Character.toUpperCase(methodName.charAt(0)) + methodName.substring(1);
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 " + table + "(" + str1 + ")" + " values(" + str2 + ")";
System.out.println(sql);

return sql;
}
}


注意:有关解析配置文件的方法,请点击/article/10306601.html

3.写利用Junit写测试类:

package com.buaa.hibernate.test;

import org.junit.Test;

import com.buaa.hibernate.bean.Session;
import com.buaa.hibernate.bean.Student;

public class StudentTest {
@Test
public void test() throws Exception{
/*
* 测试之前请你确保已经在数据库中创建表_student
*/
Student s = new Student();
s.setId(1);
s.setName("neo");
s.setSchool("tsinghua");

Session session = new Session();
session.save(s);
}
}
ok,大功告成,数据成功插入到数据库中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: