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

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

Introduction

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

Prototype

void commit();

Source Link

Document

Flushes batch statements and commits database connection.

Usage

From source file:com.baomidou.mybatisplus.test.oracle.TestUserMapperTest.java

License:Apache License

/**
 *
 * RUN   MySql  //from   www .  j  av a 2 s  . c  om
 *
 */
public static void main(String[] args) {

    //?
    InputStream in = TestUserMapperTest.class.getClassLoader().getResourceAsStream("oracle-config.xml");

    /*
       *  MybatisSessionFactoryBuilder 
     * SqlSessionFactoryBaseMapper
     */
    MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();

    /** ? oracle */
    GlobalConfiguration gc = new GlobalConfiguration();
    gc.setDbType("oracle");
    mf.setGlobalConfig(gc);

    /*
     * 1??????
     * 2?????
     * 3?????
     *    ?? @TableId(value = "test_id") 
     */
    //mf.setDbColumnUnderline(true);

    SqlSessionFactory sessionFactory = mf.build(in);
    SqlSession session = sessionFactory.openSession();
    TestUserMapper testUserMapper = session.getMapper(TestUserMapper.class);
    System.err.println(" debug run  test_user ??? ");
    session.delete("deleteAll");

    /**
     * ?
     */
    int rlt = testUserMapper.insert(new TestUser("10", "abc", 18, 1));
    System.err.println("\n--------------insert-------" + rlt);
    sleep();

    /**
     * ??
     */
    List<TestUser> ul = new ArrayList<>();
    ul.add(new TestUser("11", "1a", 11, 1));
    ul.add(new TestUser("12", "2a", 12, 2));
    ul.add(new TestUser("a", 1, 1));
    ul.add(new TestUser("b", 2, 2));
    ul.add(new TestUser("c", 3, 1));
    for (TestUser u : ul) {
        rlt = testUserMapper.insert(u);
    }
    System.err.println("\n--------------insertBatch-------" + rlt);
    sleep();

    /**
     * ?
     */
    List<TestUser> ul1 = new ArrayList<>();
    ul1.add(new TestUser("10", "update-0a", 11, 1));
    ul1.add(new TestUser("11", "update-1a", 11, 1));
    ul1.add(new TestUser("12", "update-2a", 12, 2));
    for (TestUser u : ul1) {
        rlt = testUserMapper.updateById(u);
    }
    System.err.println("\n--------------updateBatchById-------" + rlt);
    sleep();

    System.err.println(
            "\n------------------list  ---- testType = 1 ?--id--DESC--?--------");
    Page<TestUser> page = new Page<>(1, 2);
    EntityWrapper<TestUser> ew = new EntityWrapper<>(new TestUser(1), "TEST_ID DESC");
    List<TestUser> paginList = testUserMapper.selectPage(page, ew);
    page.setRecords(paginList);
    for (int i = 0; i < page.getRecords().size(); i++) {
        print(page.getRecords().get(i));
    }
    System.err.println(" " + page.toString());

    /* ?  */
    rlt = session.delete("deleteAll");
    System.err.println("?? rlt=" + rlt);

    /**
     * ??
     */
    session.commit();
}

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

License:Apache License

/** 
 * {@inheritDoc}/*from   www  .ja v a 2  s  .co  m*/
 */
@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}// w  w w  .j  a v  a  2s. 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}/* w ww  .j ava2 s  .com*/
 */
@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.bibisco.manager.ArchitectureItemManager.java

License:GNU General Public License

public static void save(ArchitectureItem pArchitectureItem, ArchitectureItemType pArchitectureItemType) {

    mLog.debug("Start save(ArchitectureItem, ArchitectureItemType)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {//  w  ww . j av  a2 s.c o m
        ProjectMapper lProjectMapper = lSqlSession.getMapper(ProjectMapper.class);
        ProjectWithBLOBs lProjectWithBLOBs = lProjectMapper
                .selectByPrimaryKey(ContextManager.getInstance().getIdProject());

        switch (pArchitectureItemType) {
        case FABULA:
            lProjectWithBLOBs.setFabulaTaskStatus(pArchitectureItem.getTaskStatus().getValue());
            lProjectWithBLOBs.setFabula(pArchitectureItem.getText());
            break;

        case PREMISE:
            lProjectWithBLOBs.setPremiseTaskStatus(pArchitectureItem.getTaskStatus().getValue());
            lProjectWithBLOBs.setPremise(pArchitectureItem.getText());
            break;

        case SETTING:
            lProjectWithBLOBs.setSettingTaskStatus(pArchitectureItem.getTaskStatus().getValue());
            lProjectWithBLOBs.setSetting(pArchitectureItem.getText());
            break;
        default:
            break;
        }

        lProjectMapper.updateByPrimaryKeyWithBLOBs(lProjectWithBLOBs);
        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End save(ArchitectureItem, ArchitectureItemType)");

}

From source file:com.bibisco.manager.ChapterManager.java

License:GNU General Public License

public static ChapterDTO insert(ChapterDTO pChapterDTO) {

    mLog.debug("Start insert(Chapter)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {// w  w w  . ja  v a2  s  . c  om
        ChaptersWithBLOBs lChapters = new ChaptersWithBLOBs();
        lChapters.setTitle(pChapterDTO.getTitle());
        lChapters.setPosition(pChapterDTO.getPosition());
        lChapters.setReasonTaskStatus(TaskStatus.TODO.getValue());

        ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class);
        lChaptersMapper.insert(lChapters);

        pChapterDTO.setIdChapter(lChapters.getIdChapter().intValue());
        pChapterDTO.setTaskStatus(TaskStatus.TODO);

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End insert(Chapter)");

    return pChapterDTO;
}

From source file:com.bibisco.manager.ChapterManager.java

License:GNU General Public License

public static void save(ChapterDTO pChapterDTO) {

    mLog.debug("Start save(ChapterDTO)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*from   w  w  w  .  j  av  a  2 s. c  o m*/

        ChaptersWithBLOBs lChapters = new ChaptersWithBLOBs();
        lChapters.setIdChapter(pChapterDTO.getIdChapter().longValue());
        lChapters.setPosition(pChapterDTO.getPosition());
        lChapters.setTitle(pChapterDTO.getTitle());
        lChapters.setReasonTaskStatus(
                pChapterDTO.getReasonTaskStatus() != null ? pChapterDTO.getReasonTaskStatus().getValue()
                        : null);
        lChapters.setReason(pChapterDTO.getReason());
        lChapters.setNote(pChapterDTO.getNote());

        ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class);
        lChaptersMapper.updateByPrimaryKeySelective(lChapters);

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End save(ChapterDTO)");
}

From source file:com.bibisco.manager.ChapterManager.java

License:GNU General Public License

public static void deleteByPosition(Integer pIntIdPosition) {

    mLog.debug("Start deleteByPosition(Integer)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*from w  w  w .j  a va 2s .co  m*/
        // delete scene revisions
        SceneRevisionsMapper lSceneRevisionsMapper = lSqlSession.getMapper(SceneRevisionsMapper.class);
        lSceneRevisionsMapper.deleteByChapterPosition(pIntIdPosition);

        // delete scenes
        ScenesMapper lScenesMapper = lSqlSession.getMapper(ScenesMapper.class);
        lScenesMapper.deleteByChapterPosition(pIntIdPosition);

        // delete chapter
        ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class);
        lChaptersMapper.deleteByPosition(pIntIdPosition);

        // shift down other chapters
        lChaptersMapper.shiftDown(pIntIdPosition, Integer.MAX_VALUE);

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End deleteByPosition(Integer)");

}

From source file:com.bibisco.manager.ChapterManager.java

License:GNU General Public License

public static void move(Integer pIntSourcePosition, Integer pIntDestPosition) {

    mLog.debug("Start move(Integer, Integer)");

    if (!pIntSourcePosition.equals(pIntDestPosition)) {
        SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance()
                .getSqlSessionFactoryProject();
        SqlSession lSqlSession = lSqlSessionFactory.openSession();
        try {/*from ww w  .j a  v  a  2  s .  c om*/

            ChaptersExample lChaptersExample = new ChaptersExample();
            lChaptersExample.createCriteria().andPositionEqualTo(pIntSourcePosition);

            ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class);

            // get chapter to update
            Chapters lChapters = lChaptersMapper.selectByExample(lChaptersExample).get(0);

            // update chapter position with fake position to preserve unique index before shift
            lChapters.setPosition(-1);
            lChaptersMapper.updateByPrimaryKey(lChapters);

            // update other chapters' position
            Integer lIntStartPosition;
            Integer lIntEndPosition;
            if (pIntSourcePosition > pIntDestPosition) {
                lIntStartPosition = pIntDestPosition;
                lIntEndPosition = pIntSourcePosition;
                lChaptersMapper.shiftUp(lIntStartPosition, lIntEndPosition);
            } else {
                lIntStartPosition = pIntSourcePosition;
                lIntEndPosition = pIntDestPosition;
                lChaptersMapper.shiftDown(lIntStartPosition, lIntEndPosition);
            }

            // update chapter position
            lChapters.setPosition(pIntDestPosition);
            lChaptersMapper.updateByPrimaryKey(lChapters);

            lSqlSession.commit();

        } catch (Throwable t) {
            mLog.error(t);
            lSqlSession.rollback();
            throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
        } finally {
            lSqlSession.close();
        }
    }

    mLog.debug("End move(Integer, Integer)");
}

From source file:com.bibisco.manager.CharacterManager.java

License:GNU General Public License

public static CharacterDTO insert(CharacterDTO pCharacterDTO) {

    mLog.debug("Start insert(CharacterDTO)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*from w ww .j a  v a 2s  .  co  m*/

        CharactersWithBLOBs lCharacters = new CharactersWithBLOBs();
        lCharacters.setMainCharacter(pCharacterDTO.isMainCharacter() ? "Y" : "N");
        lCharacters.setName(pCharacterDTO.getName());
        lCharacters.setPosition(pCharacterDTO.getPosition());

        CharactersMapper lCharactersMapper = lSqlSession.getMapper(CharactersMapper.class);
        lCharactersMapper.insertSelective(lCharacters);

        pCharacterDTO = createCharacterDTOFromCharacters(lCharacters);
        pCharacterDTO.setTaskStatus(TaskStatus.TODO);

        if (pCharacterDTO.isMainCharacter()) {
            // populate character info
            CharacterInfosMapper lCharacterInfosMapper = lSqlSession.getMapper(CharacterInfosMapper.class);
            for (CharacterInfoQuestions lCharacterInfoQuestions : CharacterInfoQuestions.values()) {
                for (int i = 0; i < lCharacterInfoQuestions.getTotalQuestions(); i++) {
                    CharacterInfos lCharacterInfos = new CharacterInfos();
                    lCharacterInfos.setCharacterInfoType(lCharacterInfoQuestions.name());
                    lCharacterInfos.setIdCharacter(lCharacters.getIdCharacter().intValue());
                    lCharacterInfos.setQuestion(i + 1);
                    lCharacterInfosMapper.insert(lCharacterInfos);
                }
            }
        }

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End insert(CharacterDTO)");

    return pCharacterDTO;
}