Mybatis多表查询

一对一(多对一)案例

  1. 一对一查询:用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户.

  2. 一对一查询的需求:查询所有订单,与此同时查询出每个订单所属的用户

  3. Orders实体类

    public class Orders {
        private Integer id;
        private String ordertime;
        private Double total;
        private Integer uid;
    
        // 表示当前订单属于那个用户 association
        private User user;
    
        @Override
        public String toString() {
            return "Orders{" +
                    "id=" + id +
                    ", ordertime='" + ordertime + '\'' +
                    ", total=" + total +
                    ", uid=" + uid +
                    ", user=" + user +
                    '}';
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getOrdertime() {
            return ordertime;
        }
    
        public void setOrdertime(String ordertime) {
            this.ordertime = ordertime;
        }
    
        public Double getTotal() {
            return total;
        }
    
        public void setTotal(Double total) {
            this.total = total;
        }
    
        public Integer getUid() {
            return uid;
        }
    
        public void setUid(Integer uid) {
            this.uid = uid;
        }
    }
    
  4. OrderMapper接口

    /**
     * 查询所有订单,与此同时查询出每个订单所属的用户
     * @return
     */
    public List<Orders> findAllWithUser();
    
  5. OrderMapper.xml映射

    <!--一对一关联查询:查询所有订单,与此同时还要查询出每个订单所属的用户信息-->
    <resultMap id="orderMap" type="com.code.entity.Orders">
        <id property="id" column="id"/>
        <result property="ordertime" column="ordertime"/>
        <result property="total" column="total"/>
        <result property="uid" column="uid"/>
        <!--
            association : 在进行一对一关联查询配置时,使用association标签进行关联
            property="user" :要封装实体的属性名
            javaType="com.code.demo.User" 要封装的实体的属性类型
        -->
        <association property="user" javaType="com.code.entity.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>
        </association>
    </resultMap>
    <select id="findAllWithUser" resultMap="orderMap">
        SELECT * FROM orders o LEFT JOIN USER u ON o.uid = u.id
    </select>
    
  6. 测试代码

    /**
     * 一对一关联查询:查询所有订单,与此同时还要查询出每个订单所属的用户信息
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
    
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    
        SqlSession sqlSession = sqlSessionFactory.openSession();
    
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
    
        List<Orders> orders = mapper.findAllWithUser();
        for (Orders order : orders) {
            System.out.println(order);
        }
        sqlSession.close();
    }
    

一对多查询案例

  1. 用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户‘;

  2. 一对多查询的需求:查询所有用户,与此同时查询出该用户具有的订单

  3. User实体

    public class User implements Serializable {
        private Integer id;
        private String username;
        private Date birthday;
        private String sex;
        private String address;
    
        // 表示多方关系:集合 : 代表了当前用户所具有的订单列表 collection
        private List<Orders> ordersList;
    
        // 表示多方关系:集合 : 代表了当前用户所具有的角色列表 collection
        private List<Role> roleList;
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", username='" + username + '\'' +
                    ", birthday=" + birthday +
                    ", sex='" + sex + '\'' +
                    ", address='" + address + '\'' +
                    ", ordersList=" + ordersList +
                    ", roleList=" + roleList +
                    '}';
        }
    
        public List<Role> getRoleList() {
            return roleList;
        }
    
        public void setRoleList(List<Role> roleList) {
            this.roleList = roleList;
        }
    
        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 List<Orders> getOrdersList() {
            return ordersList;
        }
    
        public void setOrdersList(List<Orders> ordersList) {
            this.ordersList = ordersList;
        }
    }
    
  4. UserMapper接口

    /**
     * 一对多关联查询:查询所有的用户,同时还要查询出每个用户所关联的订单信息
     * @return
     */
    public List<User> findAllWithOrder();
    
  5. UserMapper.xml映射

    <!--一对多嵌套查询:查询所有的用户,同时还要查询出每个用户所关联的订单信息-->
    <resultMap id="userOrderMap" type="com.code.entity.User">
        <id property="id" column="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>
        <!--fetchType="lazy" : 延迟加载策略
            fetchType="eager": 立即加载策略
        -->
        <collection property="ordersList" ofType="com.code.entity.Orders" column="id"
                    select="com.code.mapper.OrderMapper.findByUid" ></collection>
    </resultMap>
    <select id="findAllWithOrder2" resultMap="userOrderMap">
        SELECT * FROM `user`
    </select>
    
  6. 测试代码

    /**
     * 一对多关联查询:查询所有用户及关联的订单信息
     * @throws IOException
     */
    @Test
    public void test2() throws IOException {
    
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    
        SqlSession sqlSession = sqlSessionFactory.openSession();
    
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    
        List<User> allWithOrder = mapper.findAllWithOrder();
    
        for (User user : allWithOrder) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    

多对多查询案例

  1. 用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用

  2. 多对多查询的需求:查询所有用户同时查询出该用户的所有角色

  3. Role 实体

    public class Role {
    
        private Integer id;
        private String rolename;
        private String roleDesc;
    
        @Override
        public String toString() {
            return "Role{" +
                    "id=" + id +
                    ", rolename='" + rolename + '\'' +
                    ", roleDesc='" + roleDesc + '\'' +
                    '}';
        }
    
        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;
        }
    }
    
  4. UserMapper接口

    /**
     * 多对多关联查询:查询所有的用户,同时还要查询出每个用户所关联的角色信息
     * @return
     */
    public List<User> findAllWithRole();
    
  5. UserMapper.xml映射

    <!--多对多嵌套查询:查询所有的用户,同时还要查询出每个用户所关联的角色信息-->
    <resultMap id="userRoleMap2" type="com.code.entity.User">
        <id property="id" column="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="roleList" ofType="com.code.entity.Role" column="id" select="com.code.mapper.RoleMapper.findByUid"></collection>
    </resultMap>
    <select id="findAllWithRole2" resultMap="userRoleMap2">
        SELECT * FROM USER
    </select>
    
  6. 测试代码

    /**
     * 多对多关联查询:查询所有用户及关联的角色信息
     * @throws IOException
     */
    @Test
    public void test3() throws IOException {
    
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    
        SqlSession sqlSession = sqlSessionFactory.openSession();
    
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    
        List<User> allWithRole = mapper.findAllWithRole();
    
        for (User user : allWithRole) {
            System.out.println(user);
        }
    
        sqlSession.close();
        
    }
    
    * 多对一(一对一)配置:使用<resultMap>+<association>做配置
    * 一对多配置:使用<resultMap>+<collection>做配置
    * 多对多配置:使用<resultMap>+<collection>做配置
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值