mybatis的多表查询

一对一查询(多对一)

  方式一:

    1.定义账户信息实体类

public class Account implements Serializable {

    private Integer id;
    private Integer uid;
    private Double money;
    // ------------get和set方法------------
}

    2.定义AccountUser类继承Account类,加入成员变量(所需要的用户信息)

public class AccountUser extends Account {

    private String username;
    private String address;
    // ------------get和set方法------------
}

    3.定义账户的持久层 Dao 接口

public interface AccountDao {

    /**
     * 查询所有账户信息,以及账户所对应用户的信息
     * @return
     */
    List findAll();
}

    4.定义 AccountDao.xml 文件中的查询配置信息

<select id="findAll" resultType="accountUser">
        select u.username, u.address, a.* from account a, user u where a.uid = u.id
select>

    5.测试

@Test
public void testfindAll() {
    List list = accountDao.findAll();
    for (AccountUser accountUser : list) {
        System.out.println(accountUser);
    }
}

  方式二:

    1.定义账户信息实体类,加入 User 类的对象作为 Account 类的一个属性

public class Account implements Serializable {

    private Integer id;
    private Integer uid;
    private Double money;
    //从表实体应该包含一个主表实体的对象引用
    private User user;
    // ------------get和set方法------------
}
public class User implements Serializable {

    private Integer id;
    private String username;
    private String address;
    private String sex;
    private Date birthday;
    // ------------get和set方法------------
}

    2.定义账户的持久层 Dao 接口

public interface AccountDao {
    /**
     * 查询所有账户信息,以及账户所对应用户的信息
     * @return
     */
    List findAll2();
}

    3.定义 AccountDao.xml 文件中的查询配置信息


    <resultMap id="accountUserMap" type="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="user">
            <id property="id" column="id">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>
        association>
    resultMap>

    <select id="findAll2" resultMap="accountUserMap">
        select u.*, a.id as aid, a.uid, a.money from account a, user u where a.uid = u.id
    select>

    4.测试

@Test
public void testfindAll2() {
    List list = accountDao.findAll2();
    for (Account account : list) {
        System.out.println(account);
        System.out.println(account.getUser());
    }
} 

一对多查询

  1.定义 User 实体类,加入 List,一个用户可以有多个账户

public class User implements Serializable {

    private Integer id;
    private String username;
    private String address;
    private String sex;
    private Date birthday;
    //一对多关系映射:主表实体应该包含从表实体的集合引用
    private List accounts;
    // ------------get和set方法------------
}

  2.定义用户的持久层 Dao 接口

public interface UserDao {
    List findAll();
}

  3.用户持久层 Dao 映射文件配置


    <resultMap id="userAccountMap" type="user">
        <id property="id" column="id">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 property="accounts" ofType="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 AS aid, a.uid, a.money FROM `user` u LEFT JOIN account a ON u.id = a.uid;
    select>

    collection 部分定义了用户关联的账户信息。表示关联查询结果集
      property="accounts" :关联查询的结果集,存储在 User 对象的上那个属性。
      ofType="account" :指定关联查询的结果集中的对象类型即List中的对象类型。在主配置文件已定义别名,此处可以使用别名,也可以使用全限定名。

  4.测试

@Test
public void testfindAll() {
    List users = userDao.findAll();
    for (User user : users) {
        System.out.println(user);
        for (Account account : user.getAccounts()) {
            System.out.println(account);
        }
    }
}

 多对多查询:可以看成双向的一对多查询

  1.编写角色实体类和用户实体类

public class Role implements Serializable {

    private Integer roleId;
    private String roleName;
    private String roleDesc;

    //多对多的关系映射:一个角色可以赋予多个用户
    private List users;
    // ------------get和set方法------------
}
public class User implements Serializable {

    private Integer id;
    private String username;
    private String address;
    private String sex;
    private Date birthday;

    //多对多的关系映射:一个用户可以具备多个角色
    private List roles;
    // ------------get和set方法------------
}

  2.编写 Role 持久层接口

public interface RoleDao {
    List findAll();
}

  3.编写映射文件

<mapper namespace="com.fgy.dao.RoleDao">
    
    <resultMap id="roleUserMap" type="role">
        <id property="roleId" column="rid">id>
        <result property="roleName" column="role_name">result>
        <result property="roleDesc" column="role_desc">result>
        
        <collection property="users" ofType="user">
            <id property="id" column="id">id>
            <result property="username" column="username">result>
            <result property="birthday" column="birthday">result>
            <result property="address" column="address">result>
            <result property="sex" column="sex">result>
        collection>
    resultMap>

    <select id="findAll" resultMap="roleUserMap">
        SELECT r.id AS rid, r.role_name, r.role_desc, u.* FROM role r
            LEFT JOIN user_role ur ON (ur.rid = r.id)
            LEFT JOIN `user` u ON (ur.uid = u.id);
    select>
mapper>

  4.测试

@Test
public void testFindAll() {
    List list = roleDao.findAll();
    for (Role role : list) {
        System.out.println(role);
        for (User user : role.getUsers()) {
            System.out.println(user);
        }
    }
}

 

你可能感兴趣的:(mybatis的多表查询)