mybatis嵌套查询对象下子对象的list

本文转载,参考https://blog.csdn.net/apple_5/article/details/72953946

有的时候我们在查询的时候会需要再一个对象里返回他的子对象里的一个list

场景:查询某个公司下,销售部和商务部下所有的人员

那么我们希望返回的数据结构是:

在mybatis里我们使用下面的方式,用一条sql语句查询出来

嵌套结果集方式

javaBean

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

接口

public Department getDepartmentByIdPlus(Integer id);
  • 1

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>
  •  

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想查询一个表及其对象,可以使用 MyBatis嵌套查询(Nested Query)功能,具体步骤如下: 1. 在 Mapper 接口中定义查询方法,并使用 @ResultMap 注解指定结果映射关系。例如: ```java @Mapper public interface MyMapper { @Select("SELECT * FROM table WHERE id = #{id}") @Results(id = "resultMap", value = { @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), // 其他字段... @Result(property = "subObjects", column = "id", many = @Many(select = "com.example.MyMapper.getSubObjects")) }) TableObject getTableObject(int id); @Select("SELECT * FROM sub_table WHERE table_id = #{tableId}") List<SubObject> getSubObjects(int tableId); } ``` 在上面的代码中,我们定义了一个查询方法 `getTableObject`,它会查询名为 `table` 的表,并将查询结果映射为 `TableObject` 对象。我们使用 `@ResultMap` 注解指定了结果映射关系,其中包括一个名为 `resultMap` 的 ID,以及一个包含了对象映射的 `@Result` 注解。其中 `many` 属性表示这是一个一对多的关系,我们将调用 `getSubObjects` 方法查询对象并映射到 `subObjects` 属性上。 2. 在 `getSubObjects` 方法中定义查询语句,并返回查询结果。例如: ```java public List<SubObject> getSubObjects(int tableId) { String sql = "SELECT * FROM sub_table WHERE table_id = #{tableId}"; return sqlSession.selectList("com.example.MyMapper.getSubObjects", tableId); } ``` 3. 在代码中调用 `getTableObject` 方法,并传递表 ID 参数。例如: ```java TableObject tableObject = myMapper.getTableObject(1); ``` 这样,我们就可以查询一个表及其对象了。注意,上面的代码仅供参考,具体的 SQL 语句和结果映射关系需要根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值