Mybatis高级

动态SQL

if

<if test="条件">
    sql 语句
</if>
当条件成立的时候,会执行sql语句
if (条件){
	sql 语句
}

choose…when…otherwise…

<choose>
    <when test="条件1"> 
    	sql语句1
    </when>
    
    <when test="条件2"> 
    	sql语句2
    </when>
    
    <otherwise>
        sql语句3
    </otherwise>
</choose>

和我们java的if...else if ...else格式一样
当条件1成立,那么就不会执行后面的代码

where

<select id="queryUserByCondition" resultType="com.model.User" parameterType="com.model.User">
    select id,username,password,age,phone from user
    <where>
        <if test="age!=null and age!=''">
            and age=#{age}
        </if>
        <if test="phone!=null and phone!=''">
            and phone=#{phone}
        </if>
    </where>
</select>

set

<update id="updateUser" parameterType="com.model.User">
    update user
    <set>
        <if test="username!=null and username!=''">username=#{username},</if>
        <if test="password!=null and password!=''">password=#{password},</if>
    </set>
    where id=#{id}
</update>

foreach


<select id="queryUsersByIds" resultType="com.model.User"
        parameterType="java.lang.Integer">
    select id,username,password,age,phone from user
    <where>
        <foreach collection="list" item="id" open="id in (" close=")" separator=",">
            #{id}
        </foreach>
    </where>
</select>

trim

替换where
<select id="queryUserByCondition" resultType="com.model.User" parameterType="com.model.User">
    select id,username,password,age,phone from user
    <trim prefix="where" prefixOverrides="and">
        <if test="age!=null and age!=''">
            and age=#{age}
        </if>
        <if test="phone!=null and phone!=''">
            and phone=#{phone}
        </if>
    </trim>
</select>


替换set
<update id="updateUser" parameterType="com.model.User">
    update user
    <trim prefix="set" suffixOverrides=",">
        <if test="password!=null and password!=''">
            password=#{password},
        </if>
        <if test="age!=null and age!=''">
            age=#{age},
        </if>
    </trim>
    where id=#{id}
</update>

Sql片段

<sql id="别名">
    查询的所有字段
</sql>

使用的时候 <include refid="别名"/>
MyBatis中,高级查询可以使用延迟加载机制来实现。延迟加载是将数据加载时机推迟,例如推迟嵌套查询的执行时机。通过延迟加载,可以先查询主表,按需实时做关联查询,返回关联表结果集,提高效率。 在MyBatis中,实现关联查询有两种不同的方式:嵌套Select查询和嵌套结果映射。嵌套Select查询是通过执行另外一个SQL映射语句来加载期望的复杂类型,而嵌套结果映射则使用嵌套的结果映射来处理连接结果的重复子集。 对于关联结果映射和其他类型的映射,工作方式类似。需要指定目标属性名以及属性的javaType,大多数情况下MyBatis可以推断出来,如果需要的话,还可以设置JDBC类型。如果想要覆盖获取结果值的过程,可以设置类型处理器。 综上所述,通过延迟加载机制和适当的关联查询方式,MyBatis可以实现高级查询功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Mybatis高级查询](https://blog.csdn.net/weixin_37650458/article/details/96587906)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [MyBatis高级查询](https://blog.csdn.net/qq_66991094/article/details/127147576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值