mybatis trim 使用

时间:2021-09-08
本文章向大家介绍mybatis trim 使用,主要包括mybatis trim 使用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

trim可以去除sql语句中多余的and关键字,逗号,或者给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀,可用于选择性插入、更新、删除或者条件查询等操作。

    <update id="update" parameterType="Product">
        update
        <include refid="tableName"></include>
        <trim prefix="set" suffixOverrides=",">
            <if test="product_name!=null and product_name != ''">product_name = #{product_name},</if>
            <if test="product_code!=null and product_code != ''">product_code = #{product_code},</if>
        </trim>
        where product_id = #{product_id}
    </update>
    
    <insert id="add" parameterType="Product">
        insert into <include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="product_id != null and product_id !=''">product_id,</if>
            <if test="product_name != null and product_name !=''">product_name, </if>
            <if test="product_code != null and product_code !=''">product_code, </if>

        </trim>
        <trim prefix="values(" suffix=")" suffixOverrides=",">
            <if test="product_id != null and product_id !=''">#{product_id},</if>
            <if test="product_name != null and product_name !=''">#{product_name},</if>
            <if test="product_code != null and product_code !=''">#{product_code}, </if>
        </trim>
    </insert>

原文地址:https://www.cnblogs.com/xuchao0506/p/15243216.html