Search in sources :

Example 1 with JdbcTransaction

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

the class BaseExecutorTest method shouldFetchPostsForBlog.

@Test
void shouldFetchPostsForBlog() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        MappedStatement selectBlog = ExecutorTestHelper.prepareComplexSelectBlogMappedStatement(config);
        MappedStatement selectPosts = ExecutorTestHelper.prepareSelectPostsForBlogMappedStatement(config);
        config.addMappedStatement(selectBlog);
        config.addMappedStatement(selectPosts);
        List<Post> posts = executor.query(selectPosts, 1, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        executor.flushStatements();
        assertEquals(2, posts.size());
        assertTrue(posts.get(1) instanceof Proxy);
        assertNotNull(posts.get(1).getBlog());
        assertEquals(1, posts.get(1).getBlog().getId());
        executor.rollback(true);
    } finally {
        executor.rollback(true);
        executor.close(false);
    }
}
Also used : Proxy(javassist.util.proxy.Proxy) 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.jupiter.api.Test)

Example 2 with JdbcTransaction

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

the class BaseExecutorTest method shouldMapConstructorResults.

@Test
void shouldMapConstructorResults() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatementWithConstructorResults(config);
        List<Author> authors = executor.query(selectStatement, 102, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        executor.flushStatements();
        executor.rollback(true);
        assertEquals(1, authors.size());
        Author author = authors.get(0);
        assertEquals(102, author.getId());
    } 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.jupiter.api.Test)

Example 3 with JdbcTransaction

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

the class BaseExecutorTest method shouldSelectAllAuthorsAutoMapped.

@Test
void shouldSelectAllAuthorsAutoMapped() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        MappedStatement selectStatement = ExecutorTestHelper.prepareSelectAllAuthorsAutoMappedStatement(config);
        List<Author> authors = executor.query(selectStatement, null, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        assertEquals(2, authors.size());
        Author author = authors.get(0);
        // id,username, password, email, bio, favourite_section
        // (101,'jim','********','jim@ibatis.apache.org','','NEWS');
        assertEquals(101, author.getId());
        assertEquals("jim", author.getUsername());
        assertEquals("jim@ibatis.apache.org", author.getEmail());
        assertEquals("", author.getBio());
        assertEquals(Section.NEWS, author.getFavouriteSection());
    } 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.jupiter.api.Test)

Example 4 with JdbcTransaction

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

the class BaseExecutorTest method shouldInsertNewAuthorWithAutoKey.

@Test
void shouldInsertNewAuthorWithAutoKey() 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.prepareInsertAuthorMappedStatementWithAutoKey(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();
        }
        assertTrue(-1 != 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.jupiter.api.Test)

Example 5 with JdbcTransaction

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

the class BaseExecutorTest method shouldDeleteAuthor.

@Test
void shouldDeleteAuthor() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        Author author = new Author(101, null, null, null, null, null);
        MappedStatement deleteStatement = ExecutorTestHelper.prepareDeleteAuthorMappedStatement(config);
        MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
        int rows = executor.update(deleteStatement, author);
        List<Author> authors = executor.query(selectStatement, 101, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        executor.flushStatements();
        executor.rollback(true);
        assertEquals(0, authors.size());
        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.jupiter.api.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.jupiter.api.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