- 写法一
<update id="updateRole" parameterType="com.coship.web.uc.dto.RoleParam"> update t_role set <if test="name != null and name !=''"> name=#{name} </if> <if test="msg != null and msg !=''"> ,msg=#{msg} </if> <if test="type != null and type !=''"> ,type=#{type} </if> <if test="creator_id != null and creator_id !=''"> ,creator_id=#{creator_id} </if> <if test="level != null and level !=''"> ,level=#{level} </if> where id=#{id} </update>
- 写法二
<update id="updateRole" parameterType="com.coship.web.uc.dto.RoleParam"> update t_role set <if test="name != null and name !=''"> name=#{name}, </if> <if test="msg != null and msg !=''"> msg=#{msg}, </if> <if test="type != null and type !=''"> type=#{type}, </if> <if test="creator_id != null and creator_id !=''"> creator_id=#{creator_id}, </if> <if test="level != null and level !=''"> level=#{level} </if> where id=#{id} </update>
乍一看,握草,这他妈没啥区别啊。但是仔细观察后会发现,这两个mapper的细微区别:逗号的位置不同,第一种写法代码的健壮性比较强,而第二种写法当我只传部分属性例如只传name、msg、id这三个属性的时候sql将变成:update t_role set name="武大郎",msg="卖烧饼,被西门大官人绿", where id="1" 此时报sql语法错误,而第一种写法能巧妙避免该问题。
mybatis填坑之一:关于mapper中逗号位置的写法
猜你喜欢
转载自blog.csdn.net/zhaoliubao1/article/details/84580600
今日推荐
周排行