问题复现
xml片段
<!--批量修改-->
<update id="updateBatch" parameterType="list">
<foreach collection="list" item="item" index="index" separator=",">
update item_category
<set>
<if test="item.userName!=null">
userName=#{item.userName},
</if>
<if test="item.passWord!=null">
passWord=#{item.passWord}
</if>
<if test="realName!=null">
realName=#{item.realName}
</if>
</set>
<where>
<if test="item.id!=null">
id=#{item.id}
</if>
</where>
</foreach>
</update>
<!--通过id进行修改-->
<update id="updateById" parameterType="com.cheng.po.ItemCategory">
update item_category
<set>
<include refid="ItemCategory_update"/>
</set>
where id=#{id}
</update>
c层
/**
* 转向到修改一级分类页面
*/
@RequestMapping("/update")
public String update(Integer id,Model model){
ItemCategory obj = itemCategoryService.load(id);
model.addAttribute("obj",obj);
return "/itemCategory/update";
}
/**
* 修改一级分类
*/
@RequestMapping("/exUpdate")
public String exUpdate(ItemCategory itemCategory){
itemCategoryService.updateById(itemCategory);
return "redirect:/itemCategory/findBySql";
}
前端页面
<input type="text" class="input w50" name="userName" data-validate="required:请输入名称" value="${obj.name}" />
此时input标签的name应该为**name=“userName”**但是我写成了userName,所以在Controller里面是接收到的值是"",为空,但是在动态SQL里只判断了!=null,所以执行的时候一直报sql语法错误。
当时找了很久有没有发现错误是什么,console打印的语句是‘near where id=xx’;
后面一句一句排除后才发现是前端的name值没有写对。
总结
- 前端页面的参数一定要写对,不然在controller接收到的数据是空的,不是null
- 动态sql应该加上 filed!=null and filed != ‘’