18 MyBatis——多表查询


引入

多表查询我们就不能使用实体类来接收结果,而使用VO(Value Object)类来接收。他们在结构形式上是一致的,但是定位不同,VO类专用于多表查询结果的封装。

案例:t_user表与t_group表的查询

t_user表结构

 

t_group表结构

VO类的设计

多表查询结果通常使用一个VO类来存储数据。

我们现在要实现查询结果附带所在的组名称,于是要在原来的bean基础上添加groupName属性。

public class UserVO {
	private Integer id;
	private String username;
	private String password;
	private Integer age;
	private String phone;
	private String email;
	private Integer groupId;
	private String groupName;
        //setter/getter
        //toString() hashCode() equals()      
}

mapper查询示例


UserMapper.xml中添加查询方法

UserVO findVOById(Integer id);