通用DAO和DAO的实现类

这篇博客介绍了如何利用c3p0连接池、JDBCTools和commons-dbutils来实现通用Dao和DaoImpl。文章提到了需要的jar包,并提供了Emp实体类、通用DAO接口和通用DaoImpl的实现。在实际应用中,只需为每个实体类创建特定的DaoImpl,继承自通用DaoImpl,即可方便地进行数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通用Dao和DaoImpl的实现需要借助于前两篇博客中的c3p0连接池,JDBCTools,commons-dbutils。

需要的jar包分别是 c3p0-0.9.5.2 、 commons-dbutils-1.6 、mchange-commons-java-0.2.11 、mysql-connector-java-5.1.10-bin

首先有Emp实体类和两个工具类,工具类代码如下:

package com.xyj.utils;

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;

/**
 * JDBC工具类
 */
public class JDBCTools {
	//1、创建数据源
	private static DataSource dataSource=null;
	
	//使用静态代码块初始化数据
	static {
		dataSource=new ComboPooledDataSource("democ3p0");//ComboPooledDataSource C3p0的数据源
	}
	
	//2、获取连接方法
	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}
	
	//关闭连接
	public static void closeDB(ResultSet rs,Statement stmt,Connection conn) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn!=null) {
			try {
				//数据连接池的Connection,进行close()的时候,不是真正的关闭
				//而是把连接返回给连接池
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	
}

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<!--定义调用此XML文件的名字  -->
	<named-config name="democ3p0">
		<!-- 设置数据源的基本属性 -->
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql:///</property>
		<property name="user">root</property>
		<property name="password">root</property>
		
		<!-- 连接池的高级配置 -->
		<!-- 若数据库中连接数不足时,一次向数据库中申请多少个连接 -->
		<property name="acquireIncrement">5</property>
		<!-- -初始化数据库连接池时连接的数量 -->
		<property name="initialPoolSize">5</property>
		<!-- 数据库连接池中最小的数据库连接数 -->
		<property name="minPoolSize">5</property>
		<!-- 数据库连接池中最大的数据库连接数 -->
		<property name="maxPoolSize">10</property>
		<!-- C3P0数据库连接池可以维护的Statement的个数 -->
		<property name="maxStatements">20</property>
		<!-- 每个连接同时可以使用的Statement对象的个数 -->
		<property name="maxStatementsPerConnection">5</property>
	</named-config>
	
	
</c3p0-config>
package com.xyj.utils;

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 YanYuhang
 *
 */
public class ReflectionUtils {

	
	/**
	 * 通过反射, 获得定义 Class 时声明的父类的泛型参数的类型
	 * 如: public EmployeeDao extends BaseDao<Employee, String>
	 * @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<Employee, String>
	 * @param <T>
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static<T> Class<T> 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;
	}
}

通用DAO接口

package com.xyj.dao;

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

/**
 * 通用DAO
 * T 任何类都可以使用这个接口中的方法
 */
public interface Dao<T> {
	
	/**
	 * 批处理
	 * @param conn
	 * @param sql
	 * @param args
	 * @throws SQLException
	 */
	void batch(Connection conn,String sql,Object[]... args) throws SQLException ;
	
	/**
	 * 获取一个具体的值  eg:总人数,总和,某某的年龄……
	 * @param conn
	 * @param sql
	 * @param args
	 * @return
	 * @throws SQLException
	 */
	<E> E getForValue(Connection conn,String sql,Object... args) throws SQLException ;
	
	
	/**
	 * 获取一个对象
	 * @param conn 连接对象
	 * @param sql  SQL语句
	 * @param args 可变参数  用于替代占位符?
	 * @return     返回泛型类对象
	 */
	T get(Connection conn,String sql,Object... args) throws SQLException ;
	
	/**
	 * 获取多个对象,返回到List集合中
	 * @param conn 连接对象
	 * @param sql  SQL语句
	 * @param args 可变参数  用于替代占位符?
	 * @return     返回List集合
	 */
	List<T> getForList(Connection conn,String sql,Object... args) throws SQLException ;
	
	/**
	 * 增删改方法 
	 * @param conn 连接对象
	 * @param sql  SQL语句
	 * @param args 可变参数  用于替代占位符?
	 * @return     返回受影响行数
	 */
	int update(Connection conn,String sql,Object... args) throws SQLException ;
	
}

通用DaoImpl

package com.xyj.dao;

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;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.xyj.utils.ReflectionUtils;

public class JDBCDaoImpl<T> implements Dao<T>{
	
	//1.创建QueryRunner对象
	QueryRunner qr=null;
	
	//2.创建获取Class类型的对象
	Class<T> type;
	
	public JDBCDaoImpl() {
		this.qr = new QueryRunner();
		this.type =ReflectionUtils.getSuperGenericType(getClass());
	}

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

	@Override
	public List<T> getForList(Connection conn, String sql, Object... args) throws SQLException {
		return qr.query(conn, sql,new BeanListHandler<>(type), args);
	}

	@Override
	public int update(Connection conn, String sql, Object... args) throws SQLException {
		return qr.update(conn, sql, args);
	}
	
	@Override
	public void batch(Connection conn, String sql, Object[]... args) throws SQLException {
		qr.batch(conn, sql, args);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <E> E getForValue(Connection conn, String sql, Object... args) throws SQLException {
		return (E)qr.query(conn,sql,new ScalarHandler<>(),args);
	}

}

以上就是通用Dao和DaoImpl,具体使用时,还要有一个实体类的DaoImpl,直接继承通用DaoImpl即可

package com.xyj.dao;

import com.xyj.entity.Emp;

public class EmpDaoImpl extends JDBCDaoImpl<Emp>{

}

然后就可以使用啦:

package com.xyj.test;

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

import org.junit.Test;

import com.xyj.dao.EmpDaoImpl;
import com.xyj.entity.Emp;
import com.xyj.utils.JDBCTools;

@SuppressWarnings("all")
public class TestEmp {

	EmpDaoImpl edi=new EmpDaoImpl();
	@Test
	public void test() {
		try {
			List<Emp> list = edi.getForList(JDBCTools.getConnection(),"select * from jdbcdemo20190804.emp",null);
			System.out.println("---------------EMP表的全部信息--------------");
			list.forEach(System.out::println);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	//查询表中总记录数
	@Test
	public void test2() throws SQLException {
		Object value = edi.getForValue(JDBCTools.getConnection(),"select count(1) from jdbcdemo20190804.emp",null);
		System.out.println(value);
	}
	
	//查询表中money列的总和
	@Test
	public void test3() throws SQLException {
		Object value = edi.getForValue(JDBCTools.getConnection(),"select sum(money) from jdbcdemo20190804.emp",null);
		System.out.println(value);
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值