(10条消息) 插入(Insert)SQL可动态设置字段属性

如图:合理运用动态sql,也可随意控制插入字段信息,代码中只对appVersion和userMobile赋值,

<insert id="insertDataPointInfo" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO data_point_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="itemId != null">
                item_id,
            </if>
            <if test="distinctId != null">
                distinct_id,
            </if>
            <if test="userId != null">
                user_id,
            </if>
            <if test="username != null">
                user_name,
            </if>
            <if test="userMobile != null">
                user_mobile,
            </if>
            <if test="os != null">
                os,
            </if>
            <if test="osVersion != null">
                os_version,
            </if>
            <if test="appVersion != null">
                app_version,
            </if>
            <if test="ip!= null">
                ip,
            </if>
            <if test="opTime!= null">
                op_time,
            </if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="itemId != null">
                #{itemId,jdbcType=INTEGER},
            </if>
            <if test="distinctId != null">
                #{distinctId,jdbcType=VARCHAR},
            </if>
            <if test="userId != null">
                #{userId,jdbcType=VARCHAR},
            </if>
            <if test="username != null">
                #{username,jdbcType=VARCHAR},
            </if>
            <if test="userMobile != null">
                #{userMobile,jdbcType=VARCHAR},
            </if>
            <if test="os != null">
                #{os,jdbcType=VARCHAR},
            </if>
            <if test="osVersion != null">
                #{osVersion,jdbcType=VARCHAR},
            </if>
            <if test="appVersion != null">
                #{appVersion,jdbcType=VARCHAR},
            </if>
            <if test="ip != null">
                #{ip,jdbcType=VARCHAR},
            </if>
            <if test="opTime != null">
                #{opTime},
            </if>
        </trim>
    </insert>
(0)

相关推荐