Search in sources :

Example 11 with JdbcTransaction

use of org.apache.ibatis.transaction.jdbc.JdbcTransaction in project mybatis-3 by mybatis.

the class BaseExecutorTest method shouldInsertNewAuthorWithBeforeAutoKey.

@Test
public void shouldInsertNewAuthorWithBeforeAutoKey() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        Author author = new Author(-1, "someone", "******", "someone@apache.org", null, Section.NEWS);
        MappedStatement insertStatement = ExecutorTestHelper.prepareInsertAuthorMappedStatementWithBeforeAutoKey(config);
        MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
        int rows = executor.update(insertStatement, author);
        assertTrue(rows > 0 || rows == BatchExecutor.BATCH_UPDATE_RETURN_VALUE);
        if (rows == BatchExecutor.BATCH_UPDATE_RETURN_VALUE) {
            executor.flushStatements();
        }
        assertEquals(123456, author.getId());
        if (author.getId() != BatchExecutor.BATCH_UPDATE_RETURN_VALUE) {
            List<Author> authors = executor.query(selectStatement, author.getId(), RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
            executor.rollback(true);
            assertEquals(1, authors.size());
            assertEquals(author.toString(), authors.get(0).toString());
            assertTrue(author.getId() >= 10000);
        }
    } finally {
        executor.rollback(true);
        executor.close(false);
    }
}
Also used : JdbcTransaction(org.apache.ibatis.transaction.jdbc.JdbcTransaction) Author(org.apache.ibatis.domain.blog.Author) MappedStatement(org.apache.ibatis.mapping.MappedStatement) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 12 with JdbcTransaction

use of org.apache.ibatis.transaction.jdbc.JdbcTransaction in project mybatis-3 by mybatis.

the class BaseExecutorTest method shouldSelectTwoSetsOfAuthorsViaProc.

@Test
public void shouldSelectTwoSetsOfAuthorsViaProc() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        MappedStatement selectStatement = ExecutorTestHelper.prepareSelectTwoSetsOfAuthorsProc(config);
        List<List<Author>> authorSets = executor.query(selectStatement, new HashMap<String, Object>() {

            {
                put("id1", 101);
                put("id2", 102);
            }
        }, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        assertEquals(2, authorSets.size());
        for (List<Author> authors : authorSets) {
            assertEquals(2, authors.size());
            for (Object author : authors) {
                assertTrue(author instanceof Author);
            }
        }
    } finally {
        executor.rollback(true);
        executor.close(false);
    }
}
Also used : JdbcTransaction(org.apache.ibatis.transaction.jdbc.JdbcTransaction) Author(org.apache.ibatis.domain.blog.Author) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 13 with JdbcTransaction

use of org.apache.ibatis.transaction.jdbc.JdbcTransaction in project mybatis-3 by mybatis.

the class BaseExecutorTest method shouldSelectDiscriminatedPost.

@Test
public void shouldSelectDiscriminatedPost() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        MappedStatement selectStatement = ExecutorTestHelper.prepareSelectDiscriminatedPost(config);
        List<Map<String, String>> products = executor.query(selectStatement, null, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        assertEquals(5, products.size());
        for (Map<String, String> m : products) {
            if ("IMAGES".equals(m.get("SECTION"))) {
                assertNull(m.get("subject"));
            } else {
                assertNotNull(m.get("subject"));
            }
        }
    } finally {
        executor.close(false);
    }
}
Also used : JdbcTransaction(org.apache.ibatis.transaction.jdbc.JdbcTransaction) MappedStatement(org.apache.ibatis.mapping.MappedStatement) HashMap(java.util.HashMap) Map(java.util.Map) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 14 with JdbcTransaction

use of org.apache.ibatis.transaction.jdbc.JdbcTransaction in project mybatis-3 by mybatis.

the class BaseExecutorTest method shouldUpdateAuthor.

@Test
public void shouldUpdateAuthor() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        Author author = new Author(101, "someone", "******", "someone@apache.org", null, Section.NEWS);
        MappedStatement updateStatement = ExecutorTestHelper.prepareUpdateAuthorMappedStatement(config);
        MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
        int rows = executor.update(updateStatement, author);
        List<Author> authors = executor.query(selectStatement, 101, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        executor.flushStatements();
        executor.rollback(true);
        assertEquals(1, authors.size());
        assertEquals(author.toString(), authors.get(0).toString());
        assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows);
    } finally {
        executor.rollback(true);
        executor.close(false);
    }
}
Also used : JdbcTransaction(org.apache.ibatis.transaction.jdbc.JdbcTransaction) Author(org.apache.ibatis.domain.blog.Author) MappedStatement(org.apache.ibatis.mapping.MappedStatement) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 15 with JdbcTransaction

use of org.apache.ibatis.transaction.jdbc.JdbcTransaction in project mybatis-3 by mybatis.

the class BaseExecutorTest method shouldFetchPostWithBlogWithCompositeKey.

@Test
public void shouldFetchPostWithBlogWithCompositeKey() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        MappedStatement selectBlog = ExecutorTestHelper.prepareSelectBlogByIdAndAuthor(config);
        MappedStatement selectPost = ExecutorTestHelper.prepareSelectPostWithBlogByAuthorMappedStatement(config);
        config.addMappedStatement(selectBlog);
        config.addMappedStatement(selectPost);
        List<Post> posts = executor.query(selectPost, 2, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        executor.flushStatements();
        assertEquals(1, posts.size());
        Post post = posts.get(0);
        assertNotNull(post.getBlog());
        assertEquals(101, post.getBlog().getAuthor().getId());
        executor.rollback(true);
    } finally {
        executor.rollback(true);
        executor.close(false);
    }
}
Also used : JdbcTransaction(org.apache.ibatis.transaction.jdbc.JdbcTransaction) Post(org.apache.ibatis.domain.blog.Post) MappedStatement(org.apache.ibatis.mapping.MappedStatement) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Aggregations

BaseDataTest (org.apache.ibatis.BaseDataTest)18 MappedStatement (org.apache.ibatis.mapping.MappedStatement)18 JdbcTransaction (org.apache.ibatis.transaction.jdbc.JdbcTransaction)18 Test (org.junit.Test)18 Author (org.apache.ibatis.domain.blog.Author)12 Post (org.apache.ibatis.domain.blog.Post)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 List (java.util.List)1 Proxy (javassist.util.proxy.Proxy)1 Blog (org.apache.ibatis.domain.blog.Blog)1 RowBounds (org.apache.ibatis.session.RowBounds)1