mybatis 批量 *** 作数据

mybatis 批量 *** 作数据,第1张

mybatis的批量 *** 作有两种方式,一是使用foreach标签,二是使用mybatis的BATCH模型

在xml中通过foreach对表数据进行循环 *** 作

在oracle中不支持insert into product(name, type, price) values ('a', 'tv', 1233), ('b', 'ac', 3455),....('','','')这种形式的sql,因此oracle批量插入使用 insert all into table(...) values(...) into table(...) values(...) select * from dual语句来解决以下方式,并且需要显示指定useGeneratedKeys=false

另一种方法是使用另外一种方法是 insert into table(...) (select ... from dual) union all (select ... from dual)

Mybatis内置的ExecutorType一共有三种,默认为SIMPLE,该模式下它为每个语句的执行创建一个新的预处理语句,并单条提交sql。

BATCH 模式会重复使用已经预处理的语句,并且批量执行所有更新语句,显然batch性能将更优;

注意 : batch模式也有自己的问题,比如在Insert *** 作时,在事务没有提交之前,是没有办法获取到自增的id,这在某型情形下是不符合业务要求的。

具体用法如下:

ProductMapper.java 如下:

mapper.xml 如下:

dao实现文件中函数:

@Override

public int insertContentList(

List<PubFieldContentEntity>list) {

Map<String, Object>params = createMap()

params.put("list", list)

return this.insert("insertContentList", params)

}

对应的mapper.xml文件中

<!-- 批量插入 -->

<insert id="insertContentList" useGeneratedKeys="true" keyProperty="id">

<![CDATA[

insert into tableContent(pubId,integrant,ownContentName,dateLine,pubFieldContentName,suffix,modifyTime,deleteTime,compareType,pubContentIndex,multiple,isInput,pubFieldTitle) values

<foreach collection="list" item="item" index="index" separator=",">

(#{item.pubId},#{item.integrant},#{item.ownContentName},unix_timestamp(),#{item.pubFieldContentName},#{item.suffix},unix_timestamp(),#{item.deleteTime},#{item.compareType},#{item.pubContentIndex},#{item.multiple},#{item.isInput},#{item.pubFieldTitle})

</foreach>

]]>

</insert>

额 mybatis只是 sql的拼接啊,你原生sql怎么插入 这个就怎么插入啊,无法在java控制分段调用 mybatis的批量插入,而mybatis<pre t="code" l="xml">方法一:

<insert id="insertbatch" parameterType="java.util.List">

<selectKey keyProperty="fetchTime" order="BEFORE"

resultType="java.lang.String">

SELECT CURRENT_TIMESTAMP()

</selectKey>

insert into kangaiduoyaodian ( depart1, depart2, product_name,

generic_name, img, product_specification, unit,

approval_certificate, manufacturer, marketPrice, vipPrice,

website, fetch_time, productdesc ) values

<foreach collection="list" item="item" index="index"

separator=",">

( #{item.depart1}, #{item.depart2}, #{item.productName},

#{item.genericName}, #{item.img},

#{item.productSpecification}, #{item.unit},

#{item.approvalCertificate}, #{item.manufacturer},

#{item.marketprice}, #{item.vipprice}, #{item.website},

#{fetchTime}, #{item.productdesc} )

</foreach>

</insert>

方法二:

<insert id="batchInsertB2B" parameterType="ArrayList">

insert into xxxxtable(hkgs,hkgsjsda,office,asdf,ddd,ffff,supfullName,classtype,agent_type,remark)

<foreach collection="list" item="item" index="index" separator="union all">

select #{item.hkgs,jdbcType=VARCHAR},

#{item.hkgsjsda,jdbcType=VARCHAR},

#{item.office,jdbcType=VARCHAR},

#{item.asdf,jdbcType=VARCHAR},

#{item.ddd,jdbcType=VARCHAR},

#{item.ffff,jdbcType=VARCHAR},

#{item.supfullName,jdbcType=VARCHAR},0,0,

#{item.remark,jdbcType=VARCHAR} from dual

</foreach>

</insert>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11455186.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存