在用mybatis
对于批量处理,
ibatis sqlMapper方式已经不能用了。
自己试了一下,java实现batch
随便写的。
对于批量处理,
ibatis sqlMapper方式已经不能用了。
自己试了一下,java实现batch
随便写的。
SqlSession sqlse = getSqlSession();
Connection connection = null;
Transaction transaction = null;
Environment env;
BatchExecutor be;
try {
Configuration conf = sqlse.getConfiguration();
conf.setDefaultExecutorType(ExecutorType.BATCH);
env = conf.getEnvironment();
connection = env.getDataSource().getConnection();
connection.setAutoCommit(false);
transaction = env.getTransactionFactory().newTransaction(connection, false);
be = new BatchExecutor(conf, transaction);
MappedStatement ms = conf.getMappedStatement(sqlID);
int x = 0;
Object bt;
List<Object> bts = (List<Object>) bindParams;
for (int i = 0; i < bts.size(); i++) {
bt = bts.get(i);
x = be.update(ms, bt);
System.out.println(x + "x:");
}
be.commit(true);
transaction.commit();
List<BatchResult> bl = be.flushStatements();
if(bl != null){
for(int b = 0; b < bl.size(); b ++){
BatchResult br = bl.get(b);
re = br.getUpdateCounts();
System.out.println("re length:" + re.length);
}
}
be.close(false);
transaction.close();
System.out.println(x);
} catch (SQLException e) {
e.printStackTrace();
}