Apache dbutils

在Apache dbutils中有两个核心接口/类:QueryRunner和ResultSetHandler;下面是讲解
在这里插入图片描述
上代码

public class UserDao {
    private QueryRunner queryRunner=new QueryRunner((DataSourceUtil.getDataSource()));
    //开启驼峰映射
    private BeanProcessor bean=new GenerousBeanProcessor();
    private RowProcessor processor=new BasicRowProcessor(bean);
    /**
     * 根据id找分类
     * @param id
     * @return
     */
    private Category findById(int id){
        String sql="select * from category where id=?";
        Category category=null;
        try {

            category=queryRunner.query(sql,new BeanHandler<>(Category.class,processor),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return category;
    }
    /**
     * 返回分类列表
     */
    private List<Category> list(){
        String sql="select * from category where id=?";
        List<Category> list=null;
        try {
            list=queryRunner.query(sql,new BeanListHandler<>(Category.class,processor));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
 }

通过上面的代码,主要解释,下面这个方法

category=queryRunner.query(sql,new BeanHandler<>(Category.class,processor),id);

首先query返回的是一个object对象,query的参数,第一个参数是sql语句,第二个new BeanHandler<>里面的两个参数,第一个是结果集查询后要映射到哪个类里面,第二个processor是设置驼峰映射,然后参数id是sql命令的参数,有些命令里面可以省略;
然后update的

public int save(User user) throws Exception {
//        String sql="insert into user (phone,pwd,sex,img,create_time,role,username,wechat) values(?,?,?,?,?,?,?,?)";
//        Object[] params={
//                    user.getPhone(),
//                    user.getPwd(),
//                    user.getSex(),
//                    user.getImg(),
//                    user.getCreateTime(),
//                    user.getRole(),
//                    user.getUsername(),
//                    user.getWechat()
//        };

        String sql = "delete from user where id= ?";
        Object [] params = {15};
        int i=0;
        try {
           i= queryRunner.update(sql,params);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
        return i;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值