这些天一直在改以前的程序,优化程序,看看之前写的都有点好笑!!!应为要用到批量的插入和更新,所以我用到了ibatis 的batch,但是batch一直都不"听话",废话少说,看一下我的程序好了,请知道的给我留言哟,在此,感谢了!
public class IbatisBatchTest {
static Logger log = Logger.getLogger(IbatisBatchTest.class);
public static SqlMapClient getSqlMapClient () {
SqlMapClient sqlmapclient = null;
String resource = "SqlMapConfig.xml";
Reader reader=null;
try {
reader = Resources.getResourceAsReader(resource);
sqlmapclient = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
}
return sqlmapclient;
}
public static int batch () {
SqlMapClient sqlMapClient = getSqlMapClient ();
int tmp = 0;
try {
sqlMapClient.startBatch();
for (int i = 0; i < 10; i++) {
BatchDTO dto = new BatchDTO();
dto.setId(new Integer(i));
dto.setName("1");
sqlMapClient.insert("batch.insertBatch",dto);
}
tmp = sqlMapClient.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}
return tmp;
}
public static void main(String arg[]) {
log.error(batch());
}
1:我的batch没有报异常,数据也插入数据库了,为什么返回值是0了,返回值应该是10呀(ibatis API 上说的)。