Search in sources :

Example 1 with BatchResult

use of org.apache.ibatis.executor.BatchResult in project mybatis-3 by mybatis.

the class FlushTest method invokeFlushStatementsViaMapper.

@Test
public void invokeFlushStatementsViaMapper() {
    SqlSession session = sqlSessionFactory.openSession();
    try {
        BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
        Author author = new Author(-1, "cbegin", "******", "cbegin@nowhere.com", "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();
    }
}
Also used : ArrayList(java.util.ArrayList) Author(org.apache.ibatis.domain.blog.Author) BatchResult(org.apache.ibatis.executor.BatchResult) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 2 with BatchResult

use of org.apache.ibatis.executor.BatchResult in project mybatis-3 by mybatis.

the class InsertTest method testInsertMappedBatch.

// Not supported yet in PostgreSQL
@Ignore
@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();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) BatchResult(org.apache.ibatis.executor.BatchResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with BatchResult

use of org.apache.ibatis.executor.BatchResult in project mybatis-3 by mybatis.

the class NoParamTypeTest method shouldAcceptDifferentTypeInTheSameBatch.

@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();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) BatchResult(org.apache.ibatis.executor.BatchResult) Test(org.junit.Test)

Aggregations

BatchResult (org.apache.ibatis.executor.BatchResult)3 Test (org.junit.Test)3 SqlSession (org.apache.ibatis.session.SqlSession)2 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 BaseDataTest (org.apache.ibatis.BaseDataTest)1 Author (org.apache.ibatis.domain.blog.Author)1 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)1 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)1 Ignore (org.junit.Ignore)1