美文网首页
Mybatis查询复杂对象(对象中存在对象集合)

Mybatis查询复杂对象(对象中存在对象集合)

作者: 叫我胖虎大人 | 来源:发表于2019-08-18 11:51 被阅读0次

主对象

@Data
public class AllocationInfo2 {

    @ApiModelProperty(value = "项目编号")
    private String projectId;

    @ApiModelProperty(value = "项目类别")
    private String category;

    @ApiModelProperty(value = "项目负责人")
    private String leader;

    @ApiModelProperty(value = "项目说明")
    private String instruction;

    @ApiModelProperty(value = "项目分配的成员信息")
    private List<Teacher> teachers;

}

子对象

@Data
public class Teacher {

    /**
     * 用户名
     */
    private String username;

    /**
     * 用户ID
     */
    private String userId;

    @Override
    public String toString() {
        return "教师名称:"+username+" 教师工号"+userId+"\n";
    }
}

xml中的resultMap(核心)

注意子对象的前缀, columnPrefix="t_"

<resultMap id="Allocation" type="group.uchain.project_management_system.vo.AllocationInfo2">
        <id column="id" property="projectId"/>
        <result column="category" property="category"/>
        <result column="leader" property="leader"/>
        <result column="instruction" property="instruction"/>
        <collection property="teachers" ofType="group.uchain.project_management_system.vo.Teacher"
        javaType="java.util.ArrayList" columnPrefix="t_">
            <id column="user_id" property="userId" jdbcType="BIGINT"/>
            <id column="username" property="username" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>

xml中的SQL

<select id="getAllAllocationInfo" resultMap="Allocation" resultType="group.uchain.project_management_system.vo.AllocationInfo2">
        select p.id,p.category,p.leader,p.instruction,a.user_id as t_user_id,u.username as t_username
          from project_info p,allocation_info a
          inner join user u
          on a.user_id = u.user_id
        where p.id = a.project_id 
    </select>

测试结果

2019-08-18 11:50:05.824 INFO 32141 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
[AllocationInfo2(projectId=V28, category=专业认证, leader=王五, instruction=计算机公共基础协助2018年石工和化工的专业认证, teachers=[教师名称:小明 教师工号123456789101
, 教师名称:李四 教师工号123456789102
])]

相关文章

  • Mybatis查询复杂对象(对象中存在对象集合)

    主对象 子对象 xml中的resultMap(核心) 注意子对象的前缀, columnPrefix="t_" xm...

  • Java SE 4

    Java SE 1.Set集合 Set用于存储不重复的对象集合,在Set集合中存储的对象中不存在两个对象equal...

  • Spring Data JPA 基本使用

    JPA查询传递参数和对象 参数 对象 通过spring-data-jpa进行复杂对象查询 通过spring-dat...

  • MyBatis 进阶-ResultMap

    ResultMap 是 MyBatis 中最重要最强大的元素 可以实现复杂查询结果到复杂对象关联的关系转化复杂 J...

  • MyBatis Connection 对象存在在哪里?

    平时查询数据库 MyBatis 查询流程 疑问 从 MyBatis 查询流程中看不到 Connection 对象的...

  • JavaScript本地存储

    一、json对象(1) 认识json对象JSON对象:存储复杂数据的字面量对象① 基本语法: ② 查询数据 ③ 修...

  • iOS对象深浅拷贝总结

    一、非集合对象(NSString、NSNumeber) 在非集合类对象中:对immutable对象(不可变对象)进...

  • C#沉淀-Linq的使用

    Linq 可以轻松的查询对象集合。Linq代表语言集成查询,是.NET框架的扩展,支持从数据库、程序对象的集合以及...

  • 并发集合

    集合类:集合类存放的都是对象的引用,而非对象本身,出于表达上的便利,我们称集合中的对象就是指集合中对象的引用(re...

  • (九)类集合框架

    什么是集合? (set)集合中的对象不按特定的方式排序,并且没有重复对象。 什么是列表? (list)集合中的对象...

网友评论

      本文标题:Mybatis查询复杂对象(对象中存在对象集合)

      本文链接:https://www.haomeiwen.com/subject/rcehsctx.html