MyBatis批量新增和更新

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);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2269.html原文链接:https://javaforall.net

(0)
上一篇 2020年11月12日 上午9:19
下一篇 2020年11月12日 上午9:19


相关推荐

  • SPSS异方差检验的实现

    SPSS异方差检验的实现SPSS 异方差检验的实现此次介绍两种异方差检验的方法 残差图分析法和等级相关系数法残差图分析法当回归模型满足所有假定时 残差图上的 n 个点的散步应该是随机的 无任何规律 如果回归模型存在异方差性 残差图上的点散布会呈现一定的趋势 在 SPSS 中选择 转换 回归 线性 分别选入对应的自变量因变量 点击 保存 在残差栏中选择未标准化 确定 选择 图形 旧对话框 散点图 将未标准化的残差选入 X 轴 自变量选入 Y 轴点击 确定 得到残差图等级相关系数法计算残差步骤在 1 中已演示

    2025年6月23日
    6
  • bindService:绑定本地服务和远程服务示例

    bindService:绑定本地服务和远程服务示例绑定本地服务AndroidManifest.xml中声明服务:&lt;serviceandroid:name=".TestLocalService"&gt;&lt;intent-filter&gt;&lt;actionandroid:name="maureen.intent.action.BIND_LOCAL…

    2022年6月7日
    32
  • DeepSeek 满血版在 VSCode 和 IDEA 中的完整使用指南

    DeepSeek 满血版在 VSCode 和 IDEA 中的完整使用指南

    2026年3月16日
    3
  • Idea激活码最新教程2018.1.8版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2018.1.8版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2018 1 8 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2018 1 8 成功激活

    2025年5月24日
    5
  • siger获取 本机信息

    siger获取 本机信息sigar x86 winnt dll 文件拷贝到 Java nbsp SDK 目录的 binpublic nbsp static nbsp void nbsp main String nbsp args nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp try nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp System 信息 从 jvm 获取 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp System setProperty java library path nbsp so

    2026年3月19日
    1
  • 给一组数据 怎么用matlab花折线图,matlab 画折线图 实例「建议收藏」

    给一组数据 怎么用matlab花折线图,matlab 画折线图 实例「建议收藏」数据:某地区近50年雷暴发生次数(1957~2006雷暴日):画如下的年际变化图。———————————————————————————%将数据载入数组a中a=[…..(省略)];x=1957:2006;x=x’;%ma中放平均值:ma=zeros(50,1)+mean(a);p…

    2022年5月23日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号