MyBatis的多表查询
数据之间有多种对应关系,如一对多(用户和订单),多对一,一对一(人和身份证号),多对多(老师和学生)
一对一
例子:用户和账户 一个用户可以有多个账户 一个账户只能属于一个用户 现在我们想要的是在查询所有账户信息的同时可以展示用户的情况.
法一
创建实体类AccountUser,通过继承的方式让这个类中拥有所有需要的属性;
public class AccountUser extends Account {
private String username;
private String address;
public String getUsername() {
return username;
}
public String getAddress() {
return address;
}
public void setUsername(String username) {
this.username = username;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return super.toString() + " AccountUser{" +
"username='" + username + '\'' +
", address='" + address + '\'' +
'}';
}
}
dao中的方法为:
List<AccountUser> findAllAccount();
xml文件的标签为:
<select id="findAllAccount" resultType="org.zjb.domain.AccountUser">
select a.*, u.username, u.address from account a, user u where u.id = a.uid;
</select>
上面的sql语句放在mysql中直接执行,得到的结果如下: 而AccountUser的属性和这个查询得到的表的列名一致(忽略大小写),这样在反射封装结果的时候就可以对应上,不会出现问题.
法二
通过组合的方式 实体类如下:
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
", user=" + user +
'}';
}
public void setId(Integer id) {
this.id = id;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public void setMoney(Double money) {
this.money = money;
}
public Integer getId() {
return id;
}
public Integer getUid() {
return uid;
}
public Double getMoney() {
return money;
}
}
dao
List<Account> findAll();
xml文件信息
<resultMap id="accountUserMap" type="org.zjb.domain.Account">
<id property="id" column="aid"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<association property="user" column="uid" javaType="org.zjb.domain.User">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="address" property="address"></result>
<result column="sex" property="sex"></result>
<result column="birthday" property="birthday"></result>
</association>
</resultMap>
<select id="findAll" resultMap="accountUserMap">
select u.*, a.id as aid, a.uid, a.money from account a, user u where u.id = a.uid;
</select>
我们使用了association这个标签,这个标签表示联系,可以理解为外键 对于它的属性 property : 代表实体类对象中对应的属性 column : 代表数据表中外键的列名 javaType : 代表property属性对应的类是哪一类 上面的sql语句的执行结果为 根据我们提前配置好的map,刚好可以将表的列对应到对象对应的属性上.
一对多
现在我们的目的变成查询用户信息的同时查询出绑定在这个用户身上的账户 由于一个用户对应多个账户,所以我们将在User类中添加一个list存放账户信息 实体类如下
public class User implements Serializable {
private Integer id;
private String username;
private String address;
private String sex;
private Date birthday;
List<Account> accounts;
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
", accounts=" + accounts +
'}';
}
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setAddress(String address) {
this.address = address;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public String getAddress() {
return address;
}
public String getSex() {
return sex;
}
public Date getBirthday() {
return birthday;
}
}
dao信息如下
List<User> findAll();
xml文件的信息如下
<resultMap id="userAccountMap" type="org.zjb.domain.User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<collection property="accounts" ofType="org.zjb.domain.Account">
<id property="id" column="aid"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="userAccountMap">
select u.*, a.id aid, a.uid, a.money from user u left join account a
on u.id = a.uid;
</select>
这里使用了collection标签,它的属性 property : 表示实体类中的哪一个属性代表集合 ofType : 表示集合中存放的是那种类型的元素 上面的sql语句执行的结果为
多对多
事例 : 用户和角色 一个用户可以有多个角色 一个角色可以赋予多个用户 属于多对多
步骤:
- 建立两张表,用户表和角色表
让用户表和角色表具有多对多的关系,需要使用中间表,中间表中包含各个表的主键 - 建立两个实体类,用户实体和角色实体
让用户和角色的实体类能体现出多对多的关系,各自包含对方一个集合的引用. - 建立两个配置文件
用户的配置文件和角色的配置文件 - 实现配置
当我们查询用户的时候,可以同时得到用户下的角色信息; 当我们查询角色时,可以同时得到角色的所属用户信息.
有了前面的一对多,这里我们就比较好处理了 只需要在两个实体类中都添加对方的集合类作为属性即可. User实体类:
public class User implements Serializable {
private Integer id;
private String username;
private String address;
private String sex;
private Date birthday;
List<Role> roles;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
", roles=" + roles +
'}';
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public List<Role> getRoles() {
return roles;
}
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setAddress(String address) {
this.address = address;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public String getAddress() {
return address;
}
public String getSex() {
return sex;
}
public Date getBirthday() {
return birthday;
}
}
User的dao:
List<Role> findAll();
User的xml:
<resultMap id="userAccountMap" type="org.zjb.domain.User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<collection property="roles" ofType="role">
<id property="roleId" column="rid"></id>
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="userAccountMap">
select n.*, r.role_name, r.role_desc from (
select u.*, u_r.rid from user u left join user_role u_r on u.id = u_r.uid
) n left join role r on n.rid = r.id;
</select>
sql语句执行结果: 需要注意属性需要和表的列名一致
role实体类:
public class Role implements Serializable {
private Integer roleId;
private String roleName;
private String roleDesc;
private List<User> users;
@Override
public String toString() {
return "Role{" +
"roleId=" + roleId +
", roleName='" + roleName + '\'' +
", roleDesc='" + roleDesc + '\'' +
", users=" + users +
'}';
}
public void setUsers(List<User> users) {
this.users = users;
}
public List<User> getUsers() {
return users;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public void setRoleDesc(String roleDesc) {
this.roleDesc = roleDesc;
}
public Integer getRoleId() {
return roleId;
}
public String getRoleName() {
return roleName;
}
public String getRoleDesc() {
return roleDesc;
}
}
role的dao:
List<Role> findAll();
role的xml:
<resultMap id="roleMap" type="org.zjb.domain.Role">
<id property="roleId" column="id"></id>
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
<collection property="users" ofType="org.zjb.domain.User">
<id property="id" column="uid"></id>
<result property="username" column="username"></result>
<result property="birthday" column="birthday"></result>
<result property="sex" column="sex"></result>
<result property="address" column="address"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="roleMap">
select n.*, u.username, u.birthday, u.sex, u.address from (
select r.*, u_r.uid from role r left join user_role u_r on r.id = u_r.rid
) n left join user u on n.uid = u.id;
</select>
sql查询结果:
|