SpringBoot整合Mybatis註解版---update出現org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0,
阿新 • • 發佈:2018-11-10
SpringBoot整合Mybatis註解版---update時出現的問題
問題描述:
1、sql建表語句
DROP TABLE IF EXISTS `department`; CREATE TABLE `department` ( `id` int(11) NOT NULL AUTO_INCREMENT, `departmentName` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、Repository層使用註解方式,
語句如下:
@Update("UPDATE department SET departmentName=#{department.departmentName} WHERE id = #{id}") public Integer updateEmp(Integer id, Department department);
顯示錯誤資訊:
org.apache.ibatis.binding.BindingException: Parameter 'department' not found. Available parameters are [arg1, arg0, param1, param2]
由錯誤資訊得知,引數發現不了,或者解析不了,可以用 arg1, arg0, param1, param2替換,假如我們不是用POJO物件Department進行傳參,
而是使用單個欄位分別進行傳值,那麼結果。
@Update("UPDATE department SET departmentName=#{param2} WHERE id = #{param1}") public Integer updateEmp( Integer id, String departmentName);
但是隨著欄位增多,我們使用POJO物件Department進行傳參,需要在傳過來的引數上添加註解,起別名的方式,獲得引數,sql語句中通過物件名.屬性名方式
獲得引數的值,結果也是成功。
更改後得語句:
controller: @PostMapping("/dept/{id}") public Integer updateEmp(@PathVariable("id") Integer id,@Param("departmentName") Department department) { return departmentService.updateEmp(id,departmentName); } ------------------------- service: public Integer updateEmp(Integer id,Department department) {
Department dep = new Department(id,department); return departmentMapper.updateEmp(id,dep); } ------------------------- repository: @Mapper public interface DepartmentMapper { @Update("UPDATE department SET departmentName=#{department.departmentName} WHERE id = #{id}") public Integer updateEmp(@Param("id") Integer id,@Param("department") Department department); }