package cn.hpu.jk.domain; public class ContractProduct { private String id; //private Contract contract;//将复杂的关联变成单表操作 private String contractId;//关联关系的表,都成为普通字段 private String factoryId; private String factoryName; private String productNo; private String productImage; private String productDesc;//货物描述 private Integer cnumber;//数量 private Integer outNumber;//出货数量 private String loadingRate;//装率 private String boxNum;//箱数 private String packingUnit;//包装单位 private Double price;//单价 private Double amount;//总价 private Integer finished;//是否出货完毕 private String exts;//附件 private Integer orderNo;//排序号 //get与set方法省略 }我们将关系对象的形式变成了一张单表,变得简单了(比hibernate效率高),我们的关键是如何将他们关联起来而已。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.hpu.jk.mapper.ContractProductMapper"> <!-- 映射合同和合同下的货物信息一对多 --> <resultMap type="cn.hpu.jk.domain.ContractProduct" id="ContractProductRM"> <id property="id" column="CONTRACT_PRODUCT_ID"/> <result property="contractId" column="CONTRACT_ID"/> <result property="factoryId" column="FACTORY_ID"/> <result property="factoryName" column="FACTORY_NAME"/> <result property="productNo" column="PRODUCT_NO"/> <result property="productImage" column="PRODUCT_IMAGE"/> <result property="productDesc" column="PRODUCT_DESC"/> <result property="cnumber" column="CNUMBER"/> <result property="outNumber" column="OUT_NUMBER"/> <result property="loadingRate" column="LOADING_RATE"/> <result property="boxNum" column="BOX_NUM"/> <result property="packingUnit" column="PACKING_UNIT"/> <result property="price" column="PRICE"/> <result property="amount" column="AMOUNT"/> <result property="finished" column="FINISHED"/> <result property="exts" column="EXTS"/> <result property="orderNo" column="ORDER_NO"/> </resultMap> <!-- 查询 --> <select id="find" parameterType="map" resultMap="ContractProductRM"> select * from contract_product_c where 1=1 </select> <!-- 查询一个 --> <select id="get" parameterType="String" resultMap="ContractProductRM"> select * from contract_product_c where contract_product_id=#{id} </select> <insert id="insert" parameterType="cn.hpu.jk.domain.ContractProduct"> insert into contract_product_c (CONTRACT_PRODUCT_ID,CONTRACT_ID,FACTORY_ID,FACTORY_NAME,PRODUCT_NO, PRODUCT_IMAGE,PRODUCT_DESC,CNUMBER,OUT_NUMBER,LOADING_RATE,BOX_NUM, PACKING_UNIT,PRICE,AMOUNT,FINISHED,EXTS,ORDER_NO) values( #{id}, #{contractId}, #{factoryId}, #{factoryName, jdbcType=VARCHAR}, #{productNo, jdbcType=VARCHAR}, #{productImage, jdbcType=VARCHAR}, #{productDesc, jdbcType=VARCHAR}, #{cnumber, jdbcType=INTEGER}, #{outNumber, jdbcType=INTEGER}, #{loadingRate, jdbcType=VARCHAR}, #{boxNum, jdbcType=INTEGER}, #{packingUnit, jdbcType=VARCHAR}, #{price, jdbcType=DOUBLE}, #{amount, jdbcType=DOUBLE}, #{finished, jdbcType=INTEGER}, #{exts, jdbcType=VARCHAR}, #{orderNo, jdbcType=INTEGER} ) </insert> <update id="update" parameterType="cn.hpu.jk.domain.ContractProduct"> update contract_product_c <set> CONTRACT_ID=#{contractId}, FACTORY_ID=#{factoryId}, <if test="factoryName != null">FACTORY_NAME=#{factoryName},</if> <if test="productNo != null">PRODUCT_NO=#{productNo},</if> <if test="productImage != null">PRODUCT_IMAGE=#{productImage},</if> <if test="productDesc != null">PRODUCT_DESC=#{productDesc},</if> <if test="cnumber != null">CNUMBER=#{cnumber},</if> <if test="outNumber != null">OUT_NUMBER=#{outNumber},</if> <if test="loadingRate != null">LOADING_RATE=#{loadingRate},</if> <if test="boxNum != null">BOX_NUM=#{boxNum},</if> <if test="packingUnit != null">PACKING_UNIT=#{packingUnit},</if> <if test="price != null">PRICE=#{price},</if> <if test="amount != null">AMOUNT=#{amount},</if> <if test="finished != null">FINISHED=#{finished},</if> <if test="exts != null">EXTS=#{exts},</if> <if test="orderNo != null">ORDER_NO=#{orderNo},</if> </set> where contract_product_id=#{id} </update> <!-- 删除一条 --> <delete id="deleteById" parameterType="string"> delete from contract_product_c where contract_product_id=#{id} </delete> <!-- 删除多条(一维字符串数组) --> <delete id="delete" parameterType="string"> delete from contract_product_c where contract_product_id in <foreach collection="array" item="id" open="(" close=")" separator=","> #{id} </foreach> </delete> </mapper>
package cn.hpu.jk.dao; import cn.hpu.jk.domain.Contract; public interface ContractProductDao extends BaseDao<ContractProduct>{ //我们这里可以直接使用继承的BaseDao的增删改查方法 //创建一系列其它Dao的原因是因为其它Dao有可能需要拓展 }
package cn.hpu.jk.dao.impl; import org.springframework.stereotype.Repository; import cn.hpu.jk.dao.ContractProductDao; import cn.hpu.jk.domain.ContractProduct; @Repository //为了包扫描的时候这个Dao被扫描到 public class ContractProductDaoImpl extends BaseDaoImpl<ContractProduct> implements ContractProductDao{ public ContractProductDaoImpl(){ //设置命名空间 super.setNs("cn.hpu.jk.mapper.ContractProductMapper"); } }
package cn.hpu.jk.service; import java.io.Serializable; import java.util.List; import java.util.Map; import cn.hpu.jk.domain.Contract; import cn.hpu.jk.domain.ContractProduct; import cn.hpu.jk.pagination.Page; public interface ContractProductService { public List<ContractProduct> findPage(Page page); //分页查询 public List<ContractProduct> find(Map paraMap); //带条件查询,条件可以为null,既没有条件;返回list对象集合 public ContractProduct get(Serializable id); //只查询一个,常用于修改 public void insert(ContractProduct contractProduct); //插入,用实体作为参数 public void update(ContractProduct contractProduct); //修改,用实体作为参数 public void deleteById(Serializable id); //按id删除,删除一条;支持整数型和字符串类型ID public void delete(Serializable[] ids); //批量删除;支持整数型和字符串类型ID }
ContractProductServiceImpl.java:
package cn.hpu.jk.service.impl; import java.io.Serializable; import java.util.List; import java.util.Map; import java.util.UUID; import javax.annotation.Resource; import org.springframework.stereotype.Service; import cn.hpu.jk.dao.ContractProductDao; import cn.hpu.jk.domain.ContractProduct; import cn.hpu.jk.pagination.Page; import cn.hpu.jk.service.ContractProductService; @Service public class ContractProductServiceImpl implements ContractProductService{ @Resource ContractProductDao contractProductDao; @Override public void delete(Serializable[] ids) { contractProductDao.delete(ids); } @Override public void deleteById(Serializable id) { contractProductDao.deleteById(id); } @Override public List<ContractProduct> find(Map paraMap) { return contractProductDao.find(paraMap); } @Override public List<ContractProduct> findPage(Page page) { // TODO Auto-generated method stub return null; } @Override public ContractProduct get(Serializable id) { return contractProductDao.get(id); } @Override public void insert(ContractProduct contractProduct) { //设置UUID contractProduct.setId(UUID.randomUUID().toString()); contractProductDao.insert(contractProduct); } @Override public void update(ContractProduct contractProduct) { contractProductDao.update(contractProduct); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 加载service--> <bean name="factoryService" class="cn.hpu.jk.service.impl.FactoryServiceImpl"/> <bean name="contractService" class="cn.hpu.jk.service.impl.ContractServiceImpl"/> <bean name="contractProductService" class="cn.hpu.jk.service.impl.ContractProductServiceImpl"/> </beans>
package cn.hpu.jk.controller.cargo.contract; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import cn.hpu.jk.controller.BaseController; import cn.hpu.jk.domain.ContractProduct; import cn.hpu.jk.service.ContractProductService; @Controller public class ContractProductController extends BaseController{ @Autowired ContractProductService contractProductService; //转向新增页面 @RequestMapping("/cargo/contractproduct/tocreate.action") public String tocreate(String contractId,Model model){ //传递购销合同Id model.addAttribute("contractId", contractId); return "/cargo/contract/jContractProductCreate.jsp";//货物的新增页面 } //新增 @RequestMapping("/cargo/contractproduct/insert.action") public String insert(ContractProduct contractProduct,Model model){ contractProductService.insert(contractProduct); model.addAttribu("contractId", contractProduct.getContractId()); return "redirect:/cargo/contractproduct/tocreate.action"; } }
<!-- 查询 --> <select id="find" parameterType="map" resultMap="factoryRM"> select * from factory_c where 1=1 <if test="state != null"> and STATE=#{state}</if> </select>即保证了单独跳向生产厂家页面的时候取所有,又保证带state参数跳向生产厂家列表的时候按条件取数据。
在FactoryService中添加
public List<Factory> getFactoryList(); //获取生产厂家列表
在FactoryServiceImpl中添加
@Override public List<Factory> getFactoryList() { Map<String,Object> paraMap=new HashMap<String,Object>(); paraMap.put("state", 1);//1启用,代表只查询启用的生产厂家 return factoryDao.find(paraMap); }
package cn.hpu.jk.controller.cargo.contract; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import cn.hpu.jk.controller.BaseController; import cn.hpu.jk.domain.ContractProduct; import cn.hpu.jk.domain.Factory; import cn.hpu.jk.service.ContractProductService; import cn.hpu.jk.service.FactoryService; @Controller public class ContractProductController extends BaseController{ @Autowired ContractProductService contractProductService; @Autowired FactoryService factoryService; //转向新增页面 @RequestMapping("/cargo/contractproduct/tocreate.action") public String tocreate(String contractId,Model model){ //传递购销合同Id model.addAttribute("contractId", contractId); //准备生产厂家的下拉列表 List<Factory> factoryList=factoryService.getFactoryList(); model.addAttribute("factoryList",factoryList); return "/cargo/contract/jContractProductCreate.jsp";//货物的新增页面 } //新增 @RequestMapping("/cargo/contractproduct/insert.action") public String insert(ContractProduct contractProduct,Model model){ contractProductService.insert(contractProduct); model.addAttribu("contractId", contractProduct.getContractId()); return "redirect:/cargo/contractproduct/tocreate.action"; } }
<!-- 查询某个合同下的货物信息 --> <select id="find" parameterType="map" resultMap="ContractProductRM"> select * from contract_product_c where 1=1 <if test="contractId != null"> and contractId=#{CONTRACT_ID}</if> </select>
我们在修改Controller来在转向货物编辑页面时带着相应合同的货物列表过去:
//转向新增页面 @RequestMapping("/cargo/contractproduct/tocreate.action") public String tocreate(String contractId,Model model){ //传递购销合同Id model.addAttribute("contractId", contractId); //准备生产厂家的下拉列表 List<Factory> factoryList=factoryService.getFactoryList(); model.addAttribute("factoryList",factoryList); //某个合同下的货物 Map paraMap=new HashMap(); paraMap.put("contractId", contractId); List<ContractProduct> dataList=contractProductService.find(paraMap); model.addAttribute("dataList", dataList); return "/cargo/contract/jContractProductCreate.jsp";//货物的新增页面 }
下面是最终的编辑新增页面jContractProductCreate.jsp:
<%@ page language="java" pageEncoding="UTF-8"%> <%@ include file="../../base.jsp"%> <%@ include file="../../baselist.jsp"%> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>添加货物信息</title> <script type="text/javascript"> //设置冗余的生产厂家名称 function setFactoryName(val){ var ele=document.getElementById("factoryName"); ele.value=val; } </script> </head> <body> <form method="post"> <div id="menubar"> <div id="middleMenubar"> <div id="innerMenubar"> <div id="navMenubar"> <ul> <li id="save"><a href="#" onclick="formSubmit('insert.action','_self');">确定</a></li> <li id="back"><a href="list.action">返回</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> <table class="commonTable" cellspacing="1"> <input type="hidden" name="contractId" value="${contractId}"/> <tr> <td class="columnTitle_mustbe">厂家名称:</td> <td class="tableContent"> <select name="factoryId" onchange="setFactoryName(this.options[this.selectedIndex].text);"> <option value="">--请选择--</option> <c:forEach items="${factoryList}" var="f"> <option value="${f.id}">${f.factoryName }</option> </c:forEach> <input type="hidden" id="factoryName" name="factoryName" value=""/> </select> </td> <td class="columnTitle_mustbe">货号:</td> <td class="tableContent"><input type="text" name="productNo" /></td> </tr> <tr> <td class="columnTitle_mustbe">货物照片:</td> <td class="tableContent"><input type="text" name="productImage" /></td> </tr> <tr> <td class="columnTitle_mustbe">数量</td> <td class="tableContent"><input type="text" name="cnumber" /></td> <td class="columnTitle_mustbe">装率:</td> <td class="tableContent"><input type="text" name="loadingRate" /></td> </tr> <tr> <td class="columnTitle_mustbe">箱数:</td> <td class="tableContent"><input type="text" name="boxNum" /></td> <td class="columnTitle_mustbe">单价:</td> <td class="tableContent"><input type="text" name="price" /></td> </tr> <tr> <td class="columnTitle_mustbe">包装单位:</td> <td class="tableContent"><input type="text" name="packingUnit" /></td> <td class="columnTitle_mustbe">排序号:</td> <td class="tableContent"><input type="text" name="orderNo" /></td> </tr> <tr> <td class="columnTitle_mustbe">货物描述:</td> <td class="tableContent"><textarea name="productDesc" style="height:200px;width: 400px"></textarea></td> </tr> </table> </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> </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.factoryName}</td> <td>${o.productNo}</td> <td>${o.cnumber}</td> <td>${o.packingUnit}</td> <td>${o.loadingRate }</td> <td>${o.boxNum }</td> <td>${o.price }</td> <td>${o.amount}</td> </tr> </c:forEach> </tbody> </table> </div> </div> </form> </body> </html>
<tr> <td class="tableHeader">操作</td> </tr> <tr> <td><a href="${ctx}/cargo/contractproduct/tocreate.action?contractId=${o.id}" title="新增货物信息">[货物]</a><td></tr>
@Override public void insert(ContractProduct contractProduct) { //设置UUID contractProduct.setId(UUID.randomUUID().toString()); //自动计算总金额=数量*单价 ...修改,删除;同步合同金额 if(UtilFuns.isNotEmpty(contractProduct.getCnumber()) &&UtilFuns.isNotEmpty(contractProduct.getCnumber())){ contractProduct.setAmount(contractProduct.getCnumber()*contractProduct.getPrice()); } contractProductDao.insert(contractProduct); } @Override public void update(ContractProduct contractProduct) { //自动计算总金额=数量*单价 ...修改,删除;同步合同金额 if(UtilFuns.isNotEmpty(contractProduct.getCnumber()) &&UtilFuns.isNotEmpty(contractProduct.getCnumber())){ contractProduct.setAmount(contractProduct.getCnumber()*contractProduct.getPrice()); } contractProductDao.update(contractProduct); }
public static boolean isNotEmpty(String str){ try{ if(str==null || str.equals("null") || str.equals("")){ return false; } return true; }catch(Exception e){ return true; } } public static boolean isNotEmpty(Object obj){ try{ if(obj==null || obj.toString().equals("null") || obj.toString().equals("")){ return false; } return true; }catch(Exception e){ return true; } }
至此,我们完成了随购销合同的货物的主要业务功能
转载请注明出处:http://blog.csdn.net/acmman/article/details/48521559