Tag Archives: The error may exist in xxxxMapper.xml

Mybatis Error: The error may exist in xxxxMapper.xml [How to Solve]

After learning mybatis, this exception is reported during one-to-one mapping

### 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

In fact, the exception report is quite clear. This is the mapping file, that is, the code in 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>

//Note the type of the following line
	<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>

	<! -- This is used to test one-to-one ResultMap One-to-one mapping of better method nesting results -->
	<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>

There are also corresponding files

The reason for my error is mybatis-config.xml in the configuration file. There is no alias for address. (because the type attribute in the resultmap uses address, the alias is called address)
(the type attribute is the fully qualified name of the class. Add address)

	<typeAliases>
		<typeAlias type="Full name" alias="Student" />
<!--		<typeAlias type="Full name" alias="Address"/>-->
	</typeAliases>

In this tab, you can also use package to set the default name of all classes in a directory
(if you use package, you don’t need to add the class name)

	<typeAliases>
		<package name="Full qualified name without class name" />
	</typeAliases>

This will work properly.