Mybatis入门笔记(8)——多表操作之一对多

一对多

需求:
查询所有用户信息及用户关联的账户信息。
分析:
用户信息和他的账户信息为一对多关系,并且查询过程中如果用户没有账户信息,此时也要将用户信息查询出来,我们想到了左外连接查询比较合适。

步骤:

  1. 编写SQL语句;

    select u.*,a.id as aid,a.uid,a.money from user u left outer join account a on u.id = a.uid
    

    查询到用户信息与其对应账户信息,如图所示。

  2. User类中加入List<Account>

    public class User implements Serializable {
    
        private Integer id;
        private String username;
        private Date birthday;
        private String sex;
        private String address;
    
        //一对多关系映射:主表实体应该包含从表实体的集合引用
        private List<Account> accounts;
    
        public List<Account> getAccounts() {
            return accounts;
        }
    
        public void setAccounts(List<Account> accounts) {
            this.accounts = accounts;
        }
    
        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;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", username='" + username + '\'' +
                    ", birthday=" + birthday +
                    ", sex='" + sex + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }
    
  3. 用户持久层接口加入查询方法;

    public interface IUserDao {
        //查询所有用户,同时获取到用户下的所有订单信息
        List<User> findAll();
        // 通过ID查询一个用户
        User findUserById(Integer id);
    }
    
  4. 映射配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!--namespace:用来区别不同的类的名字 -->
    <mapper namespace="com.ben.dao.IUserDao">
    
        <resultMap id="userMap" type="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>
            <!-- collection 是用于建立一对多中集合属性的对应关系
            ofType 用于指定集合元素的数据类型
            -->
            <collection property="accounts" ofType="account">
                <id column="aid" property="id"/>
                <result column="uid" property="uid"/>
                <result column="money" property="money"/>
            </collection>
    
        </resultMap>
    
        <!--    查询所有-->
        <select id="findAll" resultMap="userMap">
            select u.*,a.id as aid,a.uid,a.money from user u left outer join account a on u.id = a.uid
        </select>
    </mapper>
    

  5. 编写测试类方法

    //查询所有用户
    @Test
    public void testFindAll(){
        List<User> list = userdao.findAll();
        for (User user : list) {
            System.out.println("----每个用户的信息----");
            System.out.println(user);
            System.out.println(user.getAccounts());
        }
    }
    
posted @ 2019-07-20 15:09  伊万夫斯基  阅读(369)  评论(0编辑  收藏  举报