1. 程式人生 > >A表關聯B表2次,A表得用左連線

A表關聯B表2次,A表得用左連線

案例:

採購表和使用者表,採購表裡有建立人和稽核人的id,人員的id在使用者表裡

表分別是:使用者表:sys_user   採購表:procurment

實體類:使用者表:User   採購表:Procurment

Mybatis:

<sql id="ProcurementColumns">
   p.id as pid,
   p.procurementCode,
   p.procurementType,
   p.createTime,
   p.createById,
   p.auditTime,
   p.auditById,
   p.status,
   su.id as suId,
   su.name as suName,
   sur.id as surId,
   sur.name as surName
</sql>
<resultMap id="ProcurementList" type="Procurement">
   <result property="id" column="pid"/>
   <result property="procurementCode" column="procurementCode"/>
   <result property="procurementType" column="procurementType"/>
   <result property="createTime" column="createTime"/>
   <result property="createById" column="createById"/>
   <result property="auditTime" column="auditTime"/>
   <result property="auditById" column="auditById"/>
   <result property="status" column="status"/>
   <association property="user" javaType="User" >
      <id property="id" column="suId"/>
      <result property="name" column="suName"/>
   </association>
       <association property="user1" javaType="User" >
           <id property="id" column="surId"/>
           <result property="name" column="surName"/>
       </association>
</resultMap>

<select id="findList"  resultMap="ProcurementList">
   SELECT
   <include refid="ProcurementColumns"/>
   from procurement p left join sys_user su on su.id=p.createById
       left join sys_user sur on p.auditById=sur.id
       where 1=1
   <if test="procurementCode!=null and procurementCode!=''">and p.procurementCode like CONCAT ('%',#{procurementCode},'%')</if>
   <if test="procurementType!=null and procurementType!=-1">and p.procurementType = #{procurementType}</if>
   <if test="status!=null and status!=-1">and p.status = #{status}</if>
       ORDER BY p.createTime DESC
</select>

實體類:

User
private String id; 
private Office company; // 歸屬公司
private Office office; // 歸屬部門
private String loginName;// 登入名
private String password;// 密碼
private String no;    // 工號
private String name;   // 姓名
private String email;  // 郵箱
private String phone;  // 電話
private String mobile; // 手機
private String userType;// 使用者型別
private String loginIp;    // 最後登陸IP
private Date loginDate;    // 最後登陸日期
private String loginFlag;  // 是否允許登陸
private String photo;  // 頭像
Procurement:
private String id; 
private String procurementCode;//採購單號
private String procurementType;//加急,普通
@JsonFormat(pattern ="yyyy-MM-dd ",timezone="GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd ")
private Date createTime;
private Integer createById;
@JsonFormat(pattern ="yyyy-MM-dd ",timezone="GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd ")
private Date auditTime;//稽核時間
private Integer auditById;//稽核人
private Integer status;
private List<Materials> MaterialsList;
private User user;
private User user1;

JSP:頁面:

<table id="contentTable" class="table table-striped table-bordered table-condensed">
   <thead><tr>
      <th>採購單號</th>
      <th>採購型別</th>
      <th>建立時間</th>
      <th>建立人</th>
      <th>稽核時間</th>
      <th>稽核人</th>
      <th>稽核狀態</th>
      <th>操作</th>
   </tr></thead>
   <tbody>
   <c:forEach items="${page.list}" var="procurement">
      <tr>
         <td>${procurement.procurementCode }</td>
         <td>
            <c:if test="${procurement.procurementType==1}">加急</c:if>
            <c:if test="${procurement.procurementType==0}">普通</c:if>
         </td>
         <td><fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss"
                        value="${procurement.createTime }" /></td>
         <td>${procurement.user.name}</td>
         <td><fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss"
                        value="${procurement.auditTime }" /></td>
         <td>${procurement.user1.name }</td>
         <td>
            <c:if test="${procurement.status==2}"><span style="color: red">已作廢</span></c:if>
            <c:if test="${procurement.status==1}"><span style="color: blue">已稽核</span></c:if>
            <c:if test="${procurement.status==0}"><span style="color: #B2CBFF">未稽核</span></c:if>
            <c:if test="${procurement.status==3}"><span style="color: #B2CBFF">已入庫</span></c:if>
         </td>
         <td>

               <a href="${ctx}/mm/procurement/view?procurementCode=${procurement.procurementCode }">檢視詳情</a>

            <c:if test="${procurement.status==1}"><%--已稽核可以檢視和入庫操作--%>

               <a href="${ctx}/mm/procurement/warehouse?procurementCode=${procurement.procurementCode }">到貨入庫</a>
               <a href="${ctx}/mm/procurement/cancellation?id=${procurement.id}" onclick="return confirmx('確認要作廢該採購單嗎?', this.href)">作廢</a>
            </c:if>
            <c:if test="${procurement.status<1}"><%--未稽核和已經稽核的可以操作--%>

               <a href="${ctx}/mm/procurement/cancellation?id=${procurement.id}" onclick="return confirmx('確認要作廢該採購單嗎?', this.href)">作廢</a>
            </c:if>

         </td>
      </tr>

   </c:forEach>
   </tbody>
</table>