When using tk.mybatis, the collection mapping of resultMap is used in the custom SQL process, but after adding a custom List to the bean generated by the Generator, the following mapping in the Mapper file will report an error:
Table Structure:
A:user_id user_name user_age
B:Role_id Role_name Role_path
Bean:
public class User{
@Column("USER_ID")
private String userId;
@Column("USER_NAME")
private String userName;
@Column("USER_AGE")
private String userAge;
private List<Role> roles;
//getter and setter
}
public class Role{
@Column("ROLE_ID")
private String roleId;
@Column("ROLE_NAME")
private String roleName;
@Column("ROLE_PATH")
private String rolePath;
//getter and setter
}
mapper.xml:
<resultMap id="userMap" type="com.XXX.model.User">
<result property="userId" jdbcType="VARCHAR" column="USER_ID"/>
<result property="userName" jdbcType="VARCHAR" column="USER_NAME"/>
<result property="userAge" jdbcType="VARCHAR" column="USER_AGE"/>
<collection property="roles" resultMap="roleMap"/>
</resultMap>
<resultMap id="roleMap" type="com.XXX.model.Role">
<result property="roleId" jdbcType="VARCHAR" column="ROLE_ID"/>
<result property="roleName" jdbcType="VARCHAR" column="ROLE_NAME"/>
<result property="rolePath" jdbcType="VARCHAR" column="ROLE_PATH"/>
</resultMap>
At this point, starting the project will report an error:
Caused by: java.lang.IllegalStateException: No typehandler found for property roles
At this point, we need to add the annotation @javax.persistence.Transient to the roles we customized in the User class to ignore this field and make it use our customized map.
See: https://github.com/abel533/MyBatis-Spring-Boot/issues/4