mybatis动态SQl语句

choose语句

<select id="find" parameterType="map" resultType="User">
        <!--name为空时,不加条件-->
        select * from t_user
         <if test="name!=null">
             where
            ( username like #{name} or nickname like #{name})
         </if>
          <if test="sort!=null">
             order by ${sort}

             <choose>
                 <when test="order!=null"> ${order}</when>
                 <otherwise>asc</otherwise>
             </choose>
         </if>
           
        limit #{pageOffset},#{pageSize}
    
    </select>

<!-- 多个参数的形式,需要使用map来存储-->
    <!-- 只要是#{}这种形式的,都会以?形式替代,对应字符串形式,都会加上引号-->
    <!-- 只要是${}这种形式的,不会用?形式替代,传过来什么,就什么替代-->
    <select id="find" parameterType="map" resultType="User">
        <!--name为空时,不加条件-->
        select * from t_user
         <if test="name!=null">
             where
            ( username like #{name} or nickname like #{name})
         </if>
        
            order by ${sort} ${order}
        limit #{pageOffset},#{pageSize}

    </select>
    <select id="count" parameterType="map" resultType="int">
        select count(*) from t_user
         <if test="name!=null">
             where
            ( username like #{name} or nickname like #{name})
         </if>
       
    </select>

where语句

<select id="find" parameterType="map" resultType="User">

        select * from t_user
        <where>
            <if test="name!=null">
             <!--第一个条件不加and,后面几个都加-->
            ( username like #{name} or nickname like #{name})
            </if>
            <if test="type!=null">
            
            and type=#{type}
         </if>
        </where>
        

        limit #{pageOffset},#{pageSize}
       
        <!--
        对应的sql便是
        select * from t_user where (username like ? or nickname like ?)
            order by id desc limit ? ?
        -->
    </select>

 

mybatis动态SQl语句,布布扣,bubuko.com

mybatis动态SQl语句

上一篇:SQL扫描并执行文件夹里的sql脚本


下一篇:c#自带压缩类实现数据库表导出到CSV压缩文件的方法