1. 程式人生 > 實用技巧 >一個mybatis collection的column引數與select的parameterType引數造成的bug

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

看來自己還需要對框架原理加強理解。