mybatis-查询(resultMap,关联集合)-15

场景:查询部门下的所有员工

第一种方式:嵌套结果集方式
第二种方式:分步查询方式

第一种方式:嵌套结果集方式

javaBean

public class Department {
    private Integer id;
    private String name;
    private List<Employee> employees;}

接口

public Department getDepartmentByIdPlus(Integer id);

sql映射文件

    <!--
         private Integer id;
        private String name;
        private List<Employee> employees;
        -->
        <!-- 嵌套结果集的方式-->
        <!--public Department getDepartmentByIdPlus(Integer id);-->
        <resultMap id="myDept" type="com.stayreal.mybatis.Department">
            <id column="did" property="id"/>
            <result column="dept_name" property="name"/>
            <!-- collection定义关联集合类型的属性封装规则
            offType:指定集合中的元素类型
            -->
            <collection property="employees" ofType="com.stayreal.mybatis.Employee">
                <id column="eid" property="id"/>
                <result column="last_name" property="lastName"/>
                <result column="email" property="email"/>
                <result column="gender" property="gender"/>
            </collection>
        </resultMap>
        <select id="getDepartmentByIdPlus"  resultMap="myDept">
            select d.id did,d.dept_name dept_name,e.id eid,e.last_name last_name,
            e.email email,e.gender gender
            from tbl_dept d
            left JOIN tbl_employee e on d.id = e.d_id
            where d.id = #{id}
        </select>

junit

 DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
                Department dept = mapper.getDepartmentByIdPlus(2);// 分步查询
                System.out.println(dept.toString());
                System.out.println(dept.getEmployees());
// Department{id=2, name='ceshi'}[Employee{id=1, //lastName='Jerry', email='Jerry@qq.com', gender='1'}, //Employee{id=3, lastName='Jerry', email='Jerry@qq.com', //gender='1'}]

第二种方式:分步查询方式

public Department getDepartmentByIdStep(Integer id);
<!--public Department getDepartmentByIdStep(Integer id);-->
        <resultMap id="myDeptStep" type="com.stayreal.mybatis.Department">
            <id column="id" property="id"/>
            <result column="dept_name" property="name"/>
            <collection property="employees" select="com.stayreal.mybatis.EmployeeMapperPlus.getEmpsByDeptId"
                    column="id">
            </collection>
        </resultMap>
        <select id="getDepartmentByIdStep"  resultMap="myDeptStep">
                    select id,dept_name name from tbl_dept where id = #{id}
        </select>
 Department dept = mapper.getDepartmentByIdStep(2);// 分步查询  collection
//Department{id=2, name='ceshi'}
//[Employee{id=1, lastName='null', email='Jerry@qq.com', gender='1'}, Employee{id=3, lastName='null', email='Jerry@qq.com', gender='1'}]
MyBatis中的ResultMap是一个非常强大的功能,它允许开发者自定义SQL查询结果映射到Java对象的过程。ResultMap可以指定如何映射结果集中的列到指定的Java对象的属性,这对于处理复杂的关系(如一对多、多对一等)尤其有用。关联查询通常用于查询结果集需要根据外键关联其他表的情况,MyBatis通过ResultMap提供了多种关联查询的方式,例如: 1. association:用于处理一对一关联关系,它可以将结果集的一个字段映射到另一个对象的实例。 2. collection:用于处理一对多关联关系,它可以将结果集的一个字段映射到对象的集合属性中。 使用ResultMap进行关联查询的基本步骤包括: -MyBatis的配置文件中定义ResultMap-resultMap中配置association或collection元素以表示关联关系。 - 在映射的SQL语句中指定使用ResultMap。 下面是一个简单的例子: ```xml <!-- 定义一个ResultMap --> <resultMap id="userOrderMap" type="User"> <id property="id" column="user_id" /> <result property="name" column="user_name" /> <!-- 一对一关联 --> <association property="order" javaType="Order"> <id property="id" column="order_id" /> <result property="orderDate" column="order_date" /> </association> </resultMap> <!-- 映射查询 --> <select id="findUserAndOrder" resultMap="userOrderMap"> SELECT u.*, o.* FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.id = #{userId} </select> ``` 在这个例子中,我们定义了一个名为`userOrderMap`的ResultMap,它映射了User对象Order对象。`findUserAndOrder`查询返回的结果集会自动根据ResultMap映射到User对象中,其中User对象中包含了一个Order类型的属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值