终于实现了ibatis的批量插入,此方法插入3000条数据,比单条插入可以节省一半的时间
XML代码:
<insert id="insertBatch" parameterType="ArrayList">
insert intouser(id,account,password,active,status,name,gender,active_date,expiry_date,type,remark,group_id,disable,exam_number,mobile)
values
<foreach collection="list"item="obj" index="index"separator="," >
(#{obj.id},#{obj.account},#{obj.password},#{obj.active},#{obj.status},#{obj.name},#{obj.gender},#{obj.active_date},
#{obj.expiry_date},#{obj.type},#{obj.remark},#{obj.group_id},#{obj.disable},#{obj.exam_number},#{obj.mobile})
</foreach>
</insert>
在java中只需要传递list过来就可以了。
我是这样写的:
private ArrayList<Map<String, String>>userList = new ArrayList<Map<String,String>>();
Map<String,String>userMap= new HashMap<String,String>();
userMap.put("id", userId);
userMap.put("account", arr[0]);
userMap.put("password", pas);
userMap.put("active", String.valueOf(1));
userMap.put("status", String.valueOf(1));
userMap.put("name", arr[1]);
userMap.put("gender", String.valueOf(gender));
userMap.put("active_date", active_date);
userMap.put("expiry_date", expiry_date);
userMap.put("type", String.valueOf(3));
userMap.put("remark", null);
userMap.put("group_id", String.valueOf(1));
userMap.put("disable", String.valueOf(0));
userMap.put("Exam_number", arr[15]);
userMap.put("phoneNumber", arr[14]);
userList.add(userMap);
通过循环将数据加入到list,最后将list传递。