mybatis一对多关系 collection的使用

实体类Department.java

public class Department {
    private Integer id;
    private String departmentName;
    private List<Employee> emps;
    
    
}
DepartmentMapper.java文件

package com.du.mybatis.dao;

import com.du.mybatis.bean.Department;

public interface DepartmentMapper {
    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    public Department getDeptById(Integer id);
    
    /**
     * 根据id查询部门并且返回员工信息
     * @param id
     * @return
     */
    public Department getDeptAndEmpById(Integer id);
    
    /**
     * 根据id查询部门并且返回员工信息
     * @param id
     * @return
     */
    public Department getDeptAndEmpById2(Integer id);
    
    
}

DepartmentMapper.xml文件

<?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">
 <mapper namespace="com.du.mybatis.dao.DepartmentMapper">
    
    
    <!-- public Department getDeptById(Integer id); -->
    <select id="getDeptById" resultType="com.du.mybatis.bean.Department">
        select id,dept_name departmentName from tbl_dept where id=#{id}
    </select>
    
    <!-- 嵌套结果的方式 -->
    <resultMap type="com.du.mybatis.bean.Department" id="MyDeptMap">
        <id column="id" property="id"/>
        <result column="dept_name" property="departmentName"/>
        <!-- 
                                定义集合级联关系
          collection:做一对多关联查询
          ofType:指定集合中元素对象的类型
         -->
        <collection property="emps" ofType="com.du.mybatis.bean.Employee">
            <!-- 此处需要注意, tbl_dept tbl_employee 主键都为id,此处需要区分,因此在sql中取别名,否则会有问题-->
            <id column="e_id" property="id"/>
            <result property="lastName" column="last_name"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>
    
    <!-- public Department getDeptAndEmpById(Integer id); -->
    <select id="getDeptAndEmpById" resultMap="MyDeptMap">
        SELECT d.id,d.dept_name,e.id e_id,e.last_name,e.gender,e.email,e.dept_id 
        FROM tbl_dept d, tbl_employee e 
        WHERE d.id=e.dept_id and d.id=#{id}
    </select>
    
    
    <!-- 嵌套查询的方式 -->
    <resultMap type="com.du.mybatis.bean.Department" id="MyDeptMap2">
        <id column="id" property="id"/>
        <result column="dept_name" property="departmentName"/>
        <!-- 
            column:将该字段作为参数传递给select作为入参 
                                    注意:column也可以传多个列值,写法为column="{key1=column1,key2=column2}",
                                    此处也可以写成column="{dept_id=id}"
            select:可以是一条另外的sql作为参数(此例写法),也可以传入一个mapper的接口方法(具体可参考一对一的例子)
            fetchType:单独配置延迟加载 ,lazy为延迟,eager为立即
        -->
        <collection property="emps" column="id" select="queryEmpsByDeptId"></collection>
    </resultMap>
    <select id="queryEmpsByDeptId" resultType="com.du.mybatis.bean.Employee">
        SELECT id,last_name lastName,gender,email FROM tbl_employee WHERE dept_id=#{dept_id};
    </select>
    
    <!-- public Department getDeptAndEmpById2(Integer id); -->
    <select id="getDeptAndEmpById2" resultMap="MyDeptMap2">
        SELECT id,dept_name FROM tbl_dept WHERE id=#{id}
    </select>
    
    
 </mapper>


DepartmentMapper.java文件
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java MyBatis中,可以使用collection来实现一对多关系。具体步骤如下: 1. 首先,在数据库中创建两个表,一个是主表(例如:Order),另一个是从表(例如:Item)。主表和从表之间通过外键进行关联。 2. 在Java代码中,创建对应的实体类(例如:Order和Item),并为它们添加相应的属性和getter/setter方法。 3. 创建Mapper接口(例如:OrderMapper),定义查询方法,用于获取主表数据及其关联的从表数据。 4. 在Mapper XML文件中,编写SQL语句,使用MyBatis提供的collection标签来映射一对多关系。示例代码如下: ```xml <!-- 查询订单及其对应的商品列表 --> <select id="getOrderWithItems" resultMap="orderResultMap"> SELECT o.id, o.order_no, i.id as item_id, i.name as item_name, i.price as item_price FROM orders o LEFT JOIN items i ON o.id = i.order_id WHERE o.id = #{orderId} </select> <resultMap id="orderResultMap" type="Order"> <id property="id" column="id"/> <result property="orderNo" column="order_no"/> <!-- 使用collection标签映射一对多关系 --> <collection property="items" ofType="Item"> <id property="id" column="item_id"/> <result property="name" column="item_name"/> <result property="price" column="item_price"/> </collection> </resultMap> ``` 5. 在Mapper接口中定义对应的方法,用于调用SQL语句并返回结果。 6. 在Java代码中,通过MyBatis的SqlSessionFactory和Mapper接口来执行查询操作,并获取一对多关系的数据。 这样,你就可以使用Java MyBatis实现一对多关系的查询了。希望能够帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值