使用DBUtils编写通用的DAO【很有价值】

1、定义DAO接口(便于各个版本来实现)

package tan.com;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * 访问数据的 DAO 接口. 在里面定义好访问数据表的各种方法
 * @param T: DAO 处理的实体类的类型
 * 
 *
 */
public interface DAO{
	
	
	/**
	 * 批量处理的方法
	 * @param connection
	 * @param sql
	 * @param args: 填充占位符的 Object [] 类型的可变参数.
	 * @throws SQLException 
	 */  
	void batch(Connection connection, String sql, Object [] ... args) throws SQLException;
	/**
	 * 返回具体的一个值, 例如总人数, 平均工资, 某一个人的 email 等.
	 * @param connection
	 * @param sql
	 * @param args
	 * @return
	 * @throws SQLException 
	 */
	 E getForValue(Connection connection,String sql, Object ... args) throws SQLException;
	/**
	 * 返回 T 的一个集合
	 * @param connection
	 * @param sql
	 * @param args
	 * @return
	 * @throws SQLException 
	 */
	List getForList(Connection connection,String sql,Object ...args)throws SQLException;

	/**
	 * 返回一个 T 的对象
	 * @param connection
	 * @param sql
	 * @param args
	 * @return
	 * @throws SQLException 
	 */
	T get(Connection connection,String sql,Object ...args)throws SQLException;
	
	/**
	 * INSRET, UPDATE, DELETE
	 * @param connection: 数据库连接
	 * @param sql: SQL 语句
	 * @param args: 填充占位符的可变参数.
	 * @throws SQLException 
	 * 
	 */
	void update(Connection connection,String sql,Object ...args)throws SQLException;
}

2、编写DAO的实现类JdbcDaoImpl.java

package tan.com;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

/**
 * 使用 QueryRunner 提供其具体的实现
 * @param : 子类需传入的泛型类型. 
 */
public class JdbcDAOImpl implements DAO {
	private QueryRunner queryRunner=null;
	public Class type;
	public JdbcDAOImpl(){
		queryRunner=new QueryRunner();
		type=ReflectionUtils.getSuperGenericType(getClass());
	}
	
	
	@Override
	public void batch(Connection connection, String sql, Object[]... args)
			throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public  E getForValue(Connection connection, String sql, Object... args)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List getForList(Connection connection, String sql, Object... args)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public T get(Connection connection, String sql, Object... args)
			throws SQLException {
			return queryRunner.query(connection, sql,
					new BeanHandler<>(type), args);
	}

	@Override
	public void update(Connection connection, String sql, Object... args)
			throws SQLException {
		// TODO Auto-generated method stub
		
	}

}

3、编写userDAO来继承这个实现类

package tan.com;

public class userDAO extends JdbcDAOImpl{

}


4、编写单元测试类

package tan.com;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

public class userDaoTest {
	userDAO dao=new userDAO();
	@Test
	public void testBatch() {
		fail("Not yet implemented");
	}

	@Test
	public void testGetForValue() {
		fail("Not yet implemented");
	}

	@Test
	public void testGetForList() {
		fail("Not yet implemented");
	}

	@Test
	public void testGet() throws SQLException {
		Connection connection=null;
		
		try {
			connection=JDBCTools.getConnection();
			String sql="SELECT id, name,age,school " +
					"FROM user " +
					"WHERE id = ?";
			 user user1=dao.get(connection, sql, 5);
			 System.out.println(user1);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JDBCTools.release(null, null, connection);
		}
	}

	@Test
	public void testUpdate() {
		fail("Not yet implemented");
	}

}

5、利用反射工具类获取类型

package tan.com;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 反射的 Utils 函数集合
 * 提供访问私有变量, 获取泛型类型 Class, 提取集合中元素属性等 Utils 函数
 * @author Administrator
 *
 */
public class ReflectionUtils {

	
	/**
	 * 通过反射, 获得定义 Class 时声明的父类的泛型参数的类型
	 * 如: public EmployeeDao extends BaseDao
	 * @param clazz
	 * @param index
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Class getSuperClassGenricType(Class clazz, int index){
		Type genType = clazz.getGenericSuperclass();
		
		if(!(genType instanceof ParameterizedType)){
			return Object.class;
		}
		
		Type [] params = ((ParameterizedType)genType).getActualTypeArguments();
		
		if(index >= params.length || index < 0){
			return Object.class;
		}
		
		if(!(params[index] instanceof Class)){
			return Object.class;
		}
		
		return (Class) params[index];
	}
	
	/**
	 * 通过反射, 获得 Class 定义中声明的父类的泛型参数类型
	 * 如: public EmployeeDao extends BaseDao
	 * @param 
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Class getSuperGenericType(Class clazz){
		return getSuperClassGenricType(clazz, 0);
	}
	
	/**
	 * 循环向上转型, 获取对象的 DeclaredMethod
	 * @param object
	 * @param methodName
	 * @param parameterTypes
	 * @return
	 */
	public static Method getDeclaredMethod(Object object, String methodName, Class[] parameterTypes){
		
		for(Class superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()){
			try {
				//superClass.getMethod(methodName, parameterTypes);
				return superClass.getDeclaredMethod(methodName, parameterTypes);
			} catch (NoSuchMethodException e) {
				//Method 不在当前类定义, 继续向上转型
			}
			//..
		}
		
		return null;
	}
	
	/**
	 * 使 filed 变为可访问
	 * @param field
	 */
	public static void makeAccessible(Field field){
		if(!Modifier.isPublic(field.getModifiers())){
			field.setAccessible(true);
		}
	}
	
	/**
	 * 循环向上转型, 获取对象的 DeclaredField
	 * @param object
	 * @param filedName
	 * @return
	 */
	public static Field getDeclaredField(Object object, String filedName){
		
		for(Class superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()){
			try {
				return superClass.getDeclaredField(filedName);
			} catch (NoSuchFieldException e) {
				//Field 不在当前类定义, 继续向上转型
			}
		}
		return null;
	}
	
	/**
	 * 直接调用对象方法, 而忽略修饰符(private, protected)
	 * @param object
	 * @param methodName
	 * @param parameterTypes
	 * @param parameters
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 */
	public static Object invokeMethod(Object object, String methodName, Class [] parameterTypes,
			Object [] parameters) throws InvocationTargetException{
		
		Method method = getDeclaredMethod(object, methodName, parameterTypes);
		
		if(method == null){
			throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
		}
		
		method.setAccessible(true);
		
		try {
			return method.invoke(object, parameters);
		} catch(IllegalAccessException e) {
			System.out.println("不可能抛出的异常");
		} 
		
		return null;
	}
	
	/**
	 * 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
	 * @param object
	 * @param fieldName
	 * @param value
	 */
	public static void setFieldValue(Object object, String fieldName, Object value){
		Field field = getDeclaredField(object, fieldName);
		
		if (field == null)
			throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
		
		makeAccessible(field);
		
		try {
			field.set(object, value);
		} catch (IllegalAccessException e) {
			System.out.println("不可能抛出的异常");
		}
	}
	
	/**
	 * 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
	 * @param object
	 * @param fieldName
	 * @return
	 */
	public static Object getFieldValue(Object object, String fieldName){
		Field field = getDeclaredField(object, fieldName);
		
		if (field == null)
			throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
		
		makeAccessible(field);
		
		Object result = null;
		
		try {
			result = field.get(object);
		} catch (IllegalAccessException e) {
			System.out.println("不可能抛出的异常");
		}
		
		return result;
	}
}

6、利用到JDBCTools.java工具类

package tan.com;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCTools {
	//数据库连接池应该只被初始化一次(多个项目共享一个连接池就够了)
		private static DataSource dataSource=null;
		static{
			dataSource=new ComboPooledDataSource("helloc3p0");
		}
	//获取数据库连接的方法
		public static Connection getConnection() throws Exception{
			return dataSource.getConnection();
		}
		
		
    /**
     * 2、关闭资源
     * 关闭资源关闭 Statement 和 Connection和ResultSet
     * @param rs
     * @param statement
     * @param conn
     */
	public static void release(ResultSet rs, Statement statement, Connection conn) {
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	/**
	 * 关闭 Statement 和 Connection
	 * @param statement
	 * @param conn
	 */
	public static void release(Statement statement, Connection conn) {
		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	

}

7、c3p0数据库配置同上




	
		
		
		root
		1234
		com.mysql.jdbc.Driver
		jdbc:mysql:///tan
		
		
		5
		
		5
		
		5
		
		10

		
		20
		
		5
	
	
		

测试结果:

使用DBUtils编写通用的DAO【很有价值】_第1张图片

整个方法实现流程:

使用DBUtils编写通用的DAO【很有价值】_第2张图片


虽说最终出来结果了,但我还是没能全部理解,特此记下以便日后查看。





 

你可能感兴趣的:(JDBC)