Mybatis使用resultMap查询对象
查询的结果对象
@Data
public class UserDto {
private User user;
private UserInformation userInformation;
}
@Data
@TableName("user")
public class User {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
private String username;
private String role;
private String permission;
private String password;
}
@Data
@TableName("user_information")
public class UserInformation {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
private Integer userId;
private String phone;
private String address;
}
mapper.xml 中
<resultMap id="usetDto" type="com.fxp.as.dto.UserDto">
<association property="user" column="id" javaType="com.fxp.as.bean.User" select="getUserById"></association>
<association property="userInformation" column="id" javaType="com.fxp.as.bean.UserInformation" select="getUserInformation"></association>
</resultMap>
<select id="getUserInformation" resultType="com.fxp.as.bean.UserInformation">
select id,user_id,phone,address
from user_information
where user_id = #{id}
</select>
<select id="getUserById" resultType="com.fxp.as.bean.User">
select id,username,role,permission,password
from user
where id = #{id}
</select>
<select id="getUserDto" resultMap="usetDto">
select #{vo.id} as id
</select>
mapper dao 层中
UserDto getUserDto(@Param("vo") GetUserVo vo);
最终结果