自己动手模仿Hibernate写数据库框架
自己动手模仿Hibernate写数据库框架
最近项目有点儿停滞,论文看的也比较浮躁,于是就打算重写下后台的框架,数据库部分模仿Hibernate随手写个小程序,当然还没有写完,不过最简单的功能已经能有了,打算把代码贴上来,后续不断的更新。
简单介绍下这个框架:采用DBCP连接池,然后实现了类似Hibernate的按对象插入、更新、删除等。当然还没有写生命周期、延迟加载这些,不过后续会慢慢的加上去,否则就太小儿科了不是么。这个框架是建立在我原有的一个BDUtil的小项目上的,当时做插入这些操作需要传SQL语句和参数,现在把这步也省略了,当然最大的好处就是不用写SQL。
下面就上代码:
Commons.java: DBCP连接池,返回Connection对象。
package org.gfg.dbutil.common;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Properties;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSourceFactory;@SuppressWarnings("static-access")public class Commons {private static DataSource dataSource;static {try {Properties prop = new Properties();prop.load(Commons.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));BasicDataSourceFactory factory = new BasicDataSourceFactory();dataSource = factory.createDataSource(prop);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static Connection getConnection() throws SQLException {return dataSource.getConnection();}public static void close(ResultSet rs, PreparedStatement ps, Connection conn) {try {if (rs != null) {rs.close();}if (ps != null) {ps.close();}if (conn != null) {conn.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
package org.gfg.dbutil.common;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import org.gfg.dbutil.handler.BeanHandler;import org.gfg.dbutil.handler.IResultSetHandler;public class DbUtils {public static void insert(String sql, Object[] params) throws SQLException {// insertConnection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {conn = Commons.getConnection();ps = conn.prepareStatement(sql);for (int i = 0; i < params.length; i++) {ps.setObject(i + 1, params[i]);}ps.executeUpdate();} finally {Commons.close(rs, ps, conn);}}public static void update(String sql, Object[] params) throws SQLException {// updateDbUtils.insert(sql, params);}public static void delete(String sql, Object[] params) throws SQLException {// deleteDbUtils.insert(sql, params);}public static Object query(String sql, Object[] params,IResultSetHandler rsh) throws SQLException {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {conn = Commons.getConnection();ps = conn.prepareStatement(sql);for (int i = 0; i < params.length; i++) {ps.setObject(i + 1, params[i]);}rs = ps.executeQuery();return rsh.handler(rs);} finally {Commons.close(rs, ps, conn);}}public static Object load(String sql, Object[] params,Class<?> clazz) {IResultSetHandler rsh=new BeanHandler(clazz);Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {conn = Commons.getConnection();ps = conn.prepareStatement(sql);for (int i = 0; i < params.length; i++) {ps.setObject(i + 1, params[i]);}rs = ps.executeQuery();return rsh.handler(rs);} catch (SQLException e) {e.printStackTrace();} finally {Commons.close(rs, ps, conn);}return null;}}
package org.gfg.dbutil.handler;import java.sql.ResultSet;public interface IResultSetHandler {public Object handler(ResultSet rs);}
package org.gfg.dbutil.handler;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;import java.sql.ResultSet;import java.sql.ResultSetMetaData;public class BeanHandler implements IResultSetHandler {private Class<?> beanClass;public BeanHandler(Class<?> beanClass) {this.beanClass = beanClass;}@Overridepublic Object handler(ResultSet rs) {try {if (!rs.next()) {return null;}// 用于封装数据的beanObject bean = beanClass.newInstance();ResultSetMetaData meta = rs.getMetaData();int columnCount = meta.getColumnCount();for (int i = 0; i < columnCount; i++) {String columnName = meta.getColumnName(i + 1);Object columnValue = rs.getObject(columnName);try { //属性修改器PropertyDescriptor pd = new PropertyDescriptor(columnName,bean.getClass());Method method = pd.getWriteMethod();method.invoke(bean, columnValue);} catch (Exception e) {continue;}}return bean;} catch (Exception e) {throw new RuntimeException(e);}}}
package org.gfg.dbutil.session;import java.lang.reflect.Field;import org.gfg.dbutil.common.DbUtils;public class Session {public void save(Object obj) { //session.saveint term=obj.getClass().getName().lastIndexOf(".")+1;StringBuffer sql = new StringBuffer("Insert into "+ obj.getClass().getName().substring(term) + " (");Field[] fields = obj.getClass().getDeclaredFields();int size = fields.length;Field field = null;Object[] params = new Object[size];try {for (int i = 0; i < size - 1; i++) {field = fields[i];field.setAccessible(true);//运行时 把private 转成 publicparams[i] = field.get(obj);sql.append(field.getName() + ",");}fields[size-1].setAccessible(true);params[size - 1] = fields[size - 1].get(obj);sql.append(fields[size - 1].getName() + ") VALUES (");for (int i = 0; i < size - 1; i++) {sql.append("? , ");}sql.append("?) ");String strSQL = new String(sql);DbUtils.insert(strSQL, params);//insert} catch (Exception e) {e.printStackTrace();}System.out.println(sql);for(Object objValue:params){System.out.print(" value:"+objValue);}}public void update(Object obj) {int term=obj.getClass().getName().lastIndexOf(".")+1;StringBuffer sql = new StringBuffer("UPDATE "+ obj.getClass().getName().substring(term) + " SET ");Field[] fields = obj.getClass().getDeclaredFields();int size = fields.length;Field field = null;Object[] params = new Object[size+1];try {for (int i = 0; i < size - 1; i++) {field = fields[i];field.setAccessible(true);//运行时 把private 转成 publicparams[i] = field.get(obj);sql.append(field.getName() + "=?,");}fields[size-1].setAccessible(true);params[size - 1] = fields[size - 1].get(obj);sql.append(fields[size - 1].getName() + "=? WHERE " +fields[0].getName()+"=?");params[size]=params[0];String strSQL = new String(sql);DbUtils.update(strSQL, params);} catch (Exception e) {e.printStackTrace();}System.out.println(sql);for(Object objValue:params){System.out.print(" value:"+objValue);}}public void delete(Object obj){int term=obj.getClass().getName().lastIndexOf(".")+1;StringBuffer sql = new StringBuffer("DELETE from "+ obj.getClass().getName().substring(term) + " WHERE ");Field[] fields = obj.getClass().getDeclaredFields();sql.append(fields[0].getName()+"=?");Object[] params = new Object[1];try {fields[0].setAccessible(true);params[0]=fields[0].get(obj);String strSQL = new String(sql);DbUtils.delete(strSQL, params);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(sql);for(Object objValue:params){System.out.print(" value:"+objValue);}}public Object load(Class<?> clazz,Object uuid){Object obj=null;try {obj = clazz.newInstance();Field[] fields = obj.getClass().getDeclaredFields();Object[] params = new Object[1];int term=obj.getClass().getName().lastIndexOf(".")+1;StringBuffer sql = new StringBuffer("SELECT * from "+ obj.getClass().getName().substring(term) + " WHERE "+fields[0].getName()+"=?");params[0]=uuid;String strSQL = new String(sql);obj=DbUtils.load(strSQL,params,clazz);System.out.println(sql);for(Object objValue:params){System.out.print(" value:"+objValue);}} catch (Exception e) {e.printStackTrace();}return obj;}}
package org.gfg.dbutil.model;public class User {private int id;private String name;private String pass;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 getPass() {return pass;}public void setPass(String pass) {this.pass = pass;}}
package test;import org.gfg.dbutil.model.User;import org.gfg.dbutil.session.Session;import junit.framework.TestCase;public class TestSession extends TestCase{public void testSessionSave(){User user=new User();user.setId(0);user.setName("Alin");user.setPass("123456");Session session=new Session();session.save(user);}public void testSessionLoad(){Session session=new Session();session.load(User.class, 1);//Clazz & ID}}