MyBatis三---动态SQL

MyBatis 动态SQL

 MyBatis通过标签的配合使用,可实现如下功能

语句的动态拼接、前后缀格式处理、复杂参数处理

  动态SQL的常用标签

1、if

动态SQL技术中最常用的标签之一,类似于Java中的if语句

以下是语法:

<if  test = "条件判断,返回true或false" >
	SQL语句
</if>

                以下是示例:                        

<!-- 根据Id或者用户名称查询用户信息 -->
    <select id="getUserByIdAndName" resultType="com.cskt.mybatis2.pojo.SysUser">
        select * from t_sysUser where
        <if test="realName!= null and realName!=''">
            and u.realName like concat('%',#{realName},'%')
        </if>
        <if test="roleId!=null and roleId!=0">
            and u.roleId=#{roleId}
        </if>
    </select>

2、where

简化SQL语句中where子句处理,智能处理and、or等关键字

以下是语法:

<where>
	<if test="条件判断">
		SQL语句
	</if>
	…
</where>

                 以下是示例:

<!-- 根据供货商编码、供货商名称模糊查询供货商表 -->
    <select id="getSupperByIdAndSupName" resultType="com.cskt.mybatis2.pojo.Supplier">
        select id,supCode,supName,supContact,supPhone,supFax,createdTime from t_supplier
        <where>
            <if test="supCode!=null and supCode!=''">
                supCode like concat('%',#{supCode},'%')
            </if>
            <if test="supName!=null and supName!=''">
                and supName like concat('%',#{supName},'%')
            </if>
        </where>
    </select>

3、choose

是一个组合标签,通常与whenotherwise标签配合使用,类似于Javaswitch语句

以下是语法:

<choose>
	<when test="条件判断,返回true或false">
	</when>
	<when test="条件判断,返回true或false">
	</when>
	...
	<otherwise>
	</otherwise>
</choose>

                以下是示例:

<!-- 供货商表 -->
    <select id="getSupplierList" resultType="com.cskt.mybatis2.pojo.Supplier">
        select id,supCode,supName,supContact,supPhone,supFax,createdTime from t_supplier
        <where>
            <choose>
                <when test="supName!=null and supName!=''">
                    and supName like concat('%',#{supName},'%')
                </when>
                <when test="supCode!=null and supCode!=''">
                    and supCode like concat('%',#{supCode},'%')
                </when>
                <when test="supContact!=null and supContact!=''">
                    and supContact like concat('%',#{supContact},'%')
                </when>
                <when test="supName==null and supCode==null and supContact==null">
                    and createdTime =#{createdTime}
                </when>
            </choose>
        </where>
    </select>

 

4、foreach

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

以下是语法:

<foreach collection = "参数名称"   
               item = "元素别名" 
               open = "("
               separator = "," 
               close = ")" 
               index = "当前元素位置下标" >
	#{元素别名}
</foreach>

        

 

以下是示例(入参:数组、List、Map):

<!-- 查询多个供货商关联的入库单列表数据(以数组为参数实现) -->
    <select id="getStorageBySupplierIdArray" resultType="com.cskt.mybatis2.pojo.StorageRecord">
        select * from t_storageRecord where supplierId in
        <foreach collection="array" item="item" open="(" separator="," close= ")">
            #{item}
        </foreach>
    </select>

5、set

简化SQL语句中set子句处理,智能忽略更新语句尾部多出来的逗号

以下是语法:

<set>
	<if test="条件判断">
		SQL语句
	</if>
	…
</set>

               以下是示例:

<!-- 根据Id修改用户信息 -->
    <update id="getUpdateUser" parameterType="com.cskt.mybatis2.pojo.SysUser">
        update t_sysUSer
        <set>
            <if test="account!=null">account=#{account},</if>
            <if test="realName!=null">realName=#{realName},</if>
            <if test="sex!=null">sex=#{sex},</if>
            <if test="phone!=null">phone=#{phone},</if>
            <if test="age!=null">age=#{age},</if>
            <if test="roleId!=null">roleId=#{roleId},</if>
        </set>
        where id=#{id}
    </update>

             6、trim

动态地为SQL语句添加前后缀,智能忽略标签前后多余的andor或逗号等字符

                以下是语法:

<trim prefix = "前缀" 
	  suffix = "后缀" 
	  prefixOverrides = "忽略前缀" 
	  suffixOverrides = "忽略后缀" >
</trim>

                以下是示例: 

<!-- 根据id修改供货商表的供货商名称、描述、联系电话 -->
<update id="updateSupplier2" parameterType="com.cskt.mybatis2.pojo.Supplier">
        update t_supplier
        <trim prefix="set" suffix="where id=#{id}" suffixOverrides=",">
            <if test="supName !=null">
                supName=#{supName},
            </if>
            <if test="supDesc !=null">
                supDesc=#{supDesc},
            </if>
            <if test="supPhone !=null">
                supPhone=#{supPhone},
            </if>
        </trim>
    </update>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值