1. 数据批量插入
数据库连接需加:&allowMultiQueries=true
int batchInsert(@Param("datas")Collection<AdmDevTypeSerial> savedata);
<insert id="batchInsert" useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
INSERT INTO `dss`.`adm_dev_type_serial`( `DEVICE_CATEGORY`, `DEVICE_TYPE`, `DEVICE_SN`, `UPDATE_TIME`, `CREATE_TIME`, `EXPIRE_TIME`) VALUES
<foreach item="item" collection="datas" separator="," open="" close="" index="">
( #{item.deviceCategory }, #{item.deviceType }, #{item.deviceSN }, now (), now (), #{item.expireTime })
</foreach>
</insert>
<insert id="insertUserBatch">
<foreach collection ="list" item="item" separator =";">
INSERT INTO user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="item.id != null and item.id != ''">id,</if>
<if test="item.username != null and item.username != ''">username,</if>
<if test="item.password != null and item.password != ''">password,</if>
<if test="item.sex != null and item.sex != ''">sex,</if>
<if test="item.age != null">age,</if>
<if test="item.address != null and item.address != ''">address,</if>
<if test="item.createTime != null">create_time,</if>
</trim>
VALUES
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="item.id != null and item.id != ''">#{item.id},</if>
<if test="item.username != null and item.username != ''">#{item.username},</if>
<if test="item.password != null and item.password != ''">#{item.password},</if>
<if test="item.sex != null and item.sex != ''">#{item.sex},</if>
<if test="item.age != null">#{item.age},</if>
<if test="item.address != null and item.address != ''">#{item.address},</if>
<if test="item.createTime != null">#{item.createTime},</if>
</trim>
</foreach>
</insert>
<insert id="batchInsert" useGeneratedKeys="true" keyProperty="id">
INSERT INTO `evo_custom`.`smart_operation_enterpirse_service`(`SMART_OPERATION_ID`, `YQAPP_CODE`, `CODE`,`NAME`, `PARENT_ITEM_CODE`,`PARENT_ITEM_NAME`,`VALUEADD_PROVIDER_LIST`,`CREATE_DATETIME`, `CREATE_USER_ID`, `UPDATE_TIME`, `UPDATE_USER_ID`)
VALUES
<foreach item="item" collection="insertList" separator="," open="" close="" index="">
( #{item.smartOperationId,jdbcType=BIGINT },#{item.yqAppCode,jdbcType=VARCHAR },#{item.code,jdbcType=VARCHAR }, #{item.name,jdbcType=VARCHAR },#{item.parentItemCode,jdbcType=VARCHAR },#{item.parentItemName,jdbcType=VARCHAR }, #{item.valueaddProviderList, jdbcType=OTHER,typeHandler=com.dahua.evo.custom.handler.MySqlJsonHandler}, now (), now (), #{item.createUserId }, #{item.updateUserId })
</foreach>
</insert>
2. 数据批量查询
<!--查询sql(批量) -->
<select id="getClassByIdList" resultMap="BaseResultMap">
SELECT * FROM inspect_classes where ID IN
<if test="classIdList != null and classIdList.size() > 0">
<foreach item="id" collection="classIdList" open="(" separator="," close=")">
#{id}
</foreach>
</if>
</select>
3.批量更新
<update id="updateAccessControlChannel" parameterType="AccessControlChannel" >
update acs_access_control_chn
<set >
<if test="deviceCode != null" >
DEVICE_CODE = #{deviceCode,jdbcType=VARCHAR},
</if>
<if test="channelCode != null" >
CHANNEL_CODE = #{channelCode,jdbcType=VARCHAR},
</if>
<if test="channelName != null" >
CHANNEL_NAME = #{channelName,jdbcType=VARCHAR},
</if>
<if test="channelSeq != null" >
CHANNEL_SEQ = #{channelSeq,jdbcType=BIGINT},
</if>
<if test="validFlag != null" >
VALID_FLAG = #{validFlag,jdbcType=INTEGER},
</if>
<if test="orgCode != null" >
ORG_CODE = #{orgCode,jdbcType=VARCHAR},
</if>
<if test="delayTime != null" >
DELAY_TIME = #{delayTime,jdbcType=INTEGER},
</if>
<if test="status != null" >
STATUS = #{status,jdbcType=INTEGER},
</if>
<if test="workMode != null" >
WORK_MODE = #{workMode,jdbcType=INTEGER},
</if>
<if test="channelSN != null">
CHANNEL_SN = #{channelSN,jdbcType=VARCHAR},
</if>
<if test="roomNum != null">
ROOM_NUM = #{roomNum,jdbcType=VARCHAR},
</if>
<if test="onlineStatus != null">
CHANNEL_STATUS = #{onlineStatus,jdbcType=VARCHAR},
</if>
<if test="channelCapacity != null">
CHANNEL_CAPACITY = #{channelCapacity,jdbcType=INTEGER},
</if>
<if test="channelPersonType != null">
CHANNEL_PERSON_TYPE = #{channelPersonType,jdbcType=INTEGER},
</if>
<if test="psdFlag != null">
PSD_FLAG = #{psdFlag,jdbcType=TINYINT},
</if>
<if test="physicsId != null">
PHYSICS_ID = #{physicsId,jdbcType=VARCHAR},
</if>
<if test="gpsX != null">
GPS_X = #{gpsX,jdbcType=VARCHAR},
</if>
<if test="gpsY != null">
GPS_Y = #{gpsY,jdbcType=VARCHAR},
</if>
<if test="gpsZ != null">
GPS_Z = #{gpsZ,jdbcType=VARCHAR},
</if>
<if test="enableMode != null">
ENABLE_MODE = #{enableMode,jdbcType=INTEGER},
</if>
<if test="timeoutAlarmEnable != null">
TIMEOUT_ALARM_ENABLE = #{timeoutAlarmEnable,jdbcType=INTEGER},
</if>
<if test="closeTimeOut != null">
CLOSE_TIMEOUT = #{closeTimeOut,jdbcType=INTEGER},
</if>
CHANNEL_BIND_ORG = #{channelBindOrg,jdbcType=BIGINT}
</set>
where ID = #{id,jdbcType=BIGINT}
</update>
<update id="updateAccessControlChannelsByChannelCodes" parameterType="map" >
update acs_access_control_chn
<set>
<if test="orgCode != null" >
ORG_CODE = #{orgCode,jdbcType=VARCHAR},
</if>
<if test="validFlag != null" >
VALID_FLAG = #{validFlag,jdbcType=INTEGER},
</if>
<if test="status != null" >
STATUS = #{status,jdbcType=INTEGER},
</if>
<if test="workMode != null" >
WORK_MODE = #{workMode,jdbcType=INTEGER}
</if>
</set>
where CHANNEL_CODE in
<foreach item="item" index="index" collection="channelCodes"
open="(" separator="," close=")">
#{item}
</foreach>
</update>
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="product" index="index" separator=";">
UPDATE park_enterprise_info
<set>
<if test="product.parkId != null">
PARK_ID = #{product.parkId},
</if>
<if test="product.enterpriseName != null">
ENTERPRISE_NAME = #{product.enterpriseName},
</if>
<if test="product.enterpriseHonor != null">
ENTERPRISE_HONOR = #{product.enterpriseHonor},
</if>
<!-- 可能还有其他字段 -->
</set>
WHERE id = #{product.id}
</foreach>
</update>
4.Mybatis 批量操作存在则更新或者忽略,不存在则插入
INSERT INTO specs(`status`,`type`,`code`,`name`,`create_time`,`create_by`)
VALUES
<foreach collection="list" item="item" separator=",">
(
#{item.status,jdbcType=INT},
#{item.type,jdbcType=INT},
#{item.code,jdbcType=VARCHAR},
#{item.name,jdbcType=VARCHAR},
#{item.createTime,jdbcType=DATETIME},
#{item.createBy,jdbcType=VARCHAR} )
</foreach>
ON DUPLICATE KEY UPDATE
<trim prefix="" suffixOverrides=",">
<foreach collection="list" separator="," item="item">
<if test="item.name != null and item.name != ''">
`name` = VALUES(name)
</if>
</foreach>
</trim>
ON DUPLICATE KEY UPDATE 语句中的“重复键”是指那些定义了唯一性约束的列。这些列可能是:
1. 主键(PRIMARY KEY):表中的主键必须是唯一的。
2. 唯一索引(UNIQUE INDEX):除了主键之外,还可以定义唯一索引,保证某些列的组合是唯一的。
当插入的数据在这些唯一性约束列上与现有数据发生冲突时,MySQL 将不会插入新的记录,而是根据 ON DUPLICATE KEY UPDATE 子句进行相应的更新操作。