结构:
DAO2_7< T >(接口)—>DAOTestImpl< T >(实现类)—>CustomerDAO(继承的子类)—>CustomerDAOTest(继承的测试类)
代码:
- 接口:
package JDBC;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* 访问数据的接口
*其中定义了访问数据表的各种方法
*@param T:DAO处理的实体类的类型
*版本:2.0
*
*/
public interface DAO2_7<T> {
/**
* 1.功能:insert、update、delete
* @param conn:链接数据库
* @param sql:SQL语句
* @param objects:占位符的可变参数
* @throws SQLException
*/
void update(Connection conn, String sql, Object ... objects ) throws SQLException;
/**
* 2.功能:返回一个T类型对象
* @param conn
* @param sql
* @param objects
* @return
* @throws SQLException
*/
T get(Connection conn, String sql, Object ...objects) throws SQLException;
/**
* 3.功能:返回一个T类型的对象的集合
* @param conn
* @param sql
* @param objects
* @return
* @throws SQLException
*/
List<T> getForList(Connection conn, String sql, Object ...objects) throws SQLException;
/**
* 4.返回具体的一个值,例如:总人数、平均数,某个人的名字。
* @param conn
* @param sql
* @param objects
* @return
* @throws SQLException
*/
<E> E getForValue(Connection conn, String sql, Object ...objects) throws SQLException;
/**
* 5.批量处理的方法
* @param conn
* @param sql
* @param objects:填充占位符的Object[]类型的可变参数
* @throws SQLException
*/
void batch(Connection conn, String sql, Object[] ...objects) throws SQLException;
}
- 实现类
package JDBC;
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 org.apache.commons.dbutils.handlers.ScalarHandler;
import 反射机制.ReflectionUtils;
public class DAOTestImpl<T> implements DAO2_7<T> {
private QueryRunner queryRunner = null;
private Class <T> type;
//构造器
public DAOTestImpl(){
queryRunner = new QueryRunner();
type = ReflectionUtils.getSuperGenericType(getClass());
}
//更新数据
@Override
public void update(Connection conn, String sql, Object... objects) throws SQLException {
queryRunner.update(conn, sql, objects);
}
//查询数据
@Override
public T get(Connection conn, String sql, Object... objects) throws SQLException {
return queryRunner.query(conn, sql, new BeanHandler<>(type), objects);
}
//获取多条记录
@Override
public List<T> getForList(Connection conn, String sql, Object... objects) throws SQLException {
return queryRunner.query(conn, sql, new BeanListHandler<>(type), objects);
}
//获取某一列值,或计算总数
@Override
public <E> E getForValue(Connection conn, String sql, Object... objects) throws SQLException {
return (E)queryRunner.query(conn, sql, new ScalarHandler(), objects);
}
//批量操作
@Override
public void batch(Connection conn, String sql, Object[]... objects) throws SQLException {
queryRunner.batch(conn, sql, objects);
}
}
- 继承的子类
package JDBC;
import JDBCTest.Customers;
public class CustomerDAO extends DAOTestImpl<Customers> {
}
- 继承子类的测试类
package JDBC;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;
import org.junit.Test;
import JDBCTest.Customers;
import JDBCTest.TestTools;
public class CustomerDAOTest extends CustomerDAO {
Connection conn = null;
CustomerDAO customerDao = new CustomerDAO();
//构造器
public CustomerDAOTest() throws Exception{
conn = TestTools.getConnection();
}
@Test
public void testUpdate() {
try {
String sql = "INSERT INTO customers(id,name,age,birth,address) VALUES(?, ?, ?, ?, ?)";
customerDao.update(conn, sql, 1, "张力", "32", new Date(new java.util.Date().getTime()), "上海市" );
System.out.println("OK");
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
@Test
public void testGet() {
try {
String sql = "select id, name, age, birth from customers where id = ?";
Customers customer = customerDao.get(conn, sql, 15);
System.out.println(customer);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
@Test
public void testGetForList() {
try {
String sql = "select id, name, age, birth from customers where id > ?";
List<Customers> customers = customerDao.getForList(conn, sql, 15);
System.out.println(customers);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
@Test
public void testGetForValue() {
try {
String sql = "select count(id) from customers";
Object result = customerDao.getForValue(conn, sql, null);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
@Test
public void testBatch() {
try {
String sql = "INSERT INTO customers(name, age, birth, address) VALUES(?, ?, ?, ?)";
Object objects[][] = new Object[1000][4];
for(int i = 0; i < 1000; i++){
objects[i][0] = "name_"+i;
objects[i][1] = "20";
objects[i][2] = (new Date(new java.util.Date().getTime()));
objects[i][3] = "河南省";
}
customerDao.batch(conn, sql, objects);
System.out.println("OK");
} catch (Exception e) {
e.printStackTrace();
}finally{
TestTools.release(null, conn);
}
}
}