Java--Mybatis一个实体内部嵌套两个或多个相同类型实例对象

需求:有一个用户User表,属性包含两个对象,一个人事部 hr对象(Dept实体对象);

一个财务部 financial(Dept实体对象),通过hrId,和 financialId 分别查询到 hr,和 financial两个对象,根据其相应主键 ID 获取其 name 名称,code 编码字段

User实体类对象如下:

public class User extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** User信息ID */
    @Id
    private Long userId;

    /** 人事部ID(Dept部门对象) */
    private Long hrId;

    /** 财务部ID(Dept部门对象)*/
    private Long financialId;

    /**
     * 人事部对象
     */
    @ApiModelProperty(value = "人事部对象")
    private Dept hr;

    /**
     * 财务部对象
     */
    @ApiModelProperty(value = "财务部对象")
    private Dept financial;

    public void setUserId(Long userId) 
    {
        this.userId = userId;
    }

    public Long getUserId() 
    {
        return userId;
    }

    public void setHrId(Long hrId) 
    {
        this.hrId = hrId;
    }

    public Long getHrId() 
    {
        return hrId;
    }

    public void setFinancialId(Long financialId) 
    {
        this.financialId = financialId;
    }

    public Long getFinancialId() 
    {
        return financialId;
    }

    public Dept getHr() {
        return hr;
    }

    public void setHr(Dept hr) {
        this.hr = hr;
    }

    public Dept getFinancial() {
        return financial;
    }

    public void setFinancial(Dept financial) {
        this.financial = financial;
    }
    }
}

mapper.xml 文件代码如下:

<mapper namespace="com.xx.mapper.UserMapper">

    <!--User表映射-->
    <resultMap type="User" id="UserResult">
        <result property="userId"    column="user_id"    />
        <result property="hrId"    column="hr_id"    />
        <result property="financialId"    column="financial_id"    />

        <!-- 获取人事部对象(Dept对象) -->
        <association property="hr" column="hr_id" javaType="Dept" select="getHr" />
        <!-- 获取财务部对象(Dept对象) -->
        <association property="financial" column="financial_id" javaType="Dept" select="getFinancial" />
    </resultMap>


    <!-- Dept表映射 -->
    <resultMap type="Dept" id="DeptResult">
        <result property="deptId"    column="dept_id"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
    </resultMap>

    <!-- sql查询语句 -->
    <sql id="selectUserVo">
        select user.user_id,
               user.hr_id,
               user.financial_id
        from User user
    </sql>


    <!-- 获取人事部对象(Dept对象) -->
    <select id="getHr" parameterType="Long" resultMap="DeptResult">
        select hr.dept_id,
               hr.code,
               hr.name
        from Dept hr where
                dept_id = #{hrId}
    </select>

    <!-- 获取财务部对象(Dept对象) -->
    <select id="getFinancial" parameterType="Long" resultMap="DeptResult">
        select financial.dept_id,
               financial.code,
               financial.name
        from Dept financial where
            dept_id = #{financialIdId}
    </select>

    <!-- 获取User列表 -->
    <select id="selectUserList" parameterType="User" resultMap="UserResult">
        <include refid="selectUserVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
    </select>
>

在User实体类中嵌套这两个对象,分别是:hr 和 financial,而我们要做的就是将在查询User这个对象的时候通过 hrId 和 financialId 这两个外键查询 hr 和 financial 这两个对象

association: 复杂对象的映射
property: 映射到Order实体类中的receiveUser
column : 这个其实就是你要传的参数名
javaType: 一个 Java 类的完全限定名

整个查询操作流程:

1、数据库依据下面的代码查找User对象,然后将对象映射到 UserResult 结果集中 

<!-- 获取User列表 -->
    <select id="selectUserList" parameterType="User" resultMap="UserResult">
        <include refid="selectUserVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
    </select>

 2、在UserResult中有以下association复杂对象的映射,select="getHr" (关键步骤);即

<!-- 获取人事部对象(Dept对象) -->
<association property="hr" column="hr_id" javaType="Dept" select="getHr" />

<!-- 获取财务部对象(Dept对象) -->
<association property="financial" column="financial_id" javaType="Dept" select="getFinancial" />

3、系统会通过 select 的属性值即 getHr 跳转到 id="getHr" 的xml中,并将column中的 hr_id 映射字段中的值 hrId 传递过去,赋值给 dept_id(关键步骤),即

    <!-- 获取人事部对象(Dept对象) -->
    <select id="getHr" parameterType="Long" resultMap="DeptResult">
        select hr.dept_id,
               hr.code,
               hr.name
        from Dept hr where
                dept_id = #{hrId}
    </select>

    <!-- 获取财务部对象(Dept对象) -->
    <select id="getFinancial" parameterType="Long" resultMap="DeptResult">
        select financial.dept_id,
               financial.code,
               financial.name
        from Dept financial where
            dept_id = #{financialIdId}
    </select>

在查询到 hr 对象之后会将对象放入hr, financial对象同理!

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值