MyBatis总结

Mybatis动态SQL实现

Mybatis提供了动态SQL,也就是可以根据用户提供的参数,动态决定查询语句依赖的查询条件或SQL语句的内容

动态SQL标签

  1. if和where

这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

<!--动态Sql : where / if-->
<select id="selectAll"  resultType="com.whz.pojo.User">
     select <include refid="all"/> from users
       <where>
          <if test="id != null and id != 0">
              AND id = #{id}
           </if>
           <if test="name != null and name != ''">
             AND name = #{name}
          </if>
           <if test="money!= null and money != ''">
               AND money= #{money}
           </if>
       </where>
  </select>

  1. choose、when、otherwise 标签(类似于 Java 中的 switch、case、default)

有三个条件,id,username,sex,只能选择一个作为查询条件
如果 id 不为空,那么查询语句为:select * from user where id=?
如果 id 为空,那么看username 是否为空,如果不为空,那么语
句为 select * from user where username=?;
如果 username 为空,那么查询语句为 select * from user where
sex=?

<!--动态Sql: choose、when、otherwise 标签-->
   <select id="selectAll2" resultType="com.whz.pojo.User">
      select * from users
     <where>
           <choose>
               <when test="name != null and name != ''">
                AND name = #{name}
               </when>
               <when test="money != null and money!= ''">
                   AND money= #{money}
               </when>
               <otherwise>
                   AND id = #{id}
               </otherwise>
           </choose>
       </where>
   </select>

  1. set 标签(if+set)

如果第一个条件 username 为空,那么 sql 语句为:update user u set u.sex=? where id=?
如果第一个条件不为空,那么 sql 语句为:update user u set u.username = ? ,u.sex = ? where id=?

<!--动态Sql: set 标签-->
   <update id="updateSet" parameterType="com.whz.pojo.User">
       update users
       <set>
           <if test="name != null and name != ''">
               name = #{name},
           </if>
           <if test="money!= null and money!= ''">
               money= #{money},
           </if>
       </set>
       where id = #{id}
   </update>

  1. foreach标签

select * from user where id=1 or id=2 or id=3

<select id="selectUserByListId" parameterType="com.whz.pojo.User" >resultType="com.whz.pojo.User">
   select * from user
   <where>
       <!--
           collection:指定输入对象中的集合属性
           item:每次遍历生成的对象
           open:开始遍历时的拼接字符串
           close:结束时拼接的字符串
           separator:遍历对象之间需要拼接的字符串
           select * from user where 1=1 and (id=1 or id=2 or id=3)
         -->
       <foreach collection="ids" item="id" open="and (" close=")" >separator="or">
           id=#{id}
       </foreach>
   </where>
</select>


select * from user where id in (1,2,3)

<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" >resultType="com.ys.po.User">
       select * from user
       <where>
           <!--
               collection:指定输入对象中的集合属性
               item:每次遍历生成的对象
               open:开始遍历时的拼接字符串
               close:结束时拼接的字符串
               separator:遍历对象之间需要拼接的字符串
               select * from user where 1=1 and id in (1,2,3)
             -->
           <foreach collection="ids" item="id" open="and id in (" close=") " >separator=",">
               #{id}
           </foreach>
       </where>
   </select>

Mybatis的分页

方法一:

UserMapper.java(interface)
在这里插入图片描述

userMapper.xml
在这里插入图片描述
Test.java
在这里插入图片描述

方法二:

UserMapp.java(interface)
在这里插入图片描述
UserMapper.xml
在这里插入图片描述
Test.java
在这里插入图片描述

方法三:

UserMapper.java(interface)
在这里插入图片描述
UserMapper.xml
在这里插入图片描述
Test.java
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值