本文整理汇总了Java中org.apache.ibatis.executor.BatchResult类的典型用法代码示例。如果您正苦于以下问题:Java BatchResult类的具体用法?Java BatchResult怎么用?Java BatchResult使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BatchResult类属于org.apache.ibatis.executor包,在下文中一共展示了BatchResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: batchInsert
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
protected int batchInsert(List<T> insertObjects , String statement , Integer batchSize){
SqlSession sqlSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
if (batchSize == null || batchSize == 0){
batchSize = BATCH_SIZE ;
}
int execCount = 0;
for (int i = 0 , size = insertObjects.size(); i < size ; i++){
sqlSession.insert(statement , insertObjects);
if ((i != 0 && i % batchSize == 0) || i == size -1){
List<BatchResult> batchResults = sqlSession.flushStatements();
execCount = batchResults.size() ;
sqlSession.commit();
sqlSession.clearCache();
LOGGER.debug("batch insert , current commit size : {} " , execCount );
}
}
sqlSession.close();
return execCount ;
}
示例2: shouldAcceptDifferentTypeInTheSameBatch
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
@Test
public void shouldAcceptDifferentTypeInTheSameBatch() {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
ObjA a = new ObjA();
a.setId(1);
a.setName(111);
sqlSession.insert("insertUser", a);
ObjB b = new ObjB();
b.setId(2);
b.setName("222");
sqlSession.insert("insertUser", b);
List<BatchResult> batchResults = sqlSession.flushStatements();
batchResults.clear();
sqlSession.clearCache();
sqlSession.commit();
List<User> users = sqlSession.selectList("selectUser");
assertEquals(2, users.size());
} finally {
sqlSession.close();
}
}
示例3: testInsertMappedBatch
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
@Ignore // Not supported yet in PostgreSQL
@Test
public void testInsertMappedBatch() throws Exception {
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
Name name = new Name();
name.setFirstName("Fred");
name.setLastName("Flintstone");
mapper.insertNameMapped(name);
Name name2 = new Name();
name2.setFirstName("Wilma");
name2.setLastName("Flintstone");
mapper.insertNameMapped(name2);
List<BatchResult> batchResults = sqlSession.flushStatements();
assertNotNull(name.getId());
assertNotNull(name2.getId());
assertEquals(1, batchResults.size());
} finally {
sqlSession.close();
}
}
示例4: write
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
public void write(final List<? extends T> items) {
if (!items.isEmpty()) {
if (logger.isDebugEnabled()) {
logger.debug("Executing batch with " + items.size() + " items.");
}
for (T item : items) {
sqlSessionTemplate.update(statementId, item);
}
List<BatchResult> results = sqlSessionTemplate.flushStatements();
if (assertUpdates) {
if (results.size() != 1) {
throw new InvalidDataAccessResourceUsageException("Batch execution returned invalid results. " +
"Expected 1 but number of BatchResult objects returned was " + results.size());
}
int[] updateCounts = results.get(0).getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++) {
int value = updateCounts[i];
if (value == 0) {
throw new EmptyResultDataAccessException("Item " + i + " of " + updateCounts.length
+ " did not update any rows: [" + items.get(i) + "]", 1);
}
}
}
}
}
示例5: batchUpdate
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
protected int batchUpdate(List<T> updateObjects , String statement , Integer batchSize){
SqlSession sqlSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH , TransactionIsolationLevel.NONE);
if (batchSize == null || batchSize == 0){
batchSize = BATCH_SIZE ;
}
int execCount = 0;
for (int i = 0 , size = updateObjects.size(); i < size ; i++){
sqlSession.update(statement, updateObjects);
if ((i != 0 && i % batchSize == 0) || i == size -1){
List<BatchResult> batchResults = sqlSession.flushStatements();
execCount = batchResults.size() ;
sqlSession.commit();
sqlSession.clearCache();
LOGGER.debug("batch insert , current commit size : {} " , execCount );
}
}
sqlSession.close();
return execCount;
}
示例6: batchDelete
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
protected int batchDelete(List<T> delObjects , String statement , Integer batchSize){
SqlSession sqlSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH , TransactionIsolationLevel.NONE);
if (batchSize == null || batchSize == 0){
batchSize = BATCH_SIZE ;
}
int execCount = 0;
for (int i = 0 , size = delObjects.size(); i < size ; i++){
sqlSession.delete(statement, delObjects);
if ((i != 0 && i % batchSize == 0) || i == size -1){
List<BatchResult> batchResults = sqlSession.flushStatements();
execCount = batchResults.size() ;
sqlSession.commit();
sqlSession.clearCache();
LOGGER.debug("batch insert , current commit size : {} " , execCount );
}
}
sqlSession.close();
return execCount;
}
示例7: testInsertMappedBatch
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
@Test
public void testInsertMappedBatch() throws Exception {
try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
Name name = new Name();
name.setFirstName("Fred");
name.setLastName("Flintstone");
mapper.insertNameMapped(name);
Name name2 = new Name();
name2.setFirstName("Wilma");
name2.setLastName("Flintstone");
mapper.insertNameMapped(name2);
List<BatchResult> batchResults = sqlSession.flushStatements();
assertNotNull(name.getId());
assertNotNull(name2.getId());
assertEquals(1, batchResults.size());
}
}
示例8: flushStatements
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
@Override
public List<BatchResult> flushStatements() {
try {
return executor.flushStatements();
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error flushing statements. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
示例9: flushStatements
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
@Override
public List<BatchResult> flushStatements() {
final SqlSession sqlSession = localSqlSession.get();
if (sqlSession == null) {
throw new SqlSessionException("Error: Cannot rollback. No managed session is started.");
}
return sqlSession.flushStatements();
}
示例10: invokeFlushStatementsViaMapper
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
@Test
public void invokeFlushStatementsViaMapper() {
SqlSession session = sqlSessionFactory.openSession();
try {
BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
Author author = new Author(-1, "cbegin", "******", "[email protected]", "N/A", Section.NEWS);
List<Integer> ids = new ArrayList<Integer>();
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
// test
List<BatchResult> results = mapper.flush();
assertThat(results.size(), is(1));
assertThat(results.get(0).getUpdateCounts().length, is(ids.size()));
for (int id : ids) {
Author selectedAuthor = mapper.selectAuthor(id);
assertNotNull(id + " is not found.", selectedAuthor);
}
session.rollback();
} finally {
session.close();
}
}
示例11: flushStatements
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
@Override
public List<BatchResult> flushStatements() {
try {
return executor.flushStatements();
} catch (Exception e) {
throw SQLExceptionFactory.wrapException("Error flushing statements. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
示例12: flushStatements
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
@Override
public List<BatchResult> flushStatements() {
try {
//转而用执行器来flushStatements
return executor.flushStatements();
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error flushing statements. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
示例13: checkFlushResults
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
protected void checkFlushResults(List<DbOperation> operationsToFlush, List<BatchResult> flushResult) {
int flushResultSize = 0;
if (flushResult != null && flushResult.size() > 0) {
LOG.printBatchResults(flushResult);
//process the batch results to handle Optimistic Lock Exceptions
Iterator<DbOperation> operationIt = operationsToFlush.iterator();
for (BatchResult batchResult : flushResult) {
for (int statementResult : batchResult.getUpdateCounts()) {
flushResultSize++;
DbOperation thisOperation = operationIt.next();
if (thisOperation instanceof DbEntityOperation && ((DbEntityOperation) thisOperation).getEntity() instanceof HasDbRevision
&& !thisOperation.getOperationType().equals(DbOperationType.INSERT)) {
final DbEntity dbEntity = ((DbEntityOperation) thisOperation).getEntity();
if (statementResult != 1) {
((DbEntityOperation) thisOperation).setFailed(true);
handleOptimisticLockingException(thisOperation);
} else {
//update revision number in cache
if (thisOperation.getOperationType().equals(DbOperationType.UPDATE)) {
HasDbRevision versionedObject = (HasDbRevision) dbEntity;
versionedObject.setRevision(versionedObject.getRevisionNext());
}
}
}
}
}
//this must not happen, but worth checking
if (operationsToFlush.size() != flushResultSize) {
LOG.wrongBatchResultsSizeException(operationsToFlush);
}
}
}
示例14: printBatchResults
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
public void printBatchResults(List<BatchResult> results) {
if (results.size() > 0) {
StringBuilder sb = new StringBuilder();
sb.append("Batch summary:\n");
for (int i = 0; i < results.size(); i++) {
BatchResult result = results.get(i);
sb.append("Result ").append(i).append(":\t");
sb.append(result.getSql().replaceAll("\n", "").replaceAll("\\s+", " ")).append("\t");
sb.append("Update counts: ").append(Arrays.toString(result.getUpdateCounts())).append("\n");
}
logDebug("082", sb.toString());
}
}
示例15: invokeFlushStatementsViaMapper
import org.apache.ibatis.executor.BatchResult; //导入依赖的package包/类
@Test
public void invokeFlushStatementsViaMapper() {
SqlSession session = sqlSessionFactory.openSession();
try {
BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
Author author = new Author(-1, "cbegin", "******", "[email protected]", "N/A", Section.NEWS);
List<Integer> ids = new ArrayList<Integer>();
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
mapper.insertAuthor(author);
ids.add(author.getId());
// test
List<BatchResult> results = mapper.flush();
assertThat(results.size()).isEqualTo(1);
assertThat(results.get(0).getUpdateCounts().length).isEqualTo(ids.size());
for (int id : ids) {
Author selectedAuthor = mapper.selectAuthor(id);
assertNotNull(id + " is not found.", selectedAuthor);
}
session.rollback();
} finally {
session.close();
}
}