maven+Springboot——mapper层的sql执行失败,在service层里不能捕获到

sql语句本身出现错误,即bad sql
在serviceimpl中,

public void updateOrder (Integer id, String d, boolean isS)throws Exception {
System.out.println("here**************");

ParamMap param = new ParamMap();
param.seti(id);
param.setTd(d);
String id =null;
try{
id = mapper.selectID(param);
}catch(Exception e){
System.out.println("sql语句执行错误,错误信息 为:");
e.printStackTrace();
}
}

这样的话即使mapper中的sql语句出错,tryCatch也无法捕获到,依然会报错,也不会到catch块中执行,同时浏览器中也会报错。
而如果在service层中抛出这个异常,向上抛到controller中,在controller中进行trycatch捕获异常,此时可以打印出错误信息,浏览器也不会报错。
controller:

try{
            order_service.updateOrder(88,"uni076",true);
        }catch(Exception e){
            System.out.println("sql语句执行错误,错误信息 为:");
            e.printStackTrace();
        }

serviceimpl:

void updateOrder(Integer id, String d, boolean isS)throws Exception{}

注意:因为是针对这个方法抛出的异常,如果这之中有很多的sql语句都出现了错误,那么他只能抛出异常后得到第一个sql语句的异常信息
因为在sql异常后的代码都不会执行了,而在这个serviceimpl的一个方法里,可能涉及到很多的mapper方法,throws Exception 是写在这个方法上的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章