MyBatis系列学习---《Mapper.xml 解析 三 - where标签 02》

关于where标签中and的位置问题

  • 正确格式
<select id="selectUserBySexAndUserName" resultMap="UserExample" parameterType="com.huarenwenyu.client.dao.UserExample">
  select * from user
  <where>
    <if test="sex != null and sex != ''">
      sex = #{sex} and
    </if>
    <if test="username != null and username != ''">
      username = #{username}
    </if>
  </where>
</select>
  • 错误格式
<select id="selectUserBySexAndUserName" resultMap="UserExample" parameterType="com.huarenwenyu.client.dao.UserExample">
  select * from user
  <where>
    <if test="sex != null and sex != ''">
      sex = #{sex}
    </if>
    <if test="username != null and username != ''">
      and username = #{username}
    </if>
  </where>
</select>

报错:###SQL: select*from user WHERE sex = ? and

WHERE标签去默认会去掉第一个前and。

发布了63 篇原创文章 · 获赞 1 · 访问量 2778

猜你喜欢

转载自blog.csdn.net/A_bad_horse/article/details/104965915