MyBatis批量新增和更新
之前有开发任务一个接口里面有大量的数据新增和更新操作,导致十分缓慢。使用了批量操作之后速度有明显提升,几乎百倍千倍的速度提升。
博主之前统计过,通过普通接口一次数据库插入大概需要200ms,对于大量新增或更新操作的情况,数据库批量操作是十分有必要的。废话不多说,直接上代码。
博主的resultMap如下:
<resultMap id="BaseResultMap" type="com.luo.domain.Words" >
<id column="word_no" property="wordNo" jdbcType="BIGINT" />
<result column="value" property="value" jdbcType="VARCHAR" />
<result column="filed_class" property="filedClass" jdbcType="VARCHAR" />
<result column="pinyin" property="pinyin" jdbcType="VARCHAR" />
<result column="synonym" property="synonym" jdbcType="VARCHAR" />
<result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
<result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />
<result column="operator_no" property="operatorNo" jdbcType="VARCHAR" />
<result column="src_channel" property="srcChannel" jdbcType="VARCHAR" />
<result column="latest_operation" property="latestOperation" jdbcType="VARCHAR" />
<result column="versions" property="versions" jdbcType="BIGINT" />
<result column="file_index" property="fileIndex" jdbcType="BIGINT" />
<result column="charac_class" property="characClass" jdbcType="VARCHAR" />
<result column="weight" property="weight" jdbcType="INTEGER" />
</resultMap>
批量新增
<insert id="addWordsByList" parameterType="java.util.List">
insert into words (word_no, value, filed_class,
pinyin, synonym, create_date,
update_date, operator_no, src_channel,
latest_operation, versions, file_index,
charac_class, weight)
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.wordNo},#{item.value},#{item.filedClass},#{item.pinyin},
#{item.synonym},#{item.createDate},#{item.updateDate},#{item.operatorNo},
#{item.srcChannel},#{item.latestOperation},#{item.versions},#{item.fileIndex},
#{item.characClass},#{item.weight})
</foreach>
</insert>
接口:
public void addWordsByList(List<Words> wordsList);
批量更新
批量更新必须在添加如下数据库连接配置:&allowMultiQueries=true,否则会报SQL格式错误
比如MySQL:
jdbc:MySQL://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
<update id="updateWordsByList" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";">
update words
<set >
<if test="item.value != null" >
value = #{item.value,jdbcType=VARCHAR},
</if>
<if test="item.filedClass != null" >
filed_class = #{item.filedClass,jdbcType=VARCHAR},
</if>
<if test="item.pinyin != null" >
pinyin = #{item.pinyin,jdbcType=VARCHAR},
</if>
<if test="item.synonym != null" >
synonym = #{item.synonym,jdbcType=VARCHAR},
</if>
<if test="item.createDate != null" >
create_date = #{item.createDate,jdbcType=TIMESTAMP},
</if>
<if test="item.updateDate != null" >
update_date = #{item.updateDate,jdbcType=TIMESTAMP},
</if>
<if test="item.operatorNo != null" >
operator_no = #{item.operatorNo,jdbcType=VARCHAR},
</if>
<if test="item.srcChannel != null" >
src_channel = #{item.srcChannel,jdbcType=VARCHAR},
</if>
<if test="item.latestOperation != null" >
latest_operation = #{item.latestOperation,jdbcType=VARCHAR},
</if>
<if test="item.versions != null" >
versions = #{item.versions,jdbcType=BIGINT},
</if>
<if test="item.fileIndex != null" >
file_index = #{item.fileIndex,jdbcType=BIGINT},
</if>
<if test="item.characClass != null" >
charac_class = #{item.characClass,jdbcType=VARCHAR},
</if>
<if test="item.weight != null" >
weight = #{item.weight,jdbcType=INTEGER},
</if>
</set>
where word_no = #{item.wordNo,jdbcType=BIGINT}
</foreach>
</update>
接口:
public void updateWordsByList(List<Words> wordsList);
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 从零开始开发一个 MCP Server!
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档