首页 > 数据库技术 > 详细

mybatis_05动态SQL_if和where

时间:2019-03-03 21:19:09      阅读:152      评论:0      收藏:0      [点我收藏+]

 

  • If标签:作为判断入参来使用的,如果符合条件,则把if标签体内的SQL拼接上。

   注意:用if进行判断是否为空时,不仅要判断null,也要判断空字符串‘’;

  • Where标签:会去掉条件中的第一个and符号。

 

通过if和where通过判断可以选择那些语句来执行,那些语句不执行,生成最终SQL语句

 

在第一个底层if判断中,SQL语句前面加上and也可以,系统会自动去掉

<resultMap id="userByresultmap" type="user">
    <id property="id" column="id_"></id>
    <result property="username" column="username_"></result>
    <result property="birthday" column="birthday_"></result>
    <result property="sex" column="sex_"></result>
    <result property="address" column="address_"></result>
</resultMap>

<select id="findUserByifwhere" parameterType="userQueryVO" resultMap="userByresultmap">

  select * from USER

  <where>
      <if test="user!=null and user!=‘‘">

          <if test="user.sex!=null and user.sex!=‘‘">
<!—也可以写成and sex=#{user.sex},系统会自动去掉-->
              sex=#{user.sex}
          </if>
          <if test="user.username!=null and user.username!=‘‘">
              and username LIKE "%${user.username}%"
          </if>
      </if>

  </where>

  </select>

 

mybatis_05动态SQL_if和where

原文:https://www.cnblogs.com/aihuadung/p/10467550.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!