关联查询,也称为多表查询,指两个或更多个表一起完成查询操作。关联查询的重点是多张表之间的连接条件,根据该条件数据库才知道这些表之间数据的对应关系,从而分别获取这些对应的记录中查询的字段并构成结果集。需要注意的是,关联查询要有连接条件,不添加连接条件会产生笛卡尔积,这通常是一个无意义的结果集。原则上N张表关联至少需要有N-1个连接条件。
实体间的关系(拥有 has、属于 belong)
OneToOne:一对一关系(account ←→ user)
OneToMany:一对多关系(user ←→ account)
ManyToMany:多对多关系(user ←→ role)
关联查询的语法
指定“一方”关系时(对象),使用< association javaType="" >
指定“多方”关系时(集合),使用< collection ofType="" >
需求:查询账户信息,关联查询用户信息
分析:因为一个账户信息只能供某个用户使用,所以从查询账户信息出发关联查询用户信息为一对一查询。
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
//加入User类的对象作为Account类的一个属性
private User user;public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}public Integer getId() {
return id;
}public void setId(Integer id) {
this.id = id;
}public Integer getUid() {
return uid;
}public void setUid(Integer uid) {
this.uid = uid;
}public Double getMoney() {
return money;
}public void setMoney(Double money) {
this.money = money;
}@Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
", user=" + user +
'}';
}
}
public interface AccountDao {
ListfindAll();
}
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
@Test
public void testOneToOne() {
AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
ListaccountList = accountDao.findAll();
for (Account ac : accountList) {
System.out.println(ac);
}
}
需求:查询所有用户信息及用户关联的账户信息。
分析:用户信息和他的账户信息为一对多关系,并且查询过程中如果用户没有账户信息,此时也要将用户信息查询出来,此时左外连接查询比较合适 。
package com.by.pojo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;public class User implements Serializable {
private Integer id;
private String username;
private String password;
private Date birthday;
private String sex;
private String address;
//加入List存储用户所拥有的账户
private Listaccounts; public Integer getId() {
return id;
}public void setId(Integer id) {
this.id = id;
}public String getUsername() {
return username;
}public void setUsername(String username) {
this.username = username;
}public Date getBirthday() {
return birthday;
}public void setBirthday(Date birthday) {
this.birthday = birthday;
}public String getSex() {
return sex;
}public void setSex(String sex) {
this.sex = sex;
}public String getAddress() {
return address;
}public void setAddress(String address) {
this.address = address;
}public String getPassword() {
return password;
}public void setPassword(String password) {
this.password = password;
}public List
getAccounts() {
return accounts;
}public void setAccounts(List
accounts) {
this.accounts = accounts;
}@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
", accounts=" + accounts +
'}';
}
}
public interface AccountDao {
ListfindAll();
}
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
@Test
public void testOneToMany() {
UserDao userDao = sqlSession.getMapper(UserDao.class);
ListuserList = userDao.findAll();
for(User user : userList){
System.out.println(user);
}
}
需求:查询角色及角色赋予的用户信息
分析:一个用户可以拥有多个角色,一个角色也可以赋予多个用户,用户和角色为双向的一对多关系,多对多关系其实我们看成是双向的一对多关系。
public class Role {
private Integer id;
private String roleName;
private String roleDesc;
//加入Listusers存储角色赋予的用户信息
private Listusers; public Integer getId() {
return id;
}public void setId(Integer id) {
this.id = id;
}public String getRoleName() {
return roleName;
}public void setRoleName(String roleName) {
this.roleName = roleName;
}public String getRoleDesc() {
return roleDesc;
}public void setRoleDesc(String roleDesc) {
this.roleDesc = roleDesc;
}public List
getUsers() {
return users;
}public void setUsers(List
users) {
this.users = users;
}@Override
public String toString() {
return "Role{" +
"id=" + id +
", roleName='" + roleName + '\'' +
", roleDesc='" + roleDesc + '\'' +
", users=" + users +
'}';
}
}
public interface RoleDao {
ListfindAll();
}
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
@Test
public void testManyToMany() {
RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
ListroleList = roleDao.findAll();
for(Role role : roleList){
System.out.println(role);
}
}