
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>
1. 自定义对象也用@param注解.2. 在mapper.xml中使用的时候,#{对象别名.属性名},如#{user.id}
注意,使用了@pram注解的话在mapper.xml不加parameterType。
public List selectAllUsers(
@Param("user") UserExtension user,
@Param("begin") int begin,
@Param("end") int end)
mybatis中多条件删除例子如下:
delete from tb_duty where
( dscd=#{item.dscd},
and unit_id=#{item.unitId},
and year=#{item.year},
and month=#{item.month},
and flag=#{item.flag} )
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)