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>

这样就能正常运行了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值