`
shuzheng5201314
  • 浏览: 203400 次
  • 来自: 北京
社区版块
存档分类
最新评论

Mapping a list of strings as <collection> in XML could be more intuitive

 
阅读更多

The bahviour observed when mapping a string collection does not feel very obvious to me. I only figured out how thanks to this thread:

For the following SQL statement:

select id, str from testTable

where testTable contains:

id str
1 A
1 B

And where we expect a single result row with the content:

{"id": 1, "str": ["A", "B"]}

this must be mapped as follows:

<resultMap id="stringsResultOk" type="java.util.Map">
    <id column="id" property="id" />
    <collection property="strings" ofType="java.lang.String" javaType="list">
        <result column="str" />
    </collection>
</resultMap>

I'd usually expect that the <result> tag in the collection would refer to a property of an item in the collection which of course would not make any sense for items of type string. However it works perfectly fine.

On the otherhand this mapping:

<resultMap id="stringsWrongColumnMapping" type="java.util.Map">
    <id column="id" property="id" />
    <collection property="strings" column="str" ofType="java.lang.String" javaType="list" />
</resultMap>

would seem to be the natural choice for me, however the "column" attribute seems to have no effect. (It is only used for Nested Select for Collection?). Instead this mapping will just be filled with a value from the id column which does seem rather random.

The following mapping won't work either:

<resultMap id="stringsMissingJavaType" type="java.util.Map">
    <id column="id" property="id" />
    <collection property="strings" ofType="java.lang.String">
        <result column="str" />
    </collection>
</resultMap>

This does not work because the collection does not specify a javaType for the collection. Instead the tag will generate just a single plain string which is quite a surprise because it is most definitly not a collection as the tag <collection> would suggest.

I suggest to either change the behaviour of the later two mapping types or throw some helpful exceptions that point one to the right direction about what is wrong. And maybe extend the documentation about how the mapping for collection of simple types works (if I haven't accidentially missed it).

See some tests that illustrate my findings with some real code here:

https://github.com/yankee42/mybatis-string-collection

 

来源:https://github.com/mybatis/mybatis-3/issues/364

分享到:
评论

相关推荐

    Visual.Studio.Tools.for.Office.Using.C.Sharp.with.Excel.Word.Outlook.and.InfoPath.Sep.2005

    Working with XML in Word &lt;br&gt; Introduction to Word's XML Features &lt;br&gt; An End-to-End Scenario: Creating a Schema and Mapping It into a Word Document &lt;br&gt; Exporting the Mapped XML in the Document to ...

    Visual.Studio.Tools.for.Office.Using.C.Sharp.with.Excel.Word.Outlook.and.InfoPath.Sep.2005.part2

    Working with XML in Word &lt;br&gt; Introduction to Word's XML Features &lt;br&gt; An End-to-End Scenario: Creating a Schema and Mapping It into a Word Document &lt;br&gt; Exporting the Mapped XML in the Document to ...

    <<Patterns of Enterprise Application Architecture>>

    &lt;br&gt;&lt;br&gt;The topics covered include: &lt;br&gt;&lt;br&gt;Dividing an enterprise application into layers &lt;br&gt;The major approaches to organizing business logic &lt;br&gt;An in-depth treatment of mapping between objects ...

    VB编程资源大全(英文源码 网络)

    This could be used for what i used it for in the past for a news program&lt;END&gt;&lt;br&gt;28 , url.zip&lt;br&gt;Worldwide list of URL extensions by country&lt;END&gt;&lt;br&gt;29 , EmailChk.zip&lt;br&gt;This Application checks for ...

    Applied ADO.NET: Building Data-Driven Solutions(1)

    Building Data-Driven Solutions 第一部分&lt;br&gt;Table of Contents &lt;br&gt; Applied ADO.NET—Building Data-Driven Solutions &lt;br&gt; Introduction &lt;br&gt; Chapter 1 - ADO.NET Basics &lt;br&gt; Chapter 2 - Data Components in ...

    动态加载图片

    &lt;mime-mapping&gt; &lt;extension&gt;htm&lt;/extension&gt; &lt;mime-type&gt;text/html;charset=gb2312&lt;/mime-type&gt; &lt;/mime-mapping&gt; &lt;mime-mapping&gt; &lt;extension&gt;html&lt;/extension&gt; &lt;mime-type&gt;text/html;charset=gb2312&lt;/mime-...

    ProjectOZ

    MMU - maps CPU virtual addresses into physical addresses, for valid mappings&lt;br&gt; TRAP - an interruption in the normal sequencing of a CPU due to an invalid&lt;br&gt; memory mapping, a trap, exception, or an...

    The Scheme

    The Scheme&lt;br&gt;Programming Language&lt;br&gt;&lt;br&gt;... Multitasking with Engines &lt;br&gt;&lt;br&gt;Bibliography&lt;br&gt;&lt;br&gt;Answers to Selected Exercises&lt;br&gt;&lt;br&gt;Formal Syntax of Scheme&lt;br&gt;&lt;br&gt;Summary of Forms&lt;br&gt;&lt;br&gt;Index&lt;br&gt;

    jsp 注册<servlet>方法说明

    &lt;servlet&gt; &lt;servlet-name&gt;HelloWorldname&lt;/servlet-name&gt; &lt;!-- 此说明该&lt;servlet/&gt; 已经得到了注册名HelloWorldname, 既此servlet已经得到了注册名HelloWorldname, 它与&lt;servlet-mapping/&gt;必须注册同样的注册...

    web.xml文件中各个标签的介绍

    例如:&lt;filter-mapping&gt;&lt;filter-name&gt;CocoSystemAuthFilter&lt;/filter-name&gt;&lt;url-pattern&gt;firstServlet(servlet 名称)&lt;/url-pattern&gt;或者 URL 模式&lt;url-pattern&gt;/*.do(URL 字符串)&lt;/url-pattern&gt;&lt;/filter-mapping&gt; ...

    mysql+jdbc+jsp+Hibernate3.2+tomcattomcat5.028成功测试

    column="EVENT_ID"&gt;&lt;br&gt; &lt;generator class="native"/&gt;&lt;br&gt; &lt;/id&gt;&lt;br&gt; &lt;property name="date" type="timestamp" column="EVENT_DATE"/&gt;&lt;br&gt; &lt;property name="title"/&gt;&lt;br&gt; &lt;/class&gt;&lt;br&gt;&lt;br&gt;&lt;/hibernate-mapping&gt;&lt;br&gt;...

    Use Cases Patterns and Blueprints

    guidance for designers and reviewers&lt;br&gt;&lt;br&gt;Examples from a wide range of application domains&lt;br&gt;&lt;br&gt;Rely on this book as the world's #1 reference to reusable use-case solutionsand as a catalyst for ...

    hibernate 教程

    hibernate-mapping&lt;br&gt;5.1.3. class&lt;br&gt;5.1.4. id&lt;br&gt;5.1.4.1. generator&lt;br&gt;5.1.4.2. 高/低位算法(Hi/Lo Algorithm)&lt;br&gt;5.1.4.3. UUID算法(UUID Algorithm )&lt;br&gt;5.1.4.4. 标识字段和序列(Identity columns ...

    hibernate

    hibernate-mapping&lt;br&gt;5.1.3. class&lt;br&gt;5.1.4. id&lt;br&gt;5.1.4.1. generator&lt;br&gt;5.1.4.2. 高/低位算法(Hi/Lo Algorithm)&lt;br&gt;5.1.4.3. UUID算法(UUID Algorithm )&lt;br&gt;5.1.4.4. 标识字段和序列(Identity columns ...

    Advances in Geometric Modeling.djvu

    It also aims to collect and disseminate information in various disciplines including: &lt;br&gt;Computer Graphics &lt;br&gt;Computer Vision &lt;br&gt;Computer Aided Geometric Design &lt;br&gt;Geometric Algorithms &lt;br&gt;...

    myeclipse中文处理

    name&gt;encoding&lt;/param-name&gt;&lt;br&gt; &lt;param-value&gt;utf-8&lt;/param-value&gt;&lt;br&gt; &lt;/init-param&gt;&lt;br&gt; &lt;init-param&gt;&lt;br&gt; &lt;param-name&gt;forceEncoding&lt;/param-name&gt;&lt;br&gt; &lt;param-value&gt;true&lt;/param-value&gt;&lt;br&gt; &lt;/init-...

    web.xml中的mime-type标签作用与使用

    &lt;extension&gt;.aif&lt;/extension&gt; &lt;mime-type&gt;audio/x-aiff&lt;/mime-type&gt; &lt;/mime-mapping&gt; &lt;mime-mapping&gt; &lt;extension&gt;.aiff&lt;/extension&gt; &lt;mime-type&gt;audio/x-aiff&lt;/mime-type&gt; &lt;/mime-mapping&gt; &lt;mime-mapping&gt; ...

    java文集

    Java正则表达式 &lt;br&gt;批量上传--采集 (多个文件夹)&lt;br&gt;The Agile Way&lt;br&gt;hibernate mapping文件中的标记详解:关系标记&lt;br&gt;ANT 安装使用及build.xml文档模板&lt;br&gt;inverse和cascade在关联更新中的作用&lt;br&gt;hibernate ...

    简单配置 shiro + spring +springMVC+hibernate简单框架

    &lt;filter-mapping&gt; &lt;filter-name&gt;shiroFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; maven 的pom.xml 配置 &lt;!-- shiro简单配置 --&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache....

    java项目实体类转换实例

    &lt;trim-strings&gt;true&lt;/trim-strings&gt; &lt;/configuration&gt; &lt;!-- &lt;mapping date-format="yyyy-MM-dd"&gt; &lt;class-a&gt;com.vine.dozer.model.Model1&lt;/class-a&gt; &lt;class-b&gt;com.vine.dozer.model.Info1&lt;/class-b&gt; &lt;field&gt; ...

Global site tag (gtag.js) - Google Analytics