mybatis批量更新和踩坑总结
一、mybatis批量更新示例:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cars.ict.rbpsems.mapper.BroadcastStatusMapper">
<!-- 批量插入数据 -->
<insert id="batchInsert" useGeneratedKeys="true" keyProperty="sid">
insert into broadcast_status (id, device_id, device_name, device_class, online_status, fault_state, ocupy_state, temp, station_code, bureau_code, create_time, modify_time ) values
<foreach collection="list" item="d" separator=",">
(
#{d.id},
#{d.deviceId},
#{d.deviceName},
#{d.deviceClass},
#{d.onlineStatus},
#{d.faultState},
#{d.ocupyState},
#{d.temp},
#{d.stationCode},
#{d.bureauCode},
#{d.createTime},
#{d.modifyTime})
</foreach>
</insert>
<!-- 批量更新数据 -->
<update id="batchUpdate">
<foreach collection="upList" item="d" separator=";">
update broadcast_status
<set>
<if test="d.deviceName != '' and d.deviceName != null">
device_name = #{d.deviceName},
</if>
<if test="d.deviceClass != '' and d.deviceClass != null">
device_class = #{d.deviceClass},
</if>
<if test="d.onlineStatus != '' and d.onlineStatus != null">
online_status = #{d.onlineStatus},
</if>
<if test="d.faultState != '' and d.faultState != null">
fault_state = #{d.faultState},
</if>
<if test="d.ocupyState != '' and d.ocupyState != null">
ocupy_state = #{d.ocupyState},
</if>
<if test="d.temp != '' and d.temp != null">
temp = #{d.temp},
</if>
<if test="d.modifyTime != null">
modify_time = #{d.modifyTime},
</if>
</set>
where bureau_code = #{bureauCode} and station_code = #{stationCode} and device_id = #{d.deviceId}
</foreach>
</update>
</mapper>
其中批量插入时,主键生成UUID的实体配置eg:
@Data
@TableName("abc_def")
public class abcDef implements Serializable {
/**
* id
*/
@Id
// @GenericGenerator(name = "uuid", strategy = "uuid")
// @GeneratedValue(generator = "uuid")
@Column(length = 50, name = "id")
@TableId(value = "id", type = IdType.UUID)
private String id;
mybatis批量更新示例2:
<update id="batchUpdate">
<foreach collection="willUpdateList" item="item" index="index" open="" close="" separator=";">
update guide_screen_sync_monitoring
<set>
<if test="item.deviceName != null and item.deviceName != ''">
device_name = #{item.deviceName},
</if>
<if test="item.cpu != null and item.cpu != ''">
cpu = #{item.cpu},
</if>
<if test="item.disk != null and item.disk != ''">
disk = #{item.disk},
</if>
<if test="item.memory != null and item.memory != ''">
memory = #{item.memory},
</if>
<if test="item.sysVersion != null and item.sysVersion != ''">
sys_version = #{item.sysVersion},
</if>
<if test="item.status != null and item.status != ''">
status = #{item.status},
</if>
<if test="item.modifyTime != null">
modify_time = #{item.modifyTime},
</if>
</set>
<trim suffixOverrides=",">
where ip = #{item.ip} and station_code = #{stationCode}
</trim>
</foreach>
</update>
二、踩的坑,搜索相关资料后总结:
1.@Param注解必须是mybatis下的否则会报错:nested exception is org.apache.ibatis.binding.BindingException
有的开发者即使使用了@Param注解,还是会报这个异常,原因是在导包的时候,导入的是spring的@Param的注解包,在这里我们需要导入mybatis的@Param注解包,也就是org.apache.ibatis.annotations.Param;这个包
2.批量执行需要在配置文件中添加&allowMultiQueries=true
如果使用 Mybatis 框架对 MySQL 数据库进行批量更新或者插入操作,需要在连接数据库的 URL 加上 allowMultiQueries=true,这样便可以执行批处理操作了,否则报sql语法错误。
可以参考:https://blog.csdn.net/weixin_43252521/article/details/125302997
3.不能在mybatis.xml文件中使用 -- 注释不然会报错:
Could not set parameters for mapping: ParameterMapping{property=‘s_id‘, mode=IN, javaType=class
4.对Date类型数据进行非空判断不能使用 != 只能用 != null 否则会报错:
Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
删除 creationDate != '' ,去掉Date类型与空字符串的比较,保留creationDate != null 就好了。
5. 确认表名、字段及字段名和类型是否正确,是否有set 关键字。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示