天下难事、必作于易;天下大事、必作|

园龄:粉丝:关注:

2016-02-23 00:11阅读: 35248评论: 3推荐: 2

6.mybatis异常:SQL Mapper Configuration,Error parsing Mapper XML,Could not resolve type alias

在xxxMapper中

复制代码
<select id="getClazz" parameterType="int" resultType="getClazzMap">
        SELECT * FROM class c,teacher t WHERE c.tid = t.tid AND c.cid=#{id}
    </select>
    
    <resultMap type="Clazz" id="getClazzMap">
        <id property="id" column="cid"/>
        <result property="name" column="cname"/>
        <!-- 关联班级对应的teacher -->
        <association property="teacher" javaType="Teacher">
            <id property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>
复制代码

查各种资料发现,用到resultType,必须在mybatis的配置文件中进行别名申明该resultType属于哪个实体类

<!-- 配置xxxMapper.xml中的实体类的别名 -->
    <typeAliases>
        <!-- 单个实体类配置别名 -->
        <typeAlias type="com.mlxs.mybatis.test1.User" alias="User"/>
        <typeAlias type="com.mlxs.mybatis.test1.Clazz" alias="getClassMap"/>
        <!-- 整个包配置,别名默认为类名 推荐 -->
        <package name="com.mlxs.mybatis.bean"/>
    </typeAliases>

但是,其实我这上面用错了,我真正要用的是resultMap,不是resultType

复制代码
<select id="getClazz" parameterType="int" resultMap="getClazzMap">
        SELECT * FROM class c,teacher t WHERE c.tid = t.tid AND c.cid=#{id}
    </select>
    
    <resultMap type="Clazz" id="getClazzMap">
        <id property="id" column="cid"/>
        <result property="name" column="cname"/>
        <!-- 关联班级对应的teacher -->
        <association property="teacher" javaType="Teacher">
            <id property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>
复制代码

这个在写的时候要非常注意... ...

本文作者:Yzl1990

本文链接:https://www.cnblogs.com/yangzhenlong/p/5208587.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   艺言弈行  阅读(35248)  评论(3编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起