第2阶段31-MyBatis动态拼接sql

本文介绍了MyBatis中常用的动态SQL标签`if`和`foreach`的使用,以及`sql`标签用于代码复用的场景。`if`标签用于条件判断,例如在查询时根据条件过滤数据;`foreach`标签则在处理集合时非常有用,如在`WHERE`子句中构建`IN`条件;`sql`标签则可以定义SQL片段,提高代码可维护性。
摘要由CSDN通过智能技术生成

一、if标签

<select id="selectByMoney" resultType="demo.entity.User">
    select * from t_user
    <where>
        <if test="money != null and money != ''">
            and money>1000
        </if>
    </where>
</select>

二、foreach标签

<select id="selectUserByIds" resultType="demo.entity.User">
    select * from t_user
    <where>
        <foreach collection="ids" open="and id in(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </where>
</select>


三、sql标签

<sql id="sql_temp">
    select id ,user_name, passwd, money from t_user
</sql>
<select id="login" resultType="demo.entity.User">
    <include refid="sql_temp"></include> where user_name=#{userName} and passwd=#{passwd}
</select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值