首页 > 其他 > 详细

Mybatis_03_动态语句

时间:2019-07-01 12:14:37      阅读:108      评论:0      收藏:0      [点我收藏+]

1.if标签

test里面的userName是实体类的属性。

    <!--根据条件查询-->
    <select id="findUserByConditon" resultMap="userMap" parameterType="user">
        select * from user where 1=1
        <if test="userName!=null">
          and username=#{userName}
        </if>
        <if test="sex!=null">
            and sex=#{sex}
        </if>

    </select>

 

2.where标签  不用谢where 1=1

    <!--根据条件查询-->
    <select id="findUserByConditon" resultMap="userMap" parameterType="user">
        select * from user
       <where>
            <if test="userName!=null">
              and username=#{userName}
            </if>
            <if test="sex!=null">
                and sex=#{sex}
            </if>
           </where>
    </select>

 

3.foreach标签

open一个(  close 一个)  item是每一项  separator使用 ‘,‘ 分隔。

 <!--根据queryVo中的id集合查询用户列表-->
    <select id="findUserInIds" resultMap="userMap" parameterType="queryVo">
      select * from user
      <where>
        <if test="ids!=null and ids.size()>0">
            <foreach collection="ids" open="and id in (" close=")" item="uid" separator=",">
               #{uid}
            </foreach>
        </if>

      </where>

    </select>
    

 

Mybatis_03_动态语句

原文:https://www.cnblogs.com/cmdzhizhu/p/11113234.html

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