use of org.apache.ibatis.mapping.MappedStatement in project mybatis-3 by mybatis.
the class ExecutorTestHelper method prepareSelectPostsForBlogMappedStatement.
public static MappedStatement prepareSelectPostsForBlogMappedStatement(final Configuration config) {
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
final SqlSource sqlSource = new StaticSqlSource(config, "SELECT p.id, p.created_on, p.blog_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" + " INNER JOIN post_tag pt ON pt.post_id = p.id" + " INNER JOIN tag t ON pt.tag_id = t.id" + " LEFT OUTER JOIN comment c ON c.post_id = p.id" + " WHERE p.blog_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, "defaultResultMap", 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", "blog_id", registry.getTypeHandler(int.class)).javaType(Blog.class).nestedQueryId("selectBlogById").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();
}
use of org.apache.ibatis.mapping.MappedStatement in project mybatis-3 by mybatis.
the class ExecutorTestHelper method prepareSelectOneAuthorMappedStatementWithConstructorResults.
public static MappedStatement prepareSelectOneAuthorMappedStatementWithConstructorResults(final Configuration config) {
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
MappedStatement ms = new MappedStatement.Builder(config, "selectAuthor", new StaticSqlSource(config, "SELECT * FROM author WHERE id = ?"), SqlCommandType.SELECT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {
{
add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build());
}
}).build()).resultMaps(new ArrayList<ResultMap>() {
{
add(new ResultMap.Builder(config, "defaultResultMap", Author.class, new ArrayList<ResultMapping>() {
{
add(new ResultMapping.Builder(config, null, "id", registry.getTypeHandler(Integer.class)).javaType(int.class).flags(new ArrayList<ResultFlag>() {
{
add(ResultFlag.CONSTRUCTOR);
}
}).build());
add(new ResultMapping.Builder(config, "username", "username", registry.getTypeHandler(String.class)).build());
add(new ResultMapping.Builder(config, "password", "password", registry.getTypeHandler(String.class)).build());
add(new ResultMapping.Builder(config, "email", "email", registry.getTypeHandler(String.class)).build());
add(new ResultMapping.Builder(config, "bio", "bio", registry.getTypeHandler(String.class)).build());
add(new ResultMapping.Builder(config, "favouriteSection", "favourite_section", registry.getTypeHandler(Section.class)).build());
}
}).build());
}
}).cache(authorCache).build();
return ms;
}
use of org.apache.ibatis.mapping.MappedStatement in project mybatis-3 by mybatis.
the class ExecutorTestHelper method prepareInsertAuthorMappedStatementWithBeforeAutoKey.
public static MappedStatement prepareInsertAuthorMappedStatementWithBeforeAutoKey(final Configuration config) {
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
final ResultMap rm = new ResultMap.Builder(config, "keyResultMap", Integer.class, new ArrayList<ResultMapping>()).build();
MappedStatement kms = new MappedStatement.Builder(config, "insertAuthor!selectKey", new StaticSqlSource(config, "SELECT 123456 as id FROM SYSIBM.SYSDUMMY1"), SqlCommandType.SELECT).keyProperty("id").resultMaps(new ArrayList<ResultMap>() {
{
add(rm);
}
}).build();
config.addMappedStatement(kms);
MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new DynamicSqlSource(config, new TextSqlNode("INSERT INTO author (id,username,password,email,bio,favourite_section) values(#{id},#{username},#{password},#{email},#{bio:VARCHAR},#{favouriteSection})")), SqlCommandType.INSERT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {
{
add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(Integer.class)).build());
add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).build());
add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).build());
add(new ParameterMapping.Builder(config, "favouriteSection", registry.getTypeHandler(Section.class)).jdbcType(JdbcType.VARCHAR).build());
}
}).build()).cache(authorCache).keyGenerator(new SelectKeyGenerator(kms, true)).keyProperty("id").build();
return ms;
}
use of org.apache.ibatis.mapping.MappedStatement in project mybatis-3 by mybatis.
the class BaseExecutorTest method shouldSelectDiscriminatedPost.
@Test
public void shouldSelectDiscriminatedPost() throws Exception {
Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
try {
MappedStatement selectStatement = ExecutorTestHelper.prepareSelectDiscriminatedPost(config);
List<Map<String, String>> products = executor.query(selectStatement, null, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
assertEquals(5, products.size());
for (Map<String, String> m : products) {
if ("IMAGES".equals(m.get("SECTION"))) {
assertNull(m.get("subject"));
} else {
assertNotNull(m.get("subject"));
}
}
} finally {
executor.close(false);
}
}
use of org.apache.ibatis.mapping.MappedStatement in project mybatis-3 by mybatis.
the class BaseExecutorTest method shouldUpdateAuthor.
@Test
public void shouldUpdateAuthor() throws Exception {
Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
try {
Author author = new Author(101, "someone", "******", "someone@apache.org", null, Section.NEWS);
MappedStatement updateStatement = ExecutorTestHelper.prepareUpdateAuthorMappedStatement(config);
MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
int rows = executor.update(updateStatement, author);
List<Author> authors = executor.query(selectStatement, 101, 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);
}
}
Aggregations