mybatis报错:The error may exist in xxxxMapper.xml
Posted 香风智乃哈~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis报错:The error may exist in xxxxMapper.xml相关的知识,希望对你有一定的参考价值。
学到了mybatis,在进行一对一映射的时候报这个异常
### Error building SqlSession.
### The error may exist in StudentMapper.xml
### The error occurred while processing mapper_resultMap[AddressResult]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'StudentMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Address'. Cause: java.lang.ClassNotFoundException: Cannot find class: Address
其实这个异常报的还挺明确的,这个是映射文件也就是StudentMapper.xml里面的代码
<resultMap id="selectStudentResult1" type="Student">
<id property="studId" column="id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="dob" column="dob"/>
<result property="phone" column="phone"/>
<association property="address" resultMap="AddressResult"/>
</resultMap>
//注意下面这一行的type
<resultMap id="AddressResult" type="Address">
<id property="addrId" column="addr_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="state" column="state"/>
<result property="zip" column="zip"/>
<result property="country" column="country"/>
</resultMap>
<!--这个是用来测试一对一ResultMap的 一对一映射更好的方法嵌套结果-->
<select id="selectStudentWithAddress1" parameterType="int" resultMap="selectStudentResult1">
select id,name,email,dob,phone,
street,city,state,zip,country from students s left join addresses a
on s.addr_id = a.addr_id
where id = #{id}
</select>
也有相对应的文件
我的出错原因是在配置文件里面mybatis-config.xml。这里面没有给Address起别名。(因为在resultMap里面的type属性使用的是Address,所以别名就叫Address)
(type里面是这个类的全限定名,把address加上就可以了)
<typeAliases>
<typeAlias type="全限定名" alias="Student" />
<!-- <typeAlias type="全限定名" alias="Address"/>-->
</typeAliases>
这个标签里面还可以使用package把一个目录下的所有类都起默认名
(使用package就不用加类名了)
<typeAliases>
<package name="全限定名去掉类名" />
</typeAliases>
这样就能正常运行了。
以上是关于mybatis报错:The error may exist in xxxxMapper.xml的主要内容,如果未能解决你的问题,请参考以下文章
mybatis报错:The error may exist in xxxxMapper.xml
mybatis的小坑 ### The error may exist in com/vector/dao/*Mapper.xml
### The error may involve defaultParameterMap ### The error occurred while setting parameters
### The error may involve defaultParameterMap ### The error occurred while setting parameters
### The error may involve defaultParameterMap ### The error occurred while setting parameters
### The error may involve defaultParameterMap ### The error occurred while setting parameters