一个mybatis collection的column参数与select的parameterType参数造成的bug


<resultMap id="TeamIndexResult" type="TeamIndex">
        <id property="teamId" column="team_id" javaType="int" jdbcType="INTEGER"/>
        <collection property="probSets" javaType="List" ofType="TeamIndexProbSet" select="getIndexProbSets"
                    column="teamId=team_id"/>
    </resultMap>


    <select id="getIndexProbSets" parameterType="int" resultMap="TeamIndexProbSetResult" >
        select team_id,set_index,title from team_index
        where team_id=#{teamId}
        order by set_index asc
    </select>

如上,上方的resultMap中有一个collection引用下方的select,其中上方的team_id列会作为select的参数。

注意,写下代码时对colleciton的column属性特性不熟,想用键值表示法将列改名,就写了teamId=team_id。

没有多想,下方select将参数类型设置为int。

parameterType="int"

 

运行后报错:

Error instantiating class java.lang.Integer with invalid types () or values (). Cause: java.lang.NoSuchMethodException: java.lang.Integer.<init>()]

将下方参数类型改成java.lang.Integer后同样报错。

原因

collection的column属性如果用键值法写,就算只有一个参数,mybatis也会自动将参数包装成map类型。

所以下方的参数类型实际上是map。

将下方select的parameterType改成map后,果然成功了。

 

看来自己还需要对框架原理加强理解。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM