美文网首页
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查询复杂对象(对象中存在对象集合)

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