Mybatis通过on duplicate key update实现批量插入或更新


批量的saveOrupdate:

  1. 使用要点:

    (1) 表要求必须有主键或唯一索引才能起效果,否则insert或update无效;

    (2)  注意语法on duplicate key update后面应为需要更新字段,不需要更新的字段不用罗列;

    (3) 相较于replace into(insert加强版,不存在时insert,存在时先delete后insert)虽然也能达到批量更新目的,但因为删除和添加需要重复维护索引,所以大批量比on duplicate key update性能要差,小量可忽略,自选为

    单条的saveOrupdate:


    根据相应的唯一主键或者唯一索引来判断是否新增或更新  [对事务支持较好]

INSERT INTO 
user(userid,sex,age) 
VALUES('oCCtTxOz28457LUISKyOq4r94DYE','男',18) 
ON DUPLICATE KEY UPDATE sex=VALUES(sex),age=VALUES(age)

ON DUPLICATE KEY UPDATE 附带更新条件

1.  单条语句--不为空进行更新

在update后面要判断参数是否为空,如果不为空进行更新,为空的就不进行更新了

insert into sys_hk_goods
	(
	`sku_code`,
	`product_code`,
	`sku_name`,
	`uom`,
	`wrapping`,
	`weight`,
	`length`,
	`width`,
	`height`,
	`logistics_package`,
	`package_material`,
	`picture_url`
	)
	values
	(
	#{skuCode},
	#{productCode},
	#{skuName},
	#{uom},
	#{wrapping},
	#{weight},
	#{length},
	#{width},
	#{height},
	#{logisticsPackage},
	#{packageMaterial},
	#{pictureUrl}
	)
	ON DUPLICATE KEY UPDATE
	<trim prefix="" suffixOverrides=",">
		<if test="productCode != null">`product_code` = #{productCode}, </if>
		<if test="skuName != null">`sku_name` = #{skuName}, </if>
		<if test="uom != null">`uom` = #{uom}, </if>
		<if test="wrapping != null">`wrapping` = #{wrapping}, </if>
		<if test="weight != null">`weight` = #{weight}, </if>
		<if test="length != null">`length` = #{length}, </if>
		<if test="width != null">`width` = #{width}, </if>
		<if test="height != null">`height` = #{height}, </if>
		<if test="logisticsPackage != null">`logistics_package` = #{logisticsPackage}, </if>
		<if test="packageMaterial != null">`package_material` = #{packageMaterial}, </if>
		<if test="pictureUrl != null">`picture_url` = #{pictureUrl}</if>
	</trim>
</insert>

之所以使用了trim, 是因为当最后一个pictureUrl为空的时候,会报异常,因为不走最后一条数据了,多了一个”,”,

处理方式可以用mybatis中的trim进行判断

2.  批量操作--不为空进行更新

<!--批量插入or更新-->
<insert id="batchSaveOrUpdateSimulatorInfo">
    insert into dm_simulator_info
    (
    simulator_id, simulator_name,
    simulator_state,simulator_type,
    simcontorler_name, simcontorler_id,
    create_time,update_time
    )
    values
    <foreach collection="list" item="item" separator=",">
        (
        #{item.simulatorId,jdbcType=VARCHAR}, #{item.simulatorName,jdbcType=VARCHAR},
        #{item.simulatorState,jdbcType=INTEGER},#{item.simulatorType,jdbcType=INTEGER},
        #{item.simcontorlerName,jdbcType=VARCHAR}, #{item.simcontorlerId,jdbcType=VARCHAR},
        NOW(),NOW()
        )
    </foreach>
    on duplicate key update
    <trim prefix="" suffixOverrides=",">
        <foreach collection="list" item="item" separator=",">
            <if test="item.simulatorName != null">
                simulator_name = #{item.simulatorName,jdbcType=VARCHAR},
            </if>
            <if test="item.simulatorType != null">
                simulator_type = #{item.simulatorType,jdbcType=VARCHAR},
            </if>
            <if test="item.simulatorState != null">
                simulator_state = #{item.simulatorState,jdbcType=INTEGER},
            </if>
            <if test="item.simcontorlerName != null">
                simcontorler_name = #{item.simcontorlerName,jdbcType=VARCHAR},
            </if>
            <if test="item.simcontorlerId != null">
                simcontorler_id = #{item.simcontorlerId,jdbcType=VARCHAR},
            </if>
            update_time =NOW()
        </foreach>
    </trim>
</insert>

因为是批量, 所以ON DUPLICATE KEY UPDATE 附带的更新条件要使用foreach,  打印出来的SQL如下

INSERT INTO dm_simulator_info (
	simulator_id,
	simulator_name,
	simulator_state,
	simulator_type,
	simcontorler_name,
	simcontorler_id,
	create_time,
	update_time
)
VALUES
	(?, ?, ?,?, ?, ?, NOW(), NOW()),
	(?, ?, ?,?, ?, ?, NOW(), NOW()),
	(?, ?, ?,?, ?, ?, NOW(), NOW()),
	(?, ?, ?,?, ?, ?, NOW(), NOW()),
	(?, ?, ?,?, ?, ?, NOW(), NOW()),
	(?, ?, ?,?, ?, ?, NOW(), NOW()) 
ON DUPLICATE KEY UPDATE 
	simulator_name = ?, simulator_state = ?, simcontorler_name = ?, simcontorler_id = ?, update_time = NOW(),
	simulator_name = ?, simulator_state = ?, simcontorler_name = ?, simcontorler_id = ?, update_time = NOW(),
	simulator_name = ?, simulator_state = ?, simcontorler_name = ?, simcontorler_id = ?, update_time = NOW(),
	simulator_name = ?, simulator_state = ?, simcontorler_name = ?, simcontorler_id = ?, update_time = NOW(),
	simulator_name = ?, simulator_state = ?, simcontorler_name = ?, simcontorler_id = ?, update_time = NOW(),
	simulator_name = ?, simulator_state = ?, simcontorler_name = ?, simcontorler_id = ?, update_time = NOW()

3.  insert ... on duplicate key update column=IF(条件,值1,值2 )

需求:  更新操作,先将columnB更新为新值,然后根据if条件(columnB更新后的值)做判断更新columnA

INSERT INTO tbl (columnA,columnB,columnC) VALUES (1,2,3) ON DUPLICATE KEY UPDATE columnA=IF(columnB>0,1,columnA)

IF 函数用法--类似JAVA中的三元表达式

语法: IF(expr1,expr2,expr3)

函数用法说明:如果 expr1 是 TRUE (expr1 <> 0 and expr1 <> NULL) ,则 IF() 的返回值为 expr2 ; 否则返回值则为 expr3 。 IF() 的返回值为数字值或字符串值,具体情况视其所在语境而定

INSERT INTO dm_simulator_info (
	simulator_id,
	simulator_name,
	simulator_state,
	simulator_type,
	simcontorler_name,
	simcontorler_id,
	create_time,
	update_time
)
VALUES
	("2222", "wwwwww", 2,"2222", "2222", "2222", NOW(), NOW())
ON DUPLICATE KEY UPDATE 
	simulator_name = "wwwwww", simulator_state = 2, simcontorler_name = IF(simulator_state>0,"FFFFF","YYYY")

你可能感兴趣的:(数据库与mybatis,mybatis)