一個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