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