Search in sources :

Example 6 with Blog

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

the class ExecutorTestHelper method prepareSelectBlogByIdAndAuthor.

static MappedStatement prepareSelectBlogByIdAndAuthor(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final SqlSource sqlSource = new StaticSqlSource(config, "SELECT b.id, b.author_id, b.title, a.username, a.password, a.email, a.bio" + " FROM blog b" + " INNER JOIN author a ON b.author_id = a.id" + " WHERE b.id = ? and a.id = ?");
    final ParameterMap parameterMap = new ParameterMap.Builder(config, "defaultParameterMap", Map.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "blogId", registry.getTypeHandler(int.class)).build());
            add(new ParameterMapping.Builder(config, "authorId", registry.getTypeHandler(int.class)).build());
        }
    }).build();
    final ResultMap resultMap = new ResultMap.Builder(config, "defaultResultMap", Blog.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, "title", "title", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "author.id", "author_id", registry.getTypeHandler(int.class)).build());
            add(new ResultMapping.Builder(config, "author.username", "username", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "author.password", "password", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "author.email", "email", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "author.bio", "bio", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "posts", "id", registry.getTypeHandler(int.class)).javaType(List.class).nestedQueryId("selectPostsForBlog").build());
        }
    }).build();
    return new MappedStatement.Builder(config, "selectBlogByIdAndAuthor", sqlSource, SqlCommandType.SELECT).parameterMap(parameterMap).resultMaps(new ArrayList<ResultMap>() {

        {
            add(resultMap);
        }
    }).build();
}
Also used : 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) ArrayList(java.util.ArrayList) ParameterMap(org.apache.ibatis.mapping.ParameterMap) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultFlag(org.apache.ibatis.mapping.ResultFlag) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) HashMap(java.util.HashMap) Map(java.util.Map) ParameterMap(org.apache.ibatis.mapping.ParameterMap) ResultMap(org.apache.ibatis.mapping.ResultMap) Blog(org.apache.ibatis.domain.blog.Blog)

Example 7 with Blog

use of org.apache.ibatis.domain.blog.Blog 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)

Example 8 with Blog

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

the class SqlSessionTest method shouldSelectNestedBlogWithPostsAndAuthorUsingJoin.

@Test
void shouldSelectNestedBlogWithPostsAndAuthorUsingJoin() {
    try (SqlSession session = sqlMapper.openSession()) {
        Blog blog = session.selectOne("org.apache.ibatis.domain.blog.mappers.NestedBlogMapper.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)

Example 9 with Blog

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

the class BindingTest method executeWithMapKeyAndRowBounds.

@Test
void executeWithMapKeyAndRowBounds() {
    try (SqlSession session = sqlSessionFactory.openSession()) {
        BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
        Map<Integer, Blog> blogs = mapper.selectRangeBlogsAsMapById(new RowBounds(1, 1));
        assertEquals(1, blogs.size());
        Blog blog = blogs.get(2);
        assertEquals(2, blog.getId());
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) RowBounds(org.apache.ibatis.session.RowBounds) Blog(org.apache.ibatis.domain.blog.Blog) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 10 with Blog

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

the class BindingTest method shouldSelectBlogWithAParamNamedValue.

@Test
void shouldSelectBlogWithAParamNamedValue() {
    try (SqlSession session = sqlSessionFactory.openSession()) {
        BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
        Blog blog = mapper.selectBlogWithAParamNamedValue("id", 1, "Jim Business");
        assertNotNull(blog);
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) Blog(org.apache.ibatis.domain.blog.Blog) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Aggregations

Blog (org.apache.ibatis.domain.blog.Blog)30 BaseDataTest (org.apache.ibatis.BaseDataTest)26 Test (org.junit.jupiter.api.Test)26 SqlSession (org.apache.ibatis.session.SqlSession)21 Post (org.apache.ibatis.domain.blog.Post)9 DraftPost (org.apache.ibatis.domain.blog.DraftPost)7 Author (org.apache.ibatis.domain.blog.Author)5 MappedStatement (org.apache.ibatis.mapping.MappedStatement)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)4 Comment (org.apache.ibatis.domain.blog.Comment)4 Tag (org.apache.ibatis.domain.blog.Tag)4 ParameterMap (org.apache.ibatis.mapping.ParameterMap)4 ResultFlag (org.apache.ibatis.mapping.ResultFlag)4 ResultMap (org.apache.ibatis.mapping.ResultMap)4 ResultMapping (org.apache.ibatis.mapping.ResultMapping)4 SqlSource (org.apache.ibatis.mapping.SqlSource)4 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)4