Search in sources :

Example 51 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement in project mybatis-3 by mybatis.

the class BaseExecutorTest method shouldSelectAuthorViaOutParams.

@Test
public void shouldSelectAuthorViaOutParams() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        MappedStatement selectStatement = ExecutorTestHelper.prepareSelectAuthorViaOutParams(config);
        Author author = new Author(102, null, null, null, null, null);
        executor.query(selectStatement, author, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        assertEquals("sally", author.getUsername());
        assertEquals("********", author.getPassword());
        assertEquals("sally@ibatis.apache.org", author.getEmail());
        assertEquals(null, author.getBio());
    } catch (ExecutorException e) {
        if (executor instanceof CachingExecutor) {
            // TODO see issue #464. Fail is OK.
            assertTrue(e.getMessage().contains("OUT params is not supported"));
        } else {
            throw e;
        }
    } 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 52 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement in project mybatis-3 by mybatis.

the class BaseExecutorTest method shouldInsertNewAuthorByProc.

@Test
public void shouldInsertNewAuthorByProc() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        Author author = new Author(97, "someone", "******", "someone@apache.org", null, null);
        MappedStatement insertStatement = ExecutorTestHelper.prepareInsertAuthorProc(config);
        MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
        int rows = executor.update(insertStatement, author);
        List<Author> authors = executor.query(selectStatement, 97, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        executor.flushStatements();
        executor.rollback(true);
        assertEquals(1, authors.size());
        assertEquals(author.toString(), authors.get(0).toString());
    } 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 53 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement in project mybatis-3 by mybatis.

the class BaseExecutorTest method shouldInsertNewAuthor.

@Test
public void shouldInsertNewAuthor() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        Author author = new Author(99, "someone", "******", "someone@apache.org", null, Section.NEWS);
        MappedStatement insertStatement = ExecutorTestHelper.prepareInsertAuthorMappedStatement(config);
        MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
        int rows = executor.update(insertStatement, author);
        List<Author> authors = executor.query(selectStatement, 99, 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 54 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement 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 55 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement 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)

Aggregations

MappedStatement (org.apache.ibatis.mapping.MappedStatement)73 Test (org.junit.Test)27 Author (org.apache.ibatis.domain.blog.Author)19 BaseDataTest (org.apache.ibatis.BaseDataTest)18 JdbcTransaction (org.apache.ibatis.transaction.jdbc.JdbcTransaction)18 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)17 Configuration (org.apache.ibatis.session.Configuration)17 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)17 ArrayList (java.util.ArrayList)15 ResultMap (org.apache.ibatis.mapping.ResultMap)12 BoundSql (org.apache.ibatis.mapping.BoundSql)11 Statement (java.sql.Statement)10 StatementHandler (org.apache.ibatis.executor.statement.StatementHandler)10 ParameterMap (org.apache.ibatis.mapping.ParameterMap)9 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)9 Section (org.apache.ibatis.domain.blog.Section)8 SqlSource (org.apache.ibatis.mapping.SqlSource)8 RowBounds (org.apache.ibatis.session.RowBounds)8 ResultMapping (org.apache.ibatis.mapping.ResultMapping)7 MetaObject (org.apache.ibatis.reflection.MetaObject)7