日期:2014-05-19  浏览次数:20851 次

一个表中的两个外键都关联同一个表,mybatis写法
列如:
table user{员工表
  uid;
  uname;
  uage;
  usalary;
 }
table lend{借款表
  lid;
  luserid;借款人id
  lmoney;
  ldate;
  lwarrant_userid;担保人id


查询一个借款单
(借款单的详细信息、借款人的姓名、借款担保人的姓名)
在mybatis中怎么写映射文件
 

------解决方案--------------------
XML code

<typeAlias alias="User" type="xxx.User" />
    <typeAlias alias="Lend" type="xxx.Lend" />

    <resultMap id="get-user-result" class="User">
        <result property="uid" column="UID" />
        <result property="uname" column="UNAME" />
        <result property="uage" column="UAGE" />
        <result property="usalary" column="USALARY" />
    </resultMap>
    
    <resultMap id="get-lend-result" class="Lend">
        <result property="lid" column="LID" />
        <result property="luserid" column="LUSERID" select="getUserById"/>
        <result property="lmoney" column="LMONEY" />
        <result property="lwarrant_userid" column="LWARRANT_USERID" select="getUserById"/>
    </resultMap>

    <select id="selectLendById" parameterClass="int"
        resultMap="get-lend-result">
            select * from LEND where ID = #id#
    </select>
    
    <select id="getUserById" parameterClass="int"
        resultMap="get-user-result">
            select * from USER where ID = #id#
    </select>