mybatis批量新增,更新的一些问题
2014-07-01 12:29
337 查看
使用mybatis批量操作时,基本都是依靠foreach 标签
关于foreach的用法
可以参考此贴http://blog.sina.com.cn/s/blog_6a0cd5e501011snl.html
下面是一些摘录
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有 item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况
下,该属性的值是不一样的,主要有一下3种情况:
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
批量删除的操作这里就不讨论了,到处都是也比较简单。
先谈下批量插入的问题,由于oracle,和mysql mssql的批量insert语句有一点点区别。
例:
Sql代码
oracle :
insert into sss
(
a,b
)
select '11','aaa' from dual union all
select '22','bbb' from dual
而在mssql和mysql中是这样的:
insert into sss
(
a,b
)
values('1','2'),('11','2')
所有就使用foreach时有一些区别:
例如下
Xml代码
<!-- 批量插入for Oracle -->
<insert id="insertBatch4Oracle" parameterType="List">
insert into aa
(
a,b
)
<foreach collection="list" item="item" index="index" separator="union all" >
select #{item.a},
#{item.b}
from dual
</foreach>
</insert>
<!-- 批量插入for Other -->
<insert id="insertBatch4Other" parameterType="List">
insert into aa
(
a,b
)
VALUES
<foreach collection="list" item="obj" index="index" separator="," >
(
#{obj.a},#{obj.b}
)
</foreach>
</insert>
<insert id="insertBatch4Oracle" parameterType="List">
insert into aa
(
id, a,b
)
select SEQ_oracle.nextval//序列, T.* from(
<foreach collection="list" item="item" index="index" separator="union all" >
select
#{item.a},
#{item.b}
from dual
</foreach>
) T
</insert>
还有一个批量更新的问题,oracle中 形如 update *** set *** where ** in(....)
这种语句 in所在的集合有条数限制 为1000条,当我们在批量更新千条以上数据时就会有问题,解决方法可以分段在执行,1000条记录为一段。
Xml代码
<update id="updateBatch" parameterType="Map">
update aa set
a=#{fptm},
b=#{csoftrain}
where c in
<foreach collection="cs" index="index" item="item" open="("separator=","close=")">
#{item}
</foreach>
</update>
java代码:
Java代码
public int updateBatch(BB b, List<String> SS) {
int count = 0;
int len = (SS.size() + 999) / 1000;
List<List<String>> a = new ArrayList<List<String>>();
for (int i = 0; i < len; i++) {
a.add(new ArrayList<String>());
}
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < 1000; j++) {
a.get(i).add(SS.get((i * 1000) + j));
}
}
for (int i = 0; i < SS.size() % 1000; i++) {
a.get(len - 1).add(SS.get(((len - 1) * 1000) + i));
}
for (int i = 0; i < a.size(); i++) {
Map pM = BeanUtils.describe(rp);
pM.put("SS", a.get(i));
this.getSqlSessionTemplate().update(MAPPER_NS + "updateBatch",
pM);
count += a.get(i).size();
}// 分段批量更新
return count;
}
关于foreach的用法
可以参考此贴http://blog.sina.com.cn/s/blog_6a0cd5e501011snl.html
下面是一些摘录
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有 item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况
下,该属性的值是不一样的,主要有一下3种情况:
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
批量删除的操作这里就不讨论了,到处都是也比较简单。
先谈下批量插入的问题,由于oracle,和mysql mssql的批量insert语句有一点点区别。
例:
Sql代码
oracle :
insert into sss
(
a,b
)
select '11','aaa' from dual union all
select '22','bbb' from dual
而在mssql和mysql中是这样的:
insert into sss
(
a,b
)
values('1','2'),('11','2')
所有就使用foreach时有一些区别:
例如下
Xml代码
<!-- 批量插入for Oracle -->
<insert id="insertBatch4Oracle" parameterType="List">
insert into aa
(
a,b
)
<foreach collection="list" item="item" index="index" separator="union all" >
select #{item.a},
#{item.b}
from dual
</foreach>
</insert>
<!-- 批量插入for Other -->
<insert id="insertBatch4Other" parameterType="List">
insert into aa
(
a,b
)
VALUES
<foreach collection="list" item="obj" index="index" separator="," >
(
#{obj.a},#{obj.b}
)
</foreach>
</insert>
<insert id="insertBatch4Oracle" parameterType="List">
insert into aa
(
id, a,b
)
select SEQ_oracle.nextval//序列, T.* from(
<foreach collection="list" item="item" index="index" separator="union all" >
select
#{item.a},
#{item.b}
from dual
</foreach>
) T
</insert>
还有一个批量更新的问题,oracle中 形如 update *** set *** where ** in(....)
这种语句 in所在的集合有条数限制 为1000条,当我们在批量更新千条以上数据时就会有问题,解决方法可以分段在执行,1000条记录为一段。
Xml代码
<update id="updateBatch" parameterType="Map">
update aa set
a=#{fptm},
b=#{csoftrain}
where c in
<foreach collection="cs" index="index" item="item" open="("separator=","close=")">
#{item}
</foreach>
</update>
java代码:
Java代码
public int updateBatch(BB b, List<String> SS) {
int count = 0;
int len = (SS.size() + 999) / 1000;
List<List<String>> a = new ArrayList<List<String>>();
for (int i = 0; i < len; i++) {
a.add(new ArrayList<String>());
}
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < 1000; j++) {
a.get(i).add(SS.get((i * 1000) + j));
}
}
for (int i = 0; i < SS.size() % 1000; i++) {
a.get(len - 1).add(SS.get(((len - 1) * 1000) + i));
}
for (int i = 0; i < a.size(); i++) {
Map pM = BeanUtils.describe(rp);
pM.put("SS", a.get(i));
this.getSqlSessionTemplate().update(MAPPER_NS + "updateBatch",
pM);
count += a.get(i).size();
}// 分段批量更新
return count;
}
相关文章推荐
- 关于Mybatis使用的一些问题汇总(持续更新)
- MyBatis+oracle 批量插入数据的一些问题
- Mysql 在 mybatis 环境下 批量新增 并 将已存在的数据更新
- mybatis如何实现批量更新和插入新增实例详解(附SQL以及mapper配置)
- mybatis 批量新增并更新已有数据
- mybatis中批量更新的问题
- MyBatis批量新增和更新
- T-SQL问题汇总:批量更新
- 0.Ubuntu下的一些小问题的解决方法(持续更新中)
- C#中dataGrid的批量更新问题解决方案
- 关于淘宝 TOP 开发 使用的一些心得,问题不断更新中。。。
- 我用VS2008写的的第一个Filter,遇见的一些问题,和解决方法,更新结束。
- cuda一些小问题(不断更新)
- 集合框架的一些问题(持续更新)
- sqlserver移植为Oracle笔记(更新,新增字段名;批量新增记录;日期查询;截取字串函数)
- C# 水晶报表问题的一些总结(持续更新)
- J2SE中的一些基础问题总结(不断更新)
- 琢磨了一天用SqlCommandBuilder进行批量更新,有一些新的认识
- 试列举c++与java的区别时发现的一些问题(更新中10-29)
- ORACLE海量数据频繁更新容易出一些怪问题