1. 程式人生 > >關於SSM中的Controller傳遞引數的型別的問題

關於SSM中的Controller傳遞引數的型別的問題

 在SSM專案中如果你想利用mybatis進行搜尋結果的話,引數並不一定都要穿進去,才能查詢結果,有時候我想利用一個引數得到一個集合,或者利用另一個引數得到一個物件,那麼在宣告變數的時候最後利用物件的形式進行宣告變數,例如:

 @RequestMapping(value="/searchProduct")
@ResponseBody
public ServerResponse<PageInfo> searchProduct(HttpSession session,String productName,Integer productId,@RequestParam(value="pageNum",defaultValue="1") int pageNum,@RequestParam(value="pageSize",defaultValue="10") int pageSize){
//檢查使用者登入狀態
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"您還未登入,請先登入");
}
if(userServer.check_admin_role(user).isSuccess()){
return  productServer.searchProductByNameAndId(productName,productId,pageNum,pageSize);
}else{
return ServerResponse.createByErrorMessage("請以管理員身份登入");
}
 
}


第二個引數就就比較適宜用包裝型別將int型別 的引數包裝成Integer型別的引數

傳遞到mybatis mapper.xml可以有效的判斷其是否為空,如果是int型別的話回報空指標異常