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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步