Search in sources :

Example 1 with Post

use of org.apache.ibatis.domain.blog.Post in project mybatis-3 by mybatis.

the class BindingTest method shouldfindThreeSpecificPosts.

@Test
public void shouldfindThreeSpecificPosts() throws Exception {
    SqlSession session = sqlSessionFactory.openSession();
    try {
        BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
        List<Post> posts = mapper.findThreeSpecificPosts(1, new RowBounds(1, 1), 3, 5);
        assertEquals(1, posts.size());
        assertEquals(3, posts.get(0).getId());
        session.rollback();
    } finally {
        session.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) DraftPost(org.apache.ibatis.domain.blog.DraftPost) Post(org.apache.ibatis.domain.blog.Post) RowBounds(org.apache.ibatis.session.RowBounds) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 2 with Post

use of org.apache.ibatis.domain.blog.Post in project mybatis-3 by mybatis.

the class BindingTest method shouldExecuteBoundSelectBlogUsingConstructorWithResultMapAndProperties.

@Test
void shouldExecuteBoundSelectBlogUsingConstructorWithResultMapAndProperties() {
    try (SqlSession session = sqlSessionFactory.openSession()) {
        BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
        Blog blog = mapper.selectBlogUsingConstructorWithResultMapAndProperties(1);
        assertEquals(1, blog.getId());
        assertEquals("Jim Business", blog.getTitle());
        assertNotNull(blog.getAuthor(), "author should not be null");
        Author author = blog.getAuthor();
        assertEquals(101, author.getId());
        assertEquals("jim@ibatis.apache.org", author.getEmail());
        assertEquals("jim", author.getUsername());
        assertEquals(Section.NEWS, author.getFavouriteSection());
        List<Post> posts = blog.getPosts();
        assertNotNull(posts, "posts should not be empty");
        assertEquals(2, posts.size());
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) DraftPost(org.apache.ibatis.domain.blog.DraftPost) Post(org.apache.ibatis.domain.blog.Post) Author(org.apache.ibatis.domain.blog.Author) Blog(org.apache.ibatis.domain.blog.Blog) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 3 with Post

use of org.apache.ibatis.domain.blog.Post in project mybatis-3 by mybatis.

the class ExecutorTestHelper method prepareSelectPostWithBlogByAuthorMappedStatement.

static MappedStatement prepareSelectPostWithBlogByAuthorMappedStatement(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final SqlSource sqlSource = new StaticSqlSource(config, "SELECT p.id, p.created_on, p.blog_id, p.author_id, p.section, p.subject, p.body, pt.tag_id," + " t.name as tag_name, c.id as comment_id, c.name as comment_name, c.comment" + " FROM post p" + " LEFT OUTER JOIN post_tag pt ON pt.post_id = p.id" + " LEFT OUTER JOIN tag t ON pt.tag_id = t.id" + " LEFT OUTER JOIN comment c ON c.post_id = p.id" + " WHERE p.id = ?");
    final ParameterMap parameterMap = new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build());
        }
    }).build();
    final ResultMap tagResultMap = new ResultMap.Builder(config, "tagResultMap", Tag.class, new ArrayList<ResultMapping>() {

        {
            add(new ResultMapping.Builder(config, "id", "tag_id", registry.getTypeHandler(int.class)).flags(new ArrayList<ResultFlag>() {

                {
                    add(ResultFlag.ID);
                }
            }).build());
            add(new ResultMapping.Builder(config, "name", "tag_name", registry.getTypeHandler(String.class)).build());
        }
    }).build();
    final ResultMap commentResultMap = new ResultMap.Builder(config, "commentResultMap", Comment.class, new ArrayList<ResultMapping>() {

        {
            add(new ResultMapping.Builder(config, "id", "comment_id", registry.getTypeHandler(int.class)).flags(new ArrayList<ResultFlag>() {

                {
                    add(ResultFlag.ID);
                }
            }).build());
            add(new ResultMapping.Builder(config, "name", "comment_name", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "comment", "comment", registry.getTypeHandler(String.class)).build());
        }
    }).build();
    config.addResultMap(tagResultMap);
    config.addResultMap(commentResultMap);
    final ResultMap postResultMap = new ResultMap.Builder(config, "postResultMap", Post.class, new ArrayList<ResultMapping>() {

        {
            add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).flags(new ArrayList<ResultFlag>() {

                {
                    add(ResultFlag.ID);
                }
            }).build());
            add(new ResultMapping.Builder(config, "blog").nestedQueryId("selectBlogByIdAndAuthor").composites(new ArrayList<ResultMapping>() {

                {
                    add(new ResultMapping.Builder(config, "authorId", "author_id", registry.getTypeHandler(int.class)).build());
                    add(new ResultMapping.Builder(config, "blogId", "blog_id", registry.getTypeHandler(int.class)).build());
                }
            }).build());
            add(new ResultMapping.Builder(config, "createdOn", "created_on", registry.getTypeHandler(Date.class)).build());
            add(new ResultMapping.Builder(config, "section", "section", registry.getTypeHandler(Section.class)).build());
            add(new ResultMapping.Builder(config, "subject", "subject", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "body", "body", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "tags").nestedResultMapId(tagResultMap.getId()).build());
            add(new ResultMapping.Builder(config, "comments").nestedResultMapId(commentResultMap.getId()).build());
        }
    }).build();
    return new MappedStatement.Builder(config, "selectPostsForBlog", sqlSource, SqlCommandType.SELECT).parameterMap(parameterMap).resultMaps(new ArrayList<ResultMap>() {

        {
            add(postResultMap);
        }
    }).build();
}
Also used : Comment(org.apache.ibatis.domain.blog.Comment) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) SqlSource(org.apache.ibatis.mapping.SqlSource) ResultMap(org.apache.ibatis.mapping.ResultMap) Post(org.apache.ibatis.domain.blog.Post) ArrayList(java.util.ArrayList) ParameterMap(org.apache.ibatis.mapping.ParameterMap) Section(org.apache.ibatis.domain.blog.Section) Date(java.util.Date) ResultMapping(org.apache.ibatis.mapping.ResultMapping) Author(org.apache.ibatis.domain.blog.Author) Tag(org.apache.ibatis.domain.blog.Tag) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultFlag(org.apache.ibatis.mapping.ResultFlag) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource)

Example 4 with Post

use of org.apache.ibatis.domain.blog.Post 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 5 with Post

use of org.apache.ibatis.domain.blog.Post in project mybatis-3 by mybatis.

the class SqlSessionTest method shouldSelectBlogWithPostsAndAuthorUsingJoin.

@Test
void shouldSelectBlogWithPostsAndAuthorUsingJoin() {
    try (SqlSession session = sqlMapper.openSession()) {
        Blog blog = session.selectOne("org.apache.ibatis.domain.blog.mappers.BlogMapper.selectBlogJoinedWithPostsAndAuthor", 1);
        assertEquals("Jim Business", blog.getTitle());
        final Author author = blog.getAuthor();
        assertEquals(101, author.getId());
        assertEquals("jim", author.getUsername());
        final List<Post> posts = blog.getPosts();
        assertEquals(2, posts.size());
        final Post post = blog.getPosts().get(0);
        assertEquals(1, post.getId());
        assertEquals("Corn nuts", post.getSubject());
        final List<Comment> comments = post.getComments();
        assertEquals(2, comments.size());
        final List<Tag> tags = post.getTags();
        assertEquals(3, tags.size());
        final Comment comment = comments.get(0);
        assertEquals(1, comment.getId());
        assertEquals(DraftPost.class, blog.getPosts().get(0).getClass());
        assertEquals(Post.class, blog.getPosts().get(1).getClass());
    }
}
Also used : Comment(org.apache.ibatis.domain.blog.Comment) DraftPost(org.apache.ibatis.domain.blog.DraftPost) Post(org.apache.ibatis.domain.blog.Post) ImmutableAuthor(org.apache.ibatis.domain.blog.ImmutableAuthor) Author(org.apache.ibatis.domain.blog.Author) Tag(org.apache.ibatis.domain.blog.Tag) Blog(org.apache.ibatis.domain.blog.Blog) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Aggregations

Post (org.apache.ibatis.domain.blog.Post)21 BaseDataTest (org.apache.ibatis.BaseDataTest)18 Test (org.junit.jupiter.api.Test)17 DraftPost (org.apache.ibatis.domain.blog.DraftPost)15 SqlSession (org.apache.ibatis.session.SqlSession)13 Blog (org.apache.ibatis.domain.blog.Blog)9 Author (org.apache.ibatis.domain.blog.Author)6 MappedStatement (org.apache.ibatis.mapping.MappedStatement)6 Comment (org.apache.ibatis.domain.blog.Comment)5 Tag (org.apache.ibatis.domain.blog.Tag)5 RowBounds (org.apache.ibatis.session.RowBounds)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)3 Section (org.apache.ibatis.domain.blog.Section)3 ParameterMap (org.apache.ibatis.mapping.ParameterMap)3 ResultFlag (org.apache.ibatis.mapping.ResultFlag)3 ResultMap (org.apache.ibatis.mapping.ResultMap)3 ResultMapping (org.apache.ibatis.mapping.ResultMapping)3 SqlSource (org.apache.ibatis.mapping.SqlSource)3