我们每次删除完都要去下一级或者数据库看看我们删除了没有,我们可以直接在合同列表中显示每个合同下的货物以及货物的附件有多少件,这样就避免了反复去下一级查看列表的繁琐操作。
那么下面我们使用SQL来编写获取合同下的货物数:
我们在购销合同列表下的每一行合同信息中增加一列,专门用来显示“货物数/附件数”(前后HTML代码省略):
那么这就要求,在我们取出购销合同列表的时候就应该把货物数/附件数一并取出,那么我们就要写一个SQL综合一下刚刚我们写的两个分散的SQL,让我们能一次性取出某个购销合同的货物数/附件数:
可以看到我们新增了两个字段--货物数cpnum以及附件数extnum,但是我们的ContractMapper的映射文件的ResultMapping中没有这两个字段:
所以我们要添加字段,并且我们的实体也要增加这两个字段(这里我们的实体改变是因为我们的实体仅仅是VO对象,并不是PO对象)
提到VO与PO,我们这里多说几句:
【面试】PO、VO、BO有什么区别?
PO 持久化对象,一般就直接对象数据库表
VO 视图对象,一般对应页面jsp
BO 业务对象,一般对应复杂业务
好了,这里我们开始修改我们的find配置语句,然后增加货物数cpnum以及附件数extnum的result:
我们这里改完,接下来修改我们的实体:
可以看到,我们的货物数cpnum以及附件数extnum是String类型,但是实际上我们数据库统计的数量和应该是Int甚至是long,这里要注意的是,MyBatis它不以数据库取出的数据类型作为最终类型,而是以实体类中的类型最为最终类型,即硬性将数据转换为实体类规定的类型。现在我们因为不参加计算,只进行查看,所以为了简单起见,我们只需要String类型的数据就可以了:
我们在回顾一下jsp页面:
我们重启服务器查看一下:
那么下面我们使用SQL来编写获取合同下的货物数:
- select count(*) from contract_product_c
- where contract_id='928eb2ae-23ba-46e0-9ad0-054354f66af8'
获取合同下的货物下的附件数:
- select count(*) from ext_cproduct_c
- where contract_product_id in (select contract_product_id from contract_product_c where contract_id='928eb2ae-23ba-46e0-9ad0-054354f66af8')
我们在购销合同列表下的每一行合同信息中增加一列,专门用来显示“货物数/附件数”(前后HTML代码省略):
- <span style="white-space:pre"> </span><tr>
- <td class="tableHeader">货物数/附件数</td>
- </tr>
- <tr>
- <td>${o.cpnum }/${o.extnum }</td>
- </tr>
效果如图
那么这就要求,在我们取出购销合同列表的时候就应该把货物数/附件数一并取出,那么我们就要写一个SQL综合一下刚刚我们写的两个分散的SQL,让我们能一次性取出某个购销合同的货物数/附件数:
- select
- (select count(*) from contract_product_c
- where contract_id=c.contract_id)as cpnum,
- (select count(*) from ext_cproduct_c
- where contract_product_id in( select contract_product_id from
- contract_product_c where contract_id=c.contract_id))as extnum,
- c.*
- from contract_c c
可以看到我们新增了两个字段--货物数cpnum以及附件数extnum,但是我们的ContractMapper的映射文件的ResultMapping中没有这两个字段:
- <mapper namespace="cn.hpu.jk.mapper.ContractMapper">
- <resultMap type="cn.hpu.jk.domain.Contract" id="contractRM">
- <id property="id" column="CONTRACT_ID"/>
- <result property="offeror" column="OFFEROR"/>
- <result property="contractNo" column="CONTRACT_NO"/>
- <result property="signingDate" column="SIGNING_DATE"/>
- <result property="inputBy" column="INPUT_BY"/>
- <result property="checkBy" column="CHECK_BY"/>
- <result property="inspector" column="INSPECTOR"/>
- <result property="totalAmount" column="TOTAL_AMOUNT"/>
- <result property="importNum" column="IMPORT_NUM"/>
- <result property="crequest" column="CREQUEST"/>
- <result property="customName" column="CUSTOM_NAME"/>
- <result property="deliveryPeriod" column="DELIVERY_PERIOD"/>
- <result property="shipTime" column="SHIP_TIME"/>
- <result property="tradeTerms" column="TRADE_TERMS"/>
- <result property="remark" column="REMARK"/>
- <result property="printStyle" column="PRINT_STYLE"/>
- <result property="oldState" column="OLD_STATE"/>
- <result property="state" column="STATE"/>
- <result property="outState" column="OUT_STATE"/>
- <result property="createBy" column="CREATE_BY"/>
- <result property="creatDept" column="CREATE_DEPT"/>
- <result property="creatTime" column="CREATE_TIME"/>
- </resultMap>
- <!-- 查询多个 -->
- <select id="find" parameterType="map" resultMap="contractRM">
- select * from contract_c
- where 1=1
- </select>
所以我们要添加字段,并且我们的实体也要增加这两个字段(这里我们的实体改变是因为我们的实体仅仅是VO对象,并不是PO对象)
提到VO与PO,我们这里多说几句:
【面试】PO、VO、BO有什么区别?
PO 持久化对象,一般就直接对象数据库表
VO 视图对象,一般对应页面jsp
BO 业务对象,一般对应复杂业务
好了,这里我们开始修改我们的find配置语句,然后增加货物数cpnum以及附件数extnum的result:
- </pre><pre name="code" class="html"><mapper namespace="cn.hpu.jk.mapper.ContractMapper">
- <resultMap type="cn.hpu.jk.domain.Contract" id="contractRM">
- <id property="id" column="CONTRACT_ID"/>
- <result property="offeror" column="OFFEROR"/>
- <!-- 虚拟字段 -->
- <result property="contractNo" column="CONTRACT_NO"/>
- <result property="cpnum" column="CPNUM"/>
- <result property="extnum" column="EXTNUM"/>
- <result property="signingDate" column="SIGNING_DATE"/>
- <result property="inputBy" column="INPUT_BY"/>
- <result property="checkBy" column="CHECK_BY"/>
- <result property="inspector" column="INSPECTOR"/>
- <result property="totalAmount" column="TOTAL_AMOUNT"/>
- <result property="importNum" column="IMPORT_NUM"/>
- <result property="crequest" column="CREQUEST"/>
- <result property="customName" column="CUSTOM_NAME"/>
- <result property="deliveryPeriod" column="DELIVERY_PERIOD"/>
- <result property="shipTime" column="SHIP_TIME"/>
- <result property="tradeTerms" column="TRADE_TERMS"/>
- <result property="remark" column="REMARK"/>
- <result property="printStyle" column="PRINT_STYLE"/>
- <result property="oldState" column="OLD_STATE"/>
- <result property="state" column="STATE"/>
- <result property="outState" column="OUT_STATE"/>
- <result property="createBy" column="CREATE_BY"/>
- <result property="creatDept" column="CREATE_DEPT"/>
- <result property="creatTime" column="CREATE_TIME"/>
- </resultMap>
- <!-- 查询多个 -->
- <select id="find" parameterType="map" resultMap="contractRM">
- select
- (select count(*) from contract_product_c
- where contract_id=c.contract_id)as cpnum,
- (select count(*) from ext_cproduct_c
- where contract_product_id in( select contract_product_id from
- contract_product_c where contract_id=c.contract_id))as extnum,
- c.*
- from contract_c c
- </select>
- <mapper>
可以看到,我们的货物数cpnum以及附件数extnum是String类型,但是实际上我们数据库统计的数量和应该是Int甚至是long,这里要注意的是,MyBatis它不以数据库取出的数据类型作为最终类型,而是以实体类中的类型最为最终类型,即硬性将数据转换为实体类规定的类型。现在我们因为不参加计算,只进行查看,所以为了简单起见,我们只需要String类型的数据就可以了:
- package cn.hpu.jk.domain;
- import java.util.Date;
- public class Contract {
- private String id;
- private String offeror;//收购方
- private String contractNo;//合同编号
- private String cpnum;
- private String extnum;
- private java.util.Date signingDate;//签单日期
- private String inputBy;//制单人
- private String checkBy;//审单人
- private String inspector;//验货员
- private Double totalAmount;//总金额
- private Integer importNum;//重要程度
- private String crequest;//要求
- private String customName;
- private java.util.Date deliveryPeriod;//交货期限
- private java.util.Date shipTime;//船期
- private String tradeTerms;//贸易条款
- private String remark;//说明
- private String printStyle;//打印版式
- private Integer oldState;//归档前状态
- private Integer state;//状态
- private Integer outState;//走货状态
- private String createBy;
- private String creatDept;
- private java.util.Date creatTime;
- //...get和set方法省略
- }
我们在回顾一下jsp页面:
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ include file="../../baselist.jsp"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- </head>
- <body>
- <form name="icform" method="post">
- <div id="menubar">
- <div id="middleMenubar">
- <div id="innerMenubar">
- <div id="navMenubar">
- <ul>
- <li id="view"><a href="#" onclick="formSubmit('toview.action','_self');this.blur();">查看</a></li>
- <li id="new"><a href="#" onclick="formSubmit('tocreate.action','_self');this.blur();">添加</a></li>
- <li id="update"><a href="#" onclick="formSubmit('toupdate.action','_self');this.blur();">修改</a></li>
- <li id="delete"><a href="#" onclick="formSubmit('delete.action','_self');this.blur();">删除</a></li>
- <li id="new"><a href="#" onclick="formSubmit('submit.action','_self');this.blur();">上报</a></li>
- <li id="new"><a href="#" onclick="formSubmit('cancel.action','_self');this.blur();">取消</a></li>
- </ul>
- </div>
- </div>
- </div>
- </div>
- <!-- 页面主体部分(列表等) -->
- <div class="textbox" id="centerTextbox">
- <div class="textbox-header">
- <div class="textbox-inner-header">
- <div class="textbox-title">
- 销售合同列表
- </div>
- </div>
- </div>
- <div>
- <div class="eXtremeTable" >
- <table id="ec_table" class="tableRegion" width="98%" >
- <thead>
- <tr>
- <td class="tableHeader"><input type="checkbox" name="selid" onclick="checkAll('id',this)"></td>
- <td class="tableHeader">序号</td>
- <td class="tableHeader">客户名称</td>
- <td class="tableHeader">合同号</td>
- <td class="tableHeader">货物数/附件数</td>
- <td class="tableHeader">制单人</td>
- <td class="tableHeader">审单人</td>
- <td class="tableHeader">验货员</td>
- <td class="tableHeader">签单日期</td>
- <td class="tableHeader">交货期限</td>
- <td class="tableHeader">船期</td>
- <td class="tableHeader">总金额</td>
- <td class="tableHeader">状态</td>
- <td class="tableHeader">操作</td>
- </tr>
- </thead>
- <tbody class="tableBody" >
- <c:forEach items="${datalist}" var="o" varStatus="status">
- <tr class="odd" onmouseover="this.className='highlight'" onmouseout="this.className='odd'" >
- <td><input type="checkbox" name="id" value="${o.id}"/></td>
- <td>${status.index+1}</td>
- <td>${o.customName}</td>
- <td><a href="toview.action?id=${o.id}">${o.contractNo}</a></td>
- <td>${o.cpnum }/${o.extnum }</td>
- <td>${o.inputBy}</td>
- <td>${o.checkBy}</td>
- <td>${o.inspector}</td>
- <td><fmt:formatDate value="${o.signingDate}" pattern="yyyy-MM-dd"/></td>
- <td><fmt:formatDate value="${o.deliveryPeriod}" pattern="yyyy-MM-dd"/></td>
- <td><fmt:formatDate value="${o.shipTime}" pattern="yyyy-MM-dd"/></td>
- <td>${o.totalAmount}</td>
- <td>
- <c:if test="${o.state==1}"><font color="green">已上报</font></c:if>
- <c:if test="${o.state==0}">草稿</a></c:if>
- </td>
- <td><a href="${ctx}/cargo/contractproduct/tocreate.action?contractId=${o.id}" title="新增货物信息">[货物]</a><td>
- </tr>
- </c:forEach>
- </tbody>
- </table>
- </div>
- </div>
- </form>
- </body>
- </html>
我们重启服务器查看一下:
我们的货物和附件数显示功能成功!