Mysql动态查询

if条件查询

格式: <if test=”条件判断”> 添加到sql的语句 </if>

where标签

简化SQL语句中WHERE条件判断

智能处理and和or

如果使用几个if条件标签,如果第一个条件不成立,那么该sql语句就不成立了.

把所有的if条件语句加入到where标签内,则会根据if语句成立是否添加where条件,若标签返回的内容是以and或者or开头的,会自动剔除.

案例:

<!-- 分类后根据关键字进行搜索 -->

   <select id="getUserListByClassify" resultMap="userList">

         SELECT user.*,role.roleName

         FROM smbms_user user

         INNER JOIN smbms_role role ON user.userRole=role.id

         <where>

            <if test="userName != null">

              AND   userName like CONCAT('%',#{userName},'%')

           </if>

           <if test="userRole != null">

              AND userRole=#{userRole}

           </if>

         </where>

</select>

trim标签

属性:

prefix:前缀 如果有返回值,在标签前加的语句

suffix:后缀 如果有返回值,在标签后加的语句

prefixOverrides:对于trim包含内容的首部进行指定内容的忽略

sufixOverrides:对于trim包含内容的尾部进行指定内容的忽略

可以去除多余的关键字(自定义)   where只能去除多余的and/or

<!-- 分类后根据关键字进行搜索 -->

   <select id="getUserListByClassify" resultMap="userList">

         SELECT user.*,role.roleName

         FROM smbms_user user

         INNER JOIN smbms_role role ON user.userRole=role.id

         <trim prefix="where" prefixOverrides="and | or">

           <if test="userName != null">

              userName like CONCAT('%',#{userName},'%')

           </if>

           <if test="userRole != null">

              and userRole=#{userRole}

           </if>

         </trim>

</select>

if+set进行修改(在实际开发中很少使用)

如何只对有值得参数进行修改,而不必每次都要获取整个对象.

案例:

<!-- 更新用户属性 -->

      <update id="update" parameterType="User">

         UPDATE smbms_user

         <set>

           <if test="userCode != null">userCode=#{userCode},</if>

            <if test="userName!= null">userName=#{userName},</if>

           <if test="userPassword!=          null">userPassword=#{userPassword},</if>

           <if test="gender!= null">gender=#{gender},</if>

           <if test="birthday!= null">birthday=#{birthday},</if>

           <if test="phone!= null">phone=#{phone},</if>

           <if test="address!= null">address=#{address},</if>

           <if test="modifyDate!= null">modifyDate=#{modifyDate}</if>

         </set>

         where id=#{id}

</update>

if+trim进行修改

添加条件判断修改

<!-- 更新用户属性 -->

      <update id="update" parameterType="User">

         UPDATE smbms_user

         <trim prefix="set" prefixOverrides="," resultMap="userList">

           <if test="userCode != null">userCode=#{userCode},</if>

            <if test="userCode != null">userName=#{userName},</if>

           <if test="userCode !=          null">userPassword=#{userPassword},</if>

           <if test="userCode != null">gender=#{gender},</if>

           <if test="userCode != null">birthday=#{birthday},</if>

           <if test="userCode != null">phone=#{phone},</if>

           <if test="userCode != null">address=#{address},</if>

           <if test="userCode != null">modifyDate=#{modifyDate}</if>

         </trim>

         where id=#{id}

</update>

使用foreach进行复杂查询

迭代一个集合,通常用于in条件

属性:

item:表示集合中每一个元素进行迭代时候的别名

index:指定一个名称,表示迭代过程中,每次迭代到的位置.(可省略...)

open:表示语句以什么开始 例:open=”(”

separator:表示每次进行迭代之间以什么作为分隔符.

close:表示该语句以什么结束.

collection:最关键并最容易出错的属性,该属性必须指定,不同情况下,该属性的值也不一样,主要分三种:

  1. 若传入的参数为单参数且参数类型是一个List的时候,collection属性值为list.
  2. 若入参为单参数且参数类型为数组的时候,collection属性值为array
  3. 若传入的参数为多参数,就需要把他们封装为一个Map处理,属性值为Map的key.

案例:使用foreach实现 全选 删除

<!-- 删除用户 -->

   <delete id="delete" parameterType="map">

      DELETE FROM smbms_user WHERE id IN

<!-- 传入的参数为map集合时 collection=为map的key -->

      <foreach collection="id" item="userId" open="(" separator="," close=")">

        #{userId}

      </foreach>

      <!-- 传入的参数为数组时候  collection="array" -->

      <foreach collection="array" item="userId" open="(" separator="," close=")">

        #{userId}

      </foreach>

      <!-- 传入的参数为List时候  collection="list" -->

      <foreach collection="list" item="userId" open="(" separator="," close=")">

        #{userId}

      </foreach>

   </delete>

 

 

choose(when,otherwise)

类似于java的switch --case语句,跟JSTL的choose使用方式一样.

when元素:当test属性中条件满足的时候.就会输出when元素的内容.跟java中switch一样,依次向下执行,当有一个when中的test条件成立后,执行该语句立马跳出choose,所以choose中只有一个条件会输出.

 

MyBatis分页

获取数据的页数

<!-- 获取数据的总记录数 -->

   <select id="getAllPages" resultType="Integer">

       SELECT COUNT(`id`) AS pages FROM smbms_user

       <trim prefix="where" prefixOverrides="and | or">

            <if test="userRole != null">

            userRole=#{userRole};

            </if>

       </trim>

</select>

 注:if或when标签是针对JAVABEAN或者MAP的,进行判断的参数要么使用注解进行封装,或传入对象,或添加进map在进行传参数.

分页

<!-- 查询所有用户 并进行分页 -->

   <select id="getAllUser" resultType="User">

         SELECT user.*,role.roleName

         FROM smbms_user user

         INNER JOIN smbms_role role ON user.userRole=role.id

         ORDER BY creationDate DESC LIMIT #{from},#{pageSize}

   </select>

获取最后一条插入数据的id(自增主键)

 

posted on 2019-05-05 17:10  听风醉  阅读(3216)  评论(0编辑  收藏  举报

导航

因为不可能,所以才值得相信。