1. 程式人生 > >Mapper.xml 錯誤查詢與分析

Mapper.xml 錯誤查詢與分析

Mapper.xml 報錯


 org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in cn/itcast/mybatis/mapper/UserMapper.xml
### The error occurred while processing mapper_resultMap[userResultMap]
### Cause: org.apache.ibatis.builder.BuilderException: Error
parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'userResultMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: userResultMap

Mapper.xml程式碼

    <resultMap type="User" id="userResultMap">
        <!--id表示查詢結果唯一標識 -->
        <id column="id_" property="id" />

        <!-- result普通名對映定義 -->
        <result column="username_" property="username" />
    </resultMap>

    <!-- 使用resultMap輸出對映 -->
<select id="findUserByIdResultMap" parameterType="int" resultType="userResultMap"> SELECT id id_,username username_ FROM USER WHERE id=#{value} </select>

錯誤分析:
無法找到別名為userResultMap的java物件

查出問題程式碼

<select id="findUserByIdResultMap" parameterType="int"
        resultType="userResultMap">

解決方案
將resultType 改為 resultMap 即可


心得
細節決定成敗