Example usage for org.apache.ibatis.session SqlSession flushStatements

List of usage examples for org.apache.ibatis.session SqlSession flushStatements

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession flushStatements.

Prototype

List<BatchResult> flushStatements();

Source Link

Document

Flushes batch statements.

Usage

From source file:com.baomidou.framework.service.impl.ServiceImpl.java

License:Apache License

/**
 * ??/* w w w.  java 2  s .co  m*/
 *
 * @param entityList
 * @param batchSize
 * @return
 */
public boolean insertBatch(List<T> entityList, int batchSize) {
    if (CollectionUtil.isEmpty(entityList)) {
        throw new IllegalArgumentException("Error: entityList must not be empty");
    }
    TableInfo tableInfo = TableInfoHelper.getTableInfo(currentModleClass());
    if (null == tableInfo) {
        throw new MybatisPlusException("Error: Cannot execute insertBatch Method, ClassGenricType not found .");
    }
    SqlSession batchSqlSession = tableInfo.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
    try {
        int size = entityList.size();
        for (int i = 0; i < size; i++) {
            baseMapper.insert(entityList.get(i));
            if (i % batchSize == 0) {
                batchSqlSession.flushStatements();
            }
        }
        batchSqlSession.flushStatements();
    } catch (Exception e) {
        logger.warning("Error: Cannot execute insertBatch Method. Cause:" + e);
        return false;
    }
    return true;

}

From source file:com.beginner.core.dao.DaoSupport.java

License:Apache License

/** 
 * {@inheritDoc}/*from w  ww  .j  ava 2 s.  c om*/
 */
@Override
public void batchSave(String str, List objs) throws Exception {
    SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
    //?
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
    try {
        if (objs != null) {
            for (int i = 0, size = objs.size(); i < size; i++) {
                sqlSession.insert(str, objs.get(i));
            }
            sqlSession.flushStatements();
            sqlSession.commit();
            sqlSession.clearCache();
        }
    } finally {
        sqlSession.close();
    }
}

From source file:com.beginner.core.dao.DaoSupport.java

License:Apache License

/** 
 * {@inheritDoc}//from www . j a  v  a2s  . c  o m
 */
@Override
public void batchUpdate(String str, List objs) throws Exception {
    SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
    //?
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
    try {
        if (objs != null) {
            for (int i = 0, size = objs.size(); i < size; i++) {
                sqlSession.update(str, objs.get(i));
            }
            sqlSession.flushStatements();
            sqlSession.commit();
            sqlSession.clearCache();
        }
    } finally {
        sqlSession.close();
    }
}

From source file:com.beginner.core.dao.DaoSupport.java

License:Apache License

/** 
 * {@inheritDoc}/*from w  w w  .j a  v  a  2  s  .c  o  m*/
 */
@Override
public void batchDelete(String str, List objs) throws Exception {
    SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
    //?
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
    try {
        if (objs != null) {
            for (int i = 0, size = objs.size(); i < size; i++) {
                sqlSession.delete(str, objs.get(i));
            }
            sqlSession.flushStatements();
            sqlSession.commit();
            sqlSession.clearCache();
        }
    } finally {
        sqlSession.close();
    }
}

From source file:com.gf.components.mybatis.SqlSessionInInvoke.java

License:Apache License

@Override
public void invoke(Action<?, ?> action, FilterChain chain) throws Exception {

    SqlSessionSettings settings = findSettingsInNextObjects();
    SqlSession session = createSession(settings);
    try {/*from w  w  w  .j ava  2  s  .com*/

        Connection connection = session.getConnection();

        addToInvocationContext(session);
        addToInvocationContext(connection);
        chain.doNext();

        session.flushStatements();
        session.commit();

    } catch (Exception e) {
        session.rollback();
        throw e;
    } finally {
        try {
            session.close();
        } catch (Exception ex) {
            log.error("can't close session", ex);
        }
    }

}

From source file:com.luxoft.mybatis.splitter.UpdateSplitterPluginTest.java

License:Apache License

public void splitterTest(ExecutorType execType) throws IOException, SQLException {
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
            .build(Resources.getResourceAsStream("configuration.xml"));
    SqlSession sqlSession = sqlSessionFactory.openSession(execType);
    sqlSession.insert("makeTable");
    sqlSession.flushStatements();
    doInsert(sqlSession);//from  ww  w  . j  a  v a  2s .c  o m
    Assert.assertEquals(Arrays.asList("first", "second", "third"), sqlSession.selectList("get"));
    sqlSession.insert("dropTable");
    sqlSession.flushStatements();
    sqlSession.close();
}

From source file:com.luxoft.mybatis.splitter.UpdateSplitterPluginTest.java

License:Apache License

private void doInsert(SqlSession sqlSession) {
    Map<String, Object> param = new HashMap<String, Object>();
    param.put("first", "first");
    param.put("other", Arrays.asList("second", "third"));
    sqlSession.insert("multy", param);
    sqlSession.flushStatements();
}

From source file:com.mmj.app.biz.base.BaseDao.java

License:Open Source License

private Integer batchOptByMapper(Collection<T> objList, String methodName) {
    SqlSession session = getSqlSession();
    Class[] paramTypes = new Class[1];
    M mapper = (M) session.getMapper(mapperClass);

    try {/*from  ww  w .  j  av a  2s.  c o  m*/
        paramTypes[0] = Class.forName(entityClass.getName());
        Method method = mapperClass.getMethod(methodName, Serializable.class);
        for (Object obj : objList) {
            method.invoke(mapper, obj);
        }
    } catch (Exception e) {
        log.error("BaseDao batchOptByMapper:error()");
    }
    session.flushStatements();
    return new Integer(1);
}

From source file:com.raycloud.cobarclient.mybatis.spring.MySqlSessionTemplate.java

License:Apache License

/**
 * ?????// ww  w . j  av  a2s.co  m
 *
 * @param statement
 * @param collection
 * @param <T>
 * @return
 */
private <T extends Object> int batchSync(final String statement, Collection<T> collection) {
    Map<Shard, List<T>> classifiedEntities = classify(statement, collection);
    final MultipleCauseException throwables = new MultipleCauseException();
    int counter = 0;
    for (final Map.Entry<Shard, List<T>> entry : classifiedEntities.entrySet()) {
        Environment environment = environmentMap.get(entry.getKey().getId());
        //??
        final SqlSession sqlSession = SqlSessionUtils.getSqlSession(MySqlSessionTemplate.this.sqlSessionFactory,
                ExecutorType.BATCH, MySqlSessionTemplate.this.exceptionTranslator, environment);
        try {
            for (T item : entry.getValue()) {
                sqlSession.update(statement, item);
            }
            List<BatchResult> results = sqlSession.flushStatements();
            int[] updateCounts = results.get(0).getUpdateCounts();
            for (int i = 0; i < updateCounts.length; i++) {
                int value = updateCounts[i];
                counter += value;
            }
        } catch (Throwable e) {
            Throwable unwrapped = unwrapThrowable(e);
            if (MySqlSessionTemplate.this.exceptionTranslator != null
                    && unwrapped instanceof PersistenceException) {
                Throwable translated = MySqlSessionTemplate.this.exceptionTranslator
                        .translateExceptionIfPossible((PersistenceException) unwrapped);
                if (translated != null) {
                    unwrapped = translated;
                }
            }
            throwables.add(unwrapped);
        } finally {
            SqlSessionUtils.closeSqlSession(sqlSession, MySqlSessionTemplate.this.sqlSessionFactory);
        }
    }
    if (!throwables.getCauses().isEmpty()) {
        throw new TransientDataAccessResourceException(
                "one or more errors when performing data access operations  against multiple shards",
                throwables);
    }
    return counter;
}

From source file:org.mybatis.guice.session.DbSessionManager.java

License:Apache License

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();
}