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();
}
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());
}
}
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());
}
}
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());
}
}
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);
}
}
Aggregations