Apache封装
导入jar包
之后重新创建基类
import day15.jdbc.utiks.JDBCUtils;
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 java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/*
* Apache工具包中有一个结果集处理器接口
* ResultSetHandler
* 它的实现类有:
* BeanListHandler:将结果集中每一行数据都封装到一个对应的JavaBean实例中 -》结果是一个List<T>
* BeanHandler:结果是一个JavaBean对象
* MapHandler:结果是一行多列,但是又不是JavaBean对象
* MapListHandler:结果是一行多列,但是不是JavaBean对象
* ScalarHandler<T>:单个值的封装对象
*
*
* */
public class ApacheBasicDAO {
private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());// 传入连接池对象
// 适用于insert、update、delete
public int update(String sql,Object...args) throws SQLException { //提取更新的方法 可变参数是SQL语句?
int update = queryRunner.update(sql, args);
return update;
}
public int update(Connection connection,String sql, Object...args) throws SQLException { //考虑到共享连接
int update = queryRunner.update(connection,sql, args);
return update;
}
// 查询
public <T> List<T> getAll(Class<T> tClass, String sql, Object...args) throws SQLException {
return queryRunner.query(sql,new BeanListHandler<>(tClass),args); //中间的参数传ResultSetHandler接口实现类
}
//查询单个
public <T> T getById(Class<T> tClass,String sql,Object...args) throws SQLException {
return queryRunner.query(sql,new BeanHandler<>(tClass),args);
}
// 查询总记录数、
public Object queryObject(String sql,Object...args) throws SQLException {
return queryRunner.query(sql,new ScalarHandler<>(),args);
}
}
之后再重写写一个接口:
import day15.bean.Student;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
public interface ApacheStudentDAO {// 和学生操作相关接口
// void addStudent(Student student) throws ParseException, SQLException;
// ArrayList<Student> getAllStudent() throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException;
// void update(Student student) throws SQLException;// 修改
// void deleteBySid(String sid) throws SQLException;// 删除
// 查询id对应对象
Student getById(String sid) throws SQLException;
// 查询总记录数
long count() throws SQLException;
}
重新写一个工具类,继承基类并且实现接口
import day15.bean.Student;
import java.sql.SQLException;
public class ApacheStudentDAOImpl extends ApacheBasicDAO implements ApacheStudentDAO{
@Override
public Student getById(String sid) throws SQLException {
String sql = "select * from student where SId = ?";
Student byId = getById(Student.class, sql, sid);
return byId;
}
@Override
public long count() throws SQLException {
String sql = "select count(*) from student ";
Object object = queryObject(sql);
return (long)object;
}
}
测试类:
import day15.bean.Student;
import java.sql.SQLException;
public class TestApacheStudentDAOImpl {
public static void main(String[] args) throws SQLException {
// 创建DAO对象
ApacheStudentDAOImpl apacheStudentDAO = new ApacheStudentDAOImpl();
Student byId = apacheStudentDAO.getById("01");
System.out.println(byId);
long count = apacheStudentDAO.count();
System.out.println(count);
}
}