<十六>JDBC_使用 DBUtils 编写通用的DAO

接口 : DAO<T>.java

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

/*
 * 访问数据的DAO接口。
 * @param T:DAO处理的实体类的类型
 * */
public interface DAO<T> {

   /*
    * 批量处理的方法
    * @param con:数据库连接
    * @param sql:SQL语句
    * @param args:填充占位符的Object[]类型的可变参数
    * @return
    * */
   void batch(Connection con,String sql,Object[]...args);
 
   /*
    * 返回具体的一个值,eg.总人数,平均工资。。。
    * @param con:数据库连接
    * @param sql:SQL语句
    * @param args:填充占位符的可变参数
    * @return
    * */
   <E> E getForValue(Connection con,String sql,Object...args);
 
   /*
    * 返回一个T的一个集合
    * @param con:数据库连接
    * @param sql:SQL语句
    * @param args:填充占位符的可变参数
    * @return
    * */
   List<T> getForList(Connection con,String sql,Object...args);
 
   /*
    * 返回一个T的对象
    * @param con:数据库连接
    * @param sql:SQL语句
    * @param args:填充占位符的可变参数
    * @return
    * */
   T get(Connection con,String sql,Object...args) throws SQLException;
 
   /*
    * insert、update、delete
    * @param con:数据库连接
    * @param sql:SQL语句
    * @param args:填充占位符的可变参数
    * */
   void update(Connection con,String sql,Object...args);
}

DAO接口实现类:JdbcDAOImpl.java

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 com.kk.jdbc.ReflectionUtils;

/*
 * 使用QueryRunner提供其具体的实现
 * @param<T>:子类需要传入泛型的类型
 * */
public class JdbcDAOImpl<T> implements DAO<T> {

   private QueryRunner qr=null;
   private Class<T> type;
 
   public JdbcDAOImpl() {
  
      qr=new QueryRunner();
      type=ReflectionUtils.getSuperGenericType(getClass());
   }
 
   @Override
   public void batch(Connection con, String sql, Object[]... args) {
      // TODO Auto-generated method stub
  
   }

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

   @Override
   public List<T> getForList(Connection con, String sql, Object... args) {
      // TODO Auto-generated method stub
      return null;
   }

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

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

JdbcDAOImpl的子类CustomerDao.java

public class CustomerDao extends JdbcDAOImpl<Customer> {

}

测试类:CustomerDaoTest.java

import static org.junit.Assert.*;
import java.sql.Connection;
import org.junit.Test;
import com.kk.jdbc.JDBCTools;

public class CustomerDaoTest {

   CustomerDao customerDao=new CustomerDao(); 
 
   @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() {
  
      Connection con = null;
      try {
   
         con=JDBCTools.getConnection();
         String sql = "select id,name,email from customers where id=?";
         Customer customer=customerDao.get(con, sql, 6);
         System.out.println(customer);
      } catch (Exception e) {
         e.printStackTrace();
      }finally{
         JDBCTools.release(null, null, con);
      }
   }

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

原文地址:https://www.cnblogs.com/iamkk/p/6095652.html