mybatis中實現批量的增刪改
阿新 • • 發佈:2018-12-15
//批量插入 public void batchInsert(List<UserDo> list) { this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { executor.startBatch(); for (UserDo parameterObject : list) { executor.insert("MS-INSERT", parameterObject); } executor.executeBatch(); return null; } }); } public void batchInsertUserDoList(List<String> userNameList) { this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { executor.startBatch(); for (String str : userNameList) { UserDo userDo = new UserDo(); userDo.setUserName(str); executor.insert("MS-INSERT"", userDo); } executor.executeBatch(); return null; } }); } MS-INSERT 表示單條插入 /** * 批量更新 */ @Override public void batchUpdate(List<UserDo> list) { this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { executor.startBatch(); for (UserDo parameterObject : list) { executor.update("MS-UPDATE", parameterObject); } executor.executeBatch(); return null; } }); } MS-UPDATE 表示單條更新的sql public void batchDeleteUserList(List<String> ids) { this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { executor.startBatch(); for (String str : ids) { Map param = new HashMap(); param.put("id", str); executor.delete("MS-DELETE", param); } executor.executeBatch(); return null; } }); } MS-DELET 表示單條刪除 如何在業務層使用,需要是spring 的事務中使用 TransactionTemplate.execute(new TransactionCallback<String>() { @Override public String doInTransaction(TransactionStatus status) { 批量程式碼呼叫 } }