日期:2014-05-16  浏览次数:20721 次

分享一个很轻的、纯JDBC ORM映射小框架,泛型化,SQL级别

首先这是一个超轻的纯JDBC 框架,说是ORM,但又不配,但又具有一点点ORM的特性。四不像,怎么称呼,各位看官自取。

笨人笨舌的,看看代码吧,也许您就更清楚:

首先定义一个通用Dao的借口,定义了有限的几个方法:

package com.xiaomin.dao;

import java.util.List;

/**
 * 利用泛型操作数据表
 * 
 * @author xiaomin
 * 
 * @param <T>
 */
public interface IBaseDao<T> {

	/**
	 * 插入对象
	 * 
	 * @param sql
	 * @param params
	 */
	int add(String sql, Object... params);

	/**
	 * 查找多个对象
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	List<T> query(String sql, Object... params);

	/**
	 * 查找对象
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	T get(String sql, Object... params);

	/**
	 * 执行更新的sql语句,插入,修改,删除
	 * 
	 * @param sql
	 * @return
	 */
	boolean update(String sql);
}

?对应实现是重点:

package com.xiaomin.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.xiaomin.dao.IBaseDao;

/**
 * 利用泛型操作数据表
 * 
 * @author xiaomin
 * 
 * @param <T>
 */
public class BaseDAOImpl<T> implements IBaseDao<T> {

	// 在构造函数中反射出泛型类对象
	private Class<T> tClass;

	@SuppressWarnings("unchecked")
	public BaseDAOImpl() {
		tClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

	/**
	 * 插入对象
	 * 
	 * @param sql
	 * @param params
	 */
	public int add(String sql, Object... params) {
		Connection conn = getConnection();
		QueryRunner qr = new QueryRunner();
		try {
			int inserts = qr.update(conn, sql, params);
			return inserts;
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		} finally {
			DbUtils.closeQuietly(conn);
		}
	}

	/**
	 * 查找多个对象
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	@SuppressWarnings( { "unchecked", "deprecation" })
	public List<T> query(String sql, Object... params) {
		List<T> beans = null;
		Connection conn = null;
		try {
			conn = getConnection();
			QueryRunner qRunner = new QueryRunner();
			beans = (List<T>) qRunner.query(conn, sql, params, new BeanListHandler(tClass));
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.closeQuietly(conn);
		}
		return beans;
	}

	/**
	 * 查找对象
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	@SuppressWarnings( { "unchecked", "deprecation" })
	public T get(String sql, Object... params) {
		T obj = null;
		Connection conn = null;
		try {
			conn = getConnection();
			QueryRunner qRunner = new QueryRunner();

			List<T> litT = (List<T>) qRunner.query(conn, sql, params, new BeanListHandler(tClass));

			if (litT != null && litT.size() > 0)
				obj = litT.get(0);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.closeQuietly(conn);
		}

		return obj;
	}

	/**
	 * 执行更新的sql语句,插入,修改,删除
	 * 
	 * @param sql
	 * @return
	 */
	public boolean update(String sql) {
		Connection conn = null;
		boolean flag = false;
		try {
			conn = getConnection();
			QueryRunner qRunner = new QueryRunner();
			int i = qRunner.update(conn, sql);
			if (i > 0) {
				flag = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.closeQuietly(conn);
		}
		return flag;
	}

	/**
	 * 数据库连接 若生产环境下,建议使用数据连接池获取链接
	 * 
	 * @return
	 */
	private static Connection getConnection() {
		Connection conn = null;

		PropertiesConfiguration propertyUtil = null;
		try {
			propertyUtil = new PropertiesConfiguration("/jdbc.properties");
		} catch (Exception e) {
			return null;
		}

		String driver = propertyUtil.getString("jdbc.driverClassName");
		String url = propertyUtil.getString("jdbc.url");

		DbUtils.loadDriver(driv