Java学习碎碎念1

Java学习心得:Dbutils

学习碎碎念

最近几天学习了Dbutils,老师说是JDBC向Mybatis的过度阶段,在学完这部分后对于sql语句部分有了一些思考,如果能把java代码中的sql语句单独抽离出来就好了,不要直接放在java代码里面写,因为当需要改变sql语句时需要改变java代码,不符合开闭原则


this.queryRunner.query(sql, new BeanListHandler<>(StudentVO.class));
sql代表sql语句,BeanListHandler返回BeanList(StudentVO.class)
BeanHandler返回一个bean
this.queryRunner.update(sql,object);
object对象,里面是sql语句问号对应得内容
  @Override
    public int deleteStudent(int stuid) {
        //多表删除重新写一个QueryRunner对象,无参构造方法
        QueryRunner queryRunner1 = new QueryRunner();
        Connection connection = null;
        try {
            connection = DruidUtils.getDataSource().getConnection();

            //关闭自动提交
            connection.setAutoCommit(false);
            //设置隔离级别
            connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

            String delete_sh_ref_sql = "delete from sh_ref where stuid=?";
            String delete_score_sql = "delete from score where stuid=?";
            String delete_stu_sql = "delete from student where stuid=?";

            queryRunner1.update(connection,delete_sh_ref_sql,stuid);
            queryRunner1.update(connection,delete_score_sql,stuid);
            queryRunner1.update(connection,delete_stu_sql,stuid);

            connection.commit();
        } catch (SQLException throwables) {
            if(connection!=null){
                try {
                    //设置回滚
                    connection.rollback();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            throwables.printStackTrace();
        }
        String sql = "delete from student s, sh_ref sh where ";
        return 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值