org.apache.ibatis.mapping.Discriminator Java Examples
The following examples show how to use
org.apache.ibatis.mapping.Discriminator.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XMLMapperBuilder.java From Shop-for-JavaWeb with MIT License | 6 votes |
private Discriminator processDiscriminatorElement(XNode context, Class<?> resultType, List<ResultMapping> resultMappings) throws Exception { String column = context.getStringAttribute("column"); String javaType = context.getStringAttribute("javaType"); String jdbcType = context.getStringAttribute("jdbcType"); String typeHandler = context.getStringAttribute("typeHandler"); Class<?> javaTypeClass = resolveClass(javaType); @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler); JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType); Map<String, String> discriminatorMap = new HashMap<String, String>(); for (XNode caseChild : context.getChildren()) { String value = caseChild.getStringAttribute("value"); String resultMap = caseChild.getStringAttribute("resultMap", processNestedResultMappings(caseChild, resultMappings)); discriminatorMap.put(value, resultMap); } return builderAssistant .buildDiscriminator(resultType, column, javaTypeClass, jdbcTypeEnum, typeHandlerClass, discriminatorMap); }
Example #2
Source File: DefaultResultSetHandler.java From mybatis with Apache License 2.0 | 6 votes |
public ResultMap resolveDiscriminatedResultMap(ResultSet rs, ResultMap resultMap, String columnPrefix) throws SQLException { Set<String> pastDiscriminators = new HashSet<String>(); Discriminator discriminator = resultMap.getDiscriminator(); while (discriminator != null) { final Object value = getDiscriminatorValue(rs, discriminator, columnPrefix); final String discriminatedMapId = discriminator.getMapIdFor(String.valueOf(value)); if (configuration.hasResultMap(discriminatedMapId)) { resultMap = configuration.getResultMap(discriminatedMapId); Discriminator lastDiscriminator = discriminator; discriminator = resultMap.getDiscriminator(); if (discriminator == lastDiscriminator || !pastDiscriminators.add(discriminatedMapId)) { break; } } else { break; } } return resultMap; }
Example #3
Source File: MapperAnnotationBuilder.java From mybaties with Apache License 2.0 | 6 votes |
private Discriminator applyDiscriminator(String resultMapId, Class<?> resultType, TypeDiscriminator discriminator) { if (discriminator != null) { String column = discriminator.column(); Class<?> javaType = discriminator.javaType() == void.class ? String.class : discriminator.javaType(); JdbcType jdbcType = discriminator.jdbcType() == JdbcType.UNDEFINED ? null : discriminator.jdbcType(); Class<? extends TypeHandler<?>> typeHandler = discriminator.typeHandler() == UnknownTypeHandler.class ? null : discriminator.typeHandler(); Case[] cases = discriminator.cases(); Map<String, String> discriminatorMap = new HashMap<String, String>(); for (Case c : cases) { String value = c.value(); String caseResultMapId = resultMapId + "-" + value; discriminatorMap.put(value, caseResultMapId); } return assistant.buildDiscriminator(resultType, column, javaType, jdbcType, typeHandler, discriminatorMap); } return null; }
Example #4
Source File: XMLMapperBuilder.java From mybatis with Apache License 2.0 | 6 votes |
private Discriminator processDiscriminatorElement(XNode context, Class<?> resultType, List<ResultMapping> resultMappings) throws Exception { String column = context.getStringAttribute("column"); String javaType = context.getStringAttribute("javaType"); String jdbcType = context.getStringAttribute("jdbcType"); String typeHandler = context.getStringAttribute("typeHandler"); Class<?> javaTypeClass = resolveClass(javaType); @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler); JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType); Map<String, String> discriminatorMap = new HashMap<String, String>(); for (XNode caseChild : context.getChildren()) { String value = caseChild.getStringAttribute("value"); String resultMap = caseChild.getStringAttribute("resultMap", processNestedResultMappings(caseChild, resultMappings)); discriminatorMap.put(value, resultMap); } return builderAssistant.buildDiscriminator(resultType, column, javaTypeClass, jdbcTypeEnum, typeHandlerClass, discriminatorMap); }
Example #5
Source File: XMLMapperBuilder.java From mybaties with Apache License 2.0 | 6 votes |
private Discriminator processDiscriminatorElement(XNode context, Class<?> resultType, List<ResultMapping> resultMappings) throws Exception { String column = context.getStringAttribute("column"); String javaType = context.getStringAttribute("javaType"); String jdbcType = context.getStringAttribute("jdbcType"); String typeHandler = context.getStringAttribute("typeHandler"); Class<?> javaTypeClass = resolveClass(javaType); @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler); JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType); Map<String, String> discriminatorMap = new HashMap<String, String>(); for (XNode caseChild : context.getChildren()) { String value = caseChild.getStringAttribute("value"); String resultMap = caseChild.getStringAttribute("resultMap", processNestedResultMappings(caseChild, resultMappings)); discriminatorMap.put(value, resultMap); } return builderAssistant.buildDiscriminator(resultType, column, javaTypeClass, jdbcTypeEnum, typeHandlerClass, discriminatorMap); }
Example #6
Source File: DefaultResultSetHandler.java From mybaties with Apache License 2.0 | 6 votes |
public ResultMap resolveDiscriminatedResultMap(ResultSet rs, ResultMap resultMap, String columnPrefix) throws SQLException { Set<String> pastDiscriminators = new HashSet<String>(); Discriminator discriminator = resultMap.getDiscriminator(); while (discriminator != null) { final Object value = getDiscriminatorValue(rs, discriminator, columnPrefix); final String discriminatedMapId = discriminator.getMapIdFor(String.valueOf(value)); if (configuration.hasResultMap(discriminatedMapId)) { resultMap = configuration.getResultMap(discriminatedMapId); Discriminator lastDiscriminator = discriminator; discriminator = resultMap.getDiscriminator(); if (discriminator == lastDiscriminator || !pastDiscriminators.add(discriminatedMapId)) { break; } } else { break; } } return resultMap; }
Example #7
Source File: MapperAnnotationBuilder.java From mybatis with Apache License 2.0 | 6 votes |
private Discriminator applyDiscriminator(String resultMapId, Class<?> resultType, TypeDiscriminator discriminator) { if (discriminator != null) { String column = discriminator.column(); Class<?> javaType = discriminator.javaType() == void.class ? String.class : discriminator.javaType(); JdbcType jdbcType = discriminator.jdbcType() == JdbcType.UNDEFINED ? null : discriminator.jdbcType(); Class<? extends TypeHandler<?>> typeHandler = discriminator.typeHandler() == UnknownTypeHandler.class ? null : discriminator.typeHandler(); Case[] cases = discriminator.cases(); Map<String, String> discriminatorMap = new HashMap<String, String>(); for (Case c : cases) { String value = c.value(); String caseResultMapId = resultMapId + "-" + value; discriminatorMap.put(value, caseResultMapId); } return assistant.buildDiscriminator(resultType, column, javaType, jdbcType, typeHandler, discriminatorMap); } return null; }
Example #8
Source File: ExecutorTestHelper.java From mybatis with Apache License 2.0 | 5 votes |
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()); } }).build(); return ms; }
Example #9
Source File: MapperBuilderAssistant.java From mybatis with Apache License 2.0 | 5 votes |
public Discriminator buildDiscriminator( Class<?> resultType, String column, Class<?> javaType, JdbcType jdbcType, Class<? extends TypeHandler<?>> typeHandler, Map<String, String> discriminatorMap) { ResultMapping resultMapping = buildResultMapping( resultType, null, column, javaType, jdbcType, null, null, null, null, typeHandler, new ArrayList<ResultFlag>(), null, null, false); Map<String, String> namespaceDiscriminatorMap = new HashMap<String, String>(); for (Map.Entry<String, String> e : discriminatorMap.entrySet()) { String resultMap = e.getValue(); resultMap = applyCurrentNamespace(resultMap, true); namespaceDiscriminatorMap.put(e.getKey(), resultMap); } Discriminator.Builder discriminatorBuilder = new Discriminator.Builder(configuration, resultMapping, namespaceDiscriminatorMap); return discriminatorBuilder.build(); }
Example #10
Source File: ResultMapResolver.java From mybatis with Apache License 2.0 | 5 votes |
public ResultMapResolver(MapperBuilderAssistant assistant, String id, Class<?> type, String extend, Discriminator discriminator, List<ResultMapping> resultMappings, Boolean autoMapping) { this.assistant = assistant; this.id = id; this.type = type; this.extend = extend; this.discriminator = discriminator; this.resultMappings = resultMappings; this.autoMapping = autoMapping; }
Example #11
Source File: MapperAnnotationBuilder.java From mybatis with Apache License 2.0 | 5 votes |
private void applyResultMap(String resultMapId, Class<?> returnType, Arg[] args, Result[] results, TypeDiscriminator discriminator) { List<ResultMapping> resultMappings = new ArrayList<ResultMapping>(); applyConstructorArgs(args, returnType, resultMappings); applyResults(results, returnType, resultMappings); Discriminator disc = applyDiscriminator(resultMapId, returnType, discriminator); // TODO add AutoMappingBehaviour assistant.addResultMap(resultMapId, returnType, null, disc, resultMappings, null); createDiscriminatorResultMaps(resultMapId, returnType, discriminator); }
Example #12
Source File: ExecutorTestHelper.java From mybaties with Apache License 2.0 | 5 votes |
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()); } }).build(); return ms; }
Example #13
Source File: MapperBuilderAssistant.java From mybaties with Apache License 2.0 | 5 votes |
public Discriminator buildDiscriminator( Class<?> resultType, String column, Class<?> javaType, JdbcType jdbcType, Class<? extends TypeHandler<?>> typeHandler, Map<String, String> discriminatorMap) { ResultMapping resultMapping = buildResultMapping( resultType, null, column, javaType, jdbcType, null, null, null, null, typeHandler, new ArrayList<ResultFlag>(), null, null, false); Map<String, String> namespaceDiscriminatorMap = new HashMap<String, String>(); for (Map.Entry<String, String> e : discriminatorMap.entrySet()) { String resultMap = e.getValue(); resultMap = applyCurrentNamespace(resultMap, true); namespaceDiscriminatorMap.put(e.getKey(), resultMap); } Discriminator.Builder discriminatorBuilder = new Discriminator.Builder(configuration, resultMapping, namespaceDiscriminatorMap); return discriminatorBuilder.build(); }
Example #14
Source File: ResultMapResolver.java From mybaties with Apache License 2.0 | 5 votes |
public ResultMapResolver(MapperBuilderAssistant assistant, String id, Class<?> type, String extend, Discriminator discriminator, List<ResultMapping> resultMappings, Boolean autoMapping) { this.assistant = assistant; this.id = id; this.type = type; this.extend = extend; this.discriminator = discriminator; this.resultMappings = resultMappings; this.autoMapping = autoMapping; }
Example #15
Source File: MapperAnnotationBuilder.java From mybaties with Apache License 2.0 | 5 votes |
private void applyResultMap(String resultMapId, Class<?> returnType, Arg[] args, Result[] results, TypeDiscriminator discriminator) { List<ResultMapping> resultMappings = new ArrayList<ResultMapping>(); applyConstructorArgs(args, returnType, resultMappings); applyResults(results, returnType, resultMappings); Discriminator disc = applyDiscriminator(resultMapId, returnType, discriminator); // TODO add AutoMappingBehaviour assistant.addResultMap(resultMapId, returnType, null, disc, resultMappings, null); createDiscriminatorResultMaps(resultMapId, returnType, discriminator); }
Example #16
Source File: DefaultResultSetHandler.java From mybaties with Apache License 2.0 | 4 votes |
private Object getDiscriminatorValue(ResultSet rs, Discriminator discriminator, String columnPrefix) throws SQLException { final ResultMapping resultMapping = discriminator.getResultMapping(); final TypeHandler<?> typeHandler = resultMapping.getTypeHandler(); return typeHandler.getResult(rs, prependPrefix(resultMapping.getColumn(), columnPrefix)); }
Example #17
Source File: XMLMapperBuilder.java From mybaties with Apache License 2.0 | 4 votes |
private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> additionalResultMappings) throws Exception { //错误上下文 //取得标示符 ("resultMap[userResultMap]") // <resultMap id="userResultMap" type="User"> // <id property="id" column="user_id" /> // <result property="username" column="username"/> // <result property="password" column="password"/> // </resultMap> ErrorContext.instance().activity("processing " + resultMapNode.getValueBasedIdentifier()); String id = resultMapNode.getStringAttribute("id", resultMapNode.getValueBasedIdentifier()); //一般拿type就可以了,后面3个难道是兼容老的代码? String type = resultMapNode.getStringAttribute("type", resultMapNode.getStringAttribute("ofType", resultMapNode.getStringAttribute("resultType", resultMapNode.getStringAttribute("javaType")))); //高级功能,还支持继承? // <resultMap id="carResult" type="Car" extends="vehicleResult"> // <result property="doorCount" column="door_count" /> // </resultMap> String extend = resultMapNode.getStringAttribute("extends"); //autoMapping Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping"); Class<?> typeClass = resolveClass(type); Discriminator discriminator = null; List<ResultMapping> resultMappings = new ArrayList<ResultMapping>(); resultMappings.addAll(additionalResultMappings); List<XNode> resultChildren = resultMapNode.getChildren(); for (XNode resultChild : resultChildren) { if ("constructor".equals(resultChild.getName())) { //解析result map的constructor processConstructorElement(resultChild, typeClass, resultMappings); } else if ("discriminator".equals(resultChild.getName())) { //解析result map的discriminator discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings); } else { List<ResultFlag> flags = new ArrayList<ResultFlag>(); if ("id".equals(resultChild.getName())) { flags.add(ResultFlag.ID); } //调5.1.1 buildResultMappingFromContext,得到ResultMapping resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags)); } } //最后再调ResultMapResolver得到ResultMap ResultMapResolver resultMapResolver = new ResultMapResolver(builderAssistant, id, typeClass, extend, discriminator, resultMappings, autoMapping); try { return resultMapResolver.resolve(); } catch (IncompleteElementException e) { configuration.addIncompleteResultMap(resultMapResolver); throw e; } }
Example #18
Source File: XMLMapperBuilder.java From mybatis with Apache License 2.0 | 4 votes |
private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> additionalResultMappings) throws Exception { //错误上下文 //取得标示符 ("resultMap[userResultMap]") // <resultMap id="userResultMap" type="User"> // <id property="id" column="user_id" /> // <result property="username" column="username"/> // <result property="password" column="password"/> // </resultMap> ErrorContext.instance().activity("processing " + resultMapNode.getValueBasedIdentifier()); String id = resultMapNode.getStringAttribute("id", resultMapNode.getValueBasedIdentifier()); //一般拿type就可以了,后面3个难道是兼容老的代码? String type = resultMapNode.getStringAttribute("type", resultMapNode.getStringAttribute("ofType", resultMapNode.getStringAttribute("resultType", resultMapNode.getStringAttribute("javaType")))); //高级功能,还支持继承? // <resultMap id="carResult" type="Car" extends="vehicleResult"> // <result property="doorCount" column="door_count" /> // </resultMap> String extend = resultMapNode.getStringAttribute("extends"); //autoMapping Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping"); Class<?> typeClass = resolveClass(type); Discriminator discriminator = null; List<ResultMapping> resultMappings = new ArrayList<ResultMapping>(); resultMappings.addAll(additionalResultMappings); List<XNode> resultChildren = resultMapNode.getChildren(); for (XNode resultChild : resultChildren) { if ("constructor".equals(resultChild.getName())) { //解析result map的constructor processConstructorElement(resultChild, typeClass, resultMappings); } else if ("discriminator".equals(resultChild.getName())) { //解析result map的discriminator discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings); } else { List<ResultFlag> flags = new ArrayList<ResultFlag>(); if ("id".equals(resultChild.getName())) { flags.add(ResultFlag.ID); } //调5.1.1 buildResultMappingFromContext,得到ResultMapping resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags)); } } //最后再调ResultMapResolver得到ResultMap ResultMapResolver resultMapResolver = new ResultMapResolver(builderAssistant, id, typeClass, extend, discriminator, resultMappings, autoMapping); try { return resultMapResolver.resolve(); } catch (IncompleteElementException e) { configuration.addIncompleteResultMap(resultMapResolver); throw e; } }
Example #19
Source File: DefaultResultSetHandler.java From mybatis with Apache License 2.0 | 4 votes |
private Object getDiscriminatorValue(ResultSet rs, Discriminator discriminator, String columnPrefix) throws SQLException { final ResultMapping resultMapping = discriminator.getResultMapping(); final TypeHandler<?> typeHandler = resultMapping.getTypeHandler(); return typeHandler.getResult(rs, prependPrefix(resultMapping.getColumn(), columnPrefix)); }
Example #20
Source File: XMLMapperBuilder.java From Shop-for-JavaWeb with MIT License | 4 votes |
private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> additionalResultMappings) throws Exception { ErrorContext.instance().activity( "processing " + resultMapNode.getValueBasedIdentifier()); String id = resultMapNode.getStringAttribute("id", resultMapNode.getValueBasedIdentifier()); String type = resultMapNode.getStringAttribute("type", resultMapNode .getStringAttribute("ofType", resultMapNode.getStringAttribute( "resultType", resultMapNode.getStringAttribute("javaType")))); String extend = resultMapNode.getStringAttribute("extends"); Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping"); Class<?> typeClass = resolveClass(type); Discriminator discriminator = null; List<ResultMapping> resultMappings = new ArrayList<ResultMapping>(); resultMappings.addAll(additionalResultMappings); List<XNode> resultChildren = resultMapNode.getChildren(); for (XNode resultChild : resultChildren) { if ("constructor".equals(resultChild.getName())) { processConstructorElement(resultChild, typeClass, resultMappings); } else if ("discriminator".equals(resultChild.getName())) { discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings); } else { ArrayList<ResultFlag> flags = new ArrayList<ResultFlag>(); if ("id".equals(resultChild.getName())) { flags.add(ResultFlag.ID); } resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags)); } } ResultMapResolver resultMapResolver = new ResultMapResolver( builderAssistant, id, typeClass, extend, discriminator, resultMappings, autoMapping); try { return resultMapResolver.resolve(); } catch (IncompleteElementException e) { configuration.addIncompleteResultMap(resultMapResolver); throw e; } }