mybatis批量插入语句(Oracle)

mybatis配置文件里面sql写法(因为是oracle数据库,所以statement的写法和 MySQL 有些不同)

<!-- 批量插入用户 -->
<insert id="insertUserByBatch" parameterType="java.util.List">
INSERT INTO TEST1

(NAME,AGE)

SELECT A.*

FROM(

<foreach collection="list" item="test1" index="index"
separator="UNION ALL">

SELECT

#{test1.name,jdbcType=VARCHAR},
#{test1.age,jdbcType=INTEGER}

FROM dual

</foreach>

) A

</insert>

service层

public void insertUserByBatch(List<Test1> list) {
// TODO Auto-generated method stub

// 2.分页数据信息
int totalSize = list.size(); // 总记录数
int pageSize = 1000; // 每页N条 1000条每次 测试10w条 大概3s多
int totalPage = totalSize / pageSize; // 共N页

if (totalSize % pageSize != 0) {
totalPage += 1;
if (totalSize < pageSize) {
pageSize = list.size();
}
}

long befortime = System.currentTimeMillis();

//临时list
List<Test1> tempList = new ArrayList<Test1>();

for (int pageNum = 1; pageNum < totalPage + 1; pageNum++) {
int starNum = (pageNum - 1) * pageSize;
int endNum = pageNum * pageSize > totalSize ? (totalSize) : pageNum * pageSize;
System.out.println("起始:" + starNum + "-" + endNum );



for (int i = starNum; i < endNum; i++) {
tempList.add(list.get(i));



}

userMapper.insertUserByBatch(tempList);
tempList.clear();
System.out.println("success!");


}

long aftertime = System.currentTimeMillis();
System.out.println("castime:" + (aftertime - befortime) + " ms");

}

猜你喜欢

转载自www.cnblogs.com/lidianlin/p/10484080.html
今日推荐