模拟hibernate save方法的实现过程

hibernate让数据库操作变得简单,最近看了一些模拟hibernate实现过程的代码,自己也尝试了下模拟hibernate save方法的实现,当作一个笔记,希望也对大家有帮助。

下面直接po代码吧,注释写的也算清楚,我们要做的就是将用save方法将wife对象存入数据库。

public class Wife {
	private int id;
	private String name;
	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;
	}
}
下面是session实现方法

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class Session {
	private Map<String,Object> map = new HashMap<String,Object>();
	String tableName = "t_wife";
	//该数组用于存放方法名
	String[] method = null;
	public Session(){
		//数据库字段和类属性对应
		map.put("id","id");
		map.put("name", "name");
		method = new String[map.size()];
	}
	public void save(Wife wife) throws ClassNotFoundException, SQLException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		//sql语句
		String sqlString = writesql();
		//数据库驱动注册
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库,用户密码依据自己情况写
		Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/mydata?" +
                                   "user=xxx&password=xxxx");
		PreparedStatement pst = connection.prepareStatement(sqlString);
		for (int i = 0; i < method.length; i++) {
			//反射机制获取wife中get方法对象,主要目的为获取返回值类型和返回值
			Method m = wife.getClass().getMethod(method[i]);
			if (m.getReturnType().getName().equals("java.lang.String")) {
				//获取get方法的返回值
				String returnvalue = (String)m.invoke(wife);
				pst.setString(i+1, returnvalue);
			}
			if(m.getReturnType().getName().equals("int")){
				pst.setInt(i+1,(Integer)m.invoke(wife) );
			}
		}
		pst.executeUpdate();
		pst.close();
		connection.close();
	}
	//sql语句拼接
	//返回值为拼接好的的sql语句
	private String writesql() {
		String properties = "";
		String value = "";
		int index = 0;
		for(String s:map.keySet()){
			properties+=s+",";
			//拼接get方法 如getId中的Id首字母为大写,取出首字母大写 
			char c = Character.toUpperCase(((String) map.get(s)).charAt(0));
			method[index++] = "get"+c+s.substring(1);
		}
		//字符串截取
		properties = properties.substring(0,properties.length()-1);
		for (int i = 0; i < map.size(); i++) {
			value+="?,";
			
		}
		//字符串截取
		value = value.substring(0,value.length()-1);
		//sql语句拼接
		String sqlString = "insert into "+tableName+"("+properties+") values("+value+")";
		return sqlString;
	}
}
最后写一个main方法测试一下,看看能不能将数据存入数据库即可

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;

public class Hibernate_save {
	public static void main(String[] args) throws ClassNotFoundException, SQLException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Session session = new Session();
		Wife wife = new Wife();
		wife.setId(23);
		wife.setName("tom");
		session.save(wife);
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值