一个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后,果然成功了。

 

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

 

posted @ 2021-01-08 18:22  lpjworkroom  阅读(2049)  评论(0编辑  收藏  举报