事件背景:<table>檢索到的數據列表,主表存在關系表的id或者code情況,如訂單主表,存在會員id,為了顯示更友好,需要根據id顯示name。由於前期數據不完善或者存在數據刪除情況,導致傳統的關聯查詢如inner join查的數據不全。
解決方式1:
select a.*,b.name as name from a inner join b where 1=1 and ... union select a.*,null as name from a where 1=1 and not exists (select * from b where a.name_id = b.id) and ...
然后entity里賦值name,傳入前台,直接顯示name
解決方式2:
controller page方法里,增加關聯表的list,傳入前台
<c:forEach items="${page.list}" var="var" varStatus="vs"> <tr> <td>${var.orderId}</td> ... <c:forEach items="${shoplist}" var="shop"> <c:if test="${var.shopId == shop.id }"> <c:set var="count" scope="session" value="1"/> <td>${shop.name}</td> </c:if> </c:forEach> <c:if test="${count ne 1}"> <td></td> </c:if> <c:set var="count" scope="session" value="0"/> </tr> </c:forEach>
使用<c:set 臨時賦值count,表示當前取到了值,然后后面重置count=0,防止count值帶到下一條數據。