return ms;
}
public static MappedStatement prepareSelectDiscriminatedPost(final Configuration config) {
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
final ResultMap discriminatorResultMap = new ResultMap.Builder(config, "postResultMap", HashMap.class, new ArrayList<ResultMapping>() {
{
add(new ResultMapping.Builder(config, "subject", "subject", registry.getTypeHandler(String.class)).build());
add(new ResultMapping.Builder(config, "body", "body", registry.getTypeHandler(String.class)).build());
}
}).build();
config.addResultMap(discriminatorResultMap);
MappedStatement ms = new MappedStatement.Builder(config, "selectPosts", new StaticSqlSource(config,"SELECT * FROM post"), SqlCommandType.SELECT)
.resultMaps(new ArrayList<ResultMap>() {
{
add(new ResultMap.Builder(config, "defaultResultMap", HashMap.class, new ArrayList<ResultMapping>() {
{
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).build());
add(new ResultMapping.Builder(config, "blog_id", "blog_id", registry.getTypeHandler(int.class)).build());
}
})
.discriminator(new Discriminator.Builder(
config, new ResultMapping.Builder(config, "section", "section", registry.getTypeHandler(String.class)).build(),
new HashMap<String, String>() {
{
put("NEWS", discriminatorResultMap.getId());
put("VIDEOS", discriminatorResultMap.getId());
put("PODCASTS", discriminatorResultMap.getId());
//NEWS left out on purpose.
}
}).build()).build());
}