笔记:Apache封装

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值