欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

记录一次:Ambiguous collection type for property ‘ adds‘. You must specify ‘javaType‘ or ‘resultMap‘.错误;

程序员文章站 2022-07-15 13:09:09
...

           最近在学mybatis,在学习高级映射时,自己的xml配置基本和老师的一样,但就是因为collection property=" adds"这句多了一个空格就报了这个错,找半天。真无语

<!-- 以下是一对多的完整代码-->
   <resultMap type="com.bxit.entity.User" id="userMap">
    <id column="num" property="num"/>
     <result column="name" property="name"/> 
     <result column="age" property="age"/>
           <!-- 映射一对多的关系:ofType属性必须要指定 -->
         <collection property=" adds" ofType="com.bxit.entity.Address" >
             <!-- 映射主键 -->
                <id column="a_num" property="aid"/>
                <!-- 映射普通属性 -->
                <result column="a_city" property="city"/>
                <result column="a_street" property="street"/>

 <!-- 一对多 -->
    
   <select id="getUserAddress" resultMap="userMap" >
    select s.*,u.* from t10 s left join t11 u on u.a_umun=s.num 
    where s.name=#{name}
   </select>  


    

<!-- 一对多 -->
   <resultMap type="com.bxit.entity.User" id="userMap">
    <id column="num" property="num"/>
     <result column="name" property="name"/> 
     <result column="age" property="age"/>
           <!-- 映射一对多的关系:ofType属性必须要指定 -->
         
错误出现位置:<collection property=" adds" ofType="com.bxit.entity.Address" >
             <!-- 映射主键 -->
		        <id column="a_num" property="aid"/>
		        <!-- 映射普通属性 -->
		        <result column="a_city" property="city"/>
		        <result column="a_street" property="street"/>



后台报错结果:​
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/bxit/mapper/UserMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Ambiguous collection type for property ' adds'. You must specify 'javaType' or 'resultMap'.
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:123)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:95)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:378)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120)
	... 4 more
Caused by: org.apache.ibatis.builder.BuilderException: Ambiguous collection type for property ' adds'. You must specify 'javaType' or 'resultMap'.
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.validateCollection(XMLMapperBuilder.java:412)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.processNestedResultMappings(XMLMapperBuilder.java:399)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.lambda$buildResultMappingFromContext$0(XMLMapperBuilder.java:383)
	at org.apache.ibatis.parsing.XNode.getStringAttribute(XNode.java:200)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildResultMappingFromContext(XMLMapperBuilder.java:382)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:280)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:254)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:246)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:119)
	... 7 more

​


改正后:

<resultMap type="com.bxit.entity.User" id="userMap">
    <id column="num" property="num"/>
     <result column="name" property="name"/> 
     <result column="age" property="age"/>
           <!-- 映射一对多的关系:ofType属性必须要指定 -->
         <collection property="adds" ofType="com.bxit.entity.Address" >
             <!-- 映射主键 -->
		        <id column="a_num" property="aid"/>
		        <!-- 映射普通属性 -->
		        <result column="a_city" property="city"/>
		        <result column="a_street" property="street"/>
           </collection>
     </resultMap>  
   

后台:
User [num=1, name=小明, age=15, adds=[Address [aid=1, city=上海, street=龙, user=null], Address [aid=2, city=上海, street=龙田, user=null]]]
Address [aid=1, city=上海, street=龙, user=null]
Address [aid=2, city=上海, street=龙田, user=null]

而且配置文档对于标签的顺序也有要求,一不小心就会报错,得万分小心!