首页 > 作文

Mybatis 批量更新实体对象方式

更新时间:2023-04-03 23:13:51 阅读: 评论:0

目录
mybatis批量更新实体对象(1)dao层接口(2)mapper.xml 文件mybatis批量更新数据三种方法效率对比探讨批量更新数据三种写法的效率问题

mybatis批量更新实体对象

(1)dao层接口

    /**     * 根据更新采购计划(批量)     * @param plans     */    void batchupdateplan(list<pubpurchaplan> plans);

(2)mapper.xml 文件

<sql id="batchupdateplancondition">    <where>        <foreach collection="list" item="item" open="( " parator=") or (" clo=" )">            comid = #{item.comid} and id = #{item.id}        </foreach>    </where></sql>&6的英文lt;update id="batchupdateplan" parametertype="list">        update pub_purcha_plan        <trim prefix="t" suffixoverrides=",">            <trim prefix="warehouid=ca" suffix="end,">                 <foreach collection="list" item="item" index="index">                    when comid = #{item.comid} and id = #{item.id} then #{item.warehouid}                 </foreach>            </trim>            <trim prefix="productid=ca" suffix="end,">                 <foreach collection="list" item="item" index="index">                   when comid = #{item.comid} and id = #{item.id} then #{item.productid}                 </foreach>            </trim>            <trim prefix="amount=ca" suffix="end,">                 <foreach collection="list" item="item" index="index">                   when comid = #{item.comid} and id = #{item.id} then #{item.amount}                 </foreach>            </trim>            <trim prefix="deleted=ca" suffix="end,">                 <foreach collection="list" item="item" index="index">                   when comid = #{item.comid} and id = #{item.id} then #{item.deleted}                 </foreach>            </trim>            <trim prefix="price=ca" suffix="end,">                 <foreach collection="list" item="item" index="index">                   when comid = #{item.comid} and id = #{item.id} then #{item.price}                 </foreach>            </trim>            <trim prefix="type=ca" suffix="end,">                 <f黄河水文网oreach collection="list" item="item" index="index">                   when comid = #{item.comid} and id = #{item.id} then #{item.type}                 </foreach>            </trim>        </trim>        <include refid="batchupdateplancondition"/></up四年级下册语文第五单元作文date> 

mybatis批量更新数据三种方法效率对比

探讨批量更新数据三种写法的效率问题

实现方式有三种

1、用for循环通过循环传过来的参数集合,循环出n条sql2、用mysql的ca when 条件判断变相的进行批量更新3、用on duplicate key update进行批量更新

下面进行实现。

注意第一种方法要想成功,需要在db链接url后面带一个参数 &allowmultiqueries=true

即: jdbc:mysql://localhost:3306/mysqltest?characterencoding=utf-8&allowmultiqueries=true

其实这种东西写过来写过去就是差不多一样的代码,不做重复的赘述,直接上代码。

     <!-- 批量更新第一种方法,通过接收传进来的参数list进行循环着组装sql -->     <update id="updatebatch" parametertype="java.util.list" >        <foreach collection="list" item="item" index="index" open="" clo="" parator=";">            update standard_relation            <t >                <if test="item.standardfromuuid != null" >                    standard_from_uuid = #{item.standardfromuuid,jdbctype=varchar},                </if>                <if tesmpa等于多少公斤t="item.standardtouuid != null" >                    standard_to_uuid = #{item.standardtouuid,jdbctype=varchar},                </if>                <if test="item.gmtmodified != null" >                    gmt_modified = #{item.gmtmodified,jdbctype=timestamp},                </if>            </t>            where id = #{item.id,jdbctype=bigint}        </foreach>    </update>     <!-- 批量更新第二种方法,通过 ca when语句变相的进行批量更新 -->    <update id="updatebatch" parametertype="java.util.list" >        update standard_relation        <trim prefix="t" suffixoverrides=",">            <trim prefix="standard_from_uuid =ca" suffix="end,">                <foreach collection="list" item="i" index="index">                    <if test="i.standardfromuuid!=null">                        when id=#{i.id} then #{i.standardfromuuid}                    </if>                </foreach>            </trim>            <trim prefix="standard_to_uuid =ca" suffix="end,">                <foreach collection="list" item="i" index="index">                    <if test="i.standardtouuid!=null">                        when id=#{i.id} then #{i.standardtouuid}                    </if>                </foreach>            </trim>            <trim prefix="gmt_modified =ca" suffix="end,">                <foreach collection="list" item="i" index="index">                    <if test="i.gmtmodified!=null">                        when id=#{i.id} then #{i.gmtmodified}                    </if>                </foreach>            </trim>        </trim>        where        <foreach collection="list" parator="or" item="i" index="index" >            id=#{i.id}        </foreach>    </update>批量更新第三种方法,用on duplicate key update <inrt id="updatebatch" parametertype="java.util.list">        inrt into standard_relation(id,relation_type, standard_from_uuid,        standard_to_uuid, relation_score, stat,        last_process_id, is_deleted, gmt_created,        gmt_modified,relation_desc)values        <foreach collection="list" item="item" index="index" parator=",">            (#{item.id,jdbctype=bigint},#{item.relationtype,jdbctype=varchar}, #{item.standardfromuuid,jdbctype=varchar},            #{item.standardtouuid,jdbctype=varchar}, #{item.relationscore,jdbctype=decimal}, #{item.stat,jdbctype=tinyint},            #{item.lastprocessid,jdbctype=bigint}, #{item.isdeleted,jdbctype=tinyint}, #{item.gmtcreated,jdbctype=timestamp},            #{item.gmtmodified,jdbctype=timestamp},#{item.relationdesc,jdbctype=varchar})        </foreach>        on duplicate key update        id=values(id),relation_type = values(relation_type),standard_from_uuid = values(standard_from_uuid),standard_to_uuid = values(standard_to_uuid),        relation_score = values(relation_score),stat = values(stat),last_process_id = values(last_process_id),        is_deleted = values(is_deleted),gmt_created = values(gmt_created),        gmt_modified = values(gmt_modified),relation_desc = values(relation_desc)    </inrt>
 @override    public void updatestandardrelations() {        list<standardrelation> list=standardrelationmapper.lectbystandarduuid("xiemingjieupdate");        for(standardrelation tmp:list){            tmp.tstandardfromuuid(tmp.getstandardfromuuid()+"update");            tmp.tstandardtouuid(tmp.getstandardtouuid()+"update");        }        long begin=system.currenttimemillis();        standardrelationmanager.updatebatch(list);        long end=system.currenttimemillis();        system.out.print("当前的批量更新的方法用时"+(end-begin)+"ms");    }

sql语句for循环效率其实相当高的,因为它仅仅有一个循环体,只不过最后update语句比较多,量大了就有可能造成sql阻塞。

ca when虽然最后只会有一条更新语句,但是xml中的循环体有点多,每一个ca论文参考文献自动生成 when 都要循环一遍list集合,所以大批量拼sql的时候会比较慢,所以效率问题严重。使用的时候建议分批插入。

duplicate key update可以看出来是最快的,但是一般大公司都禁用,公司一般都禁止使用replace into和inrt into … on duplicate key update,这种sql有可能会造成数据丢失和主从上表的自增id值不一致。而且用这个更新时,记得一定要加上id,而且values()括号里面放的是数据库字段,不是java对象的属性字段。

根据效率,安全方面综合考虑,选择适合的很重要。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。

本文发布于:2023-04-03 23:13:50,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/0e069b0d1edf642b028616df4455d210.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Mybatis 批量更新实体对象方式.doc

本文 PDF 下载地址:Mybatis 批量更新实体对象方式.pdf

标签:批量   效率   三种   种方法
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图