我们接下来开始编写购销合同的列表业务 首先我们现往我们的FACTORY_C和基础表中倒入一些数据方便我们测试 导入数据的SQL文件: jk.sql (点击下载) 然后我们看到数据已经插入
启动服务器查看我们的数据已经插入
接下来我们开始做购销合同的业务。首先我们的数据库表已经建立(CONTRACT_C),我们首先创建购销合同的实体类Contract.java:
package cn.hpu.jk.domain;import java.util.Date;public class Contract { private String id; private String offeror;//收购方 private String contractNo;//合同编号 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方法省略}然后建立我们的购销合同的Mapper映射文件ContractMapper.xml,编写后期需要使用的resultMap:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><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></mapper>接下来我们在ContractMapper.xml中添加增删改查语句:
<!-- 查询多个 --><select id="find" parameterType="map" resultMap="contractRM"> select * from contract_c where 1=1</select><!-- 查询一个 --><select id="get" parameterType="string" resultMap="contractRM"> select * from contract_c where contract_id=#{id}</select><!-- 新增 --><insert id="insert" parameterType="cn.hpu.jk.domain.Contract"> insert into contract_c (CONTRACT_ID,,OFFEROR,CONTRACT_NO,SIGNING_DATE,INPUT_BY,CHECK_BY,INSPECTOR,TOTAL_AMOUNT, IMPORT_NUM,CREQUEST,CUSTOM_NAME,DELIVERY_PERIOD,SHIP_TIME,TRADE_TERMS,REMARK,PRINT_STYLE, OLD_STATE,STATE,OUT_STATE,CREATE_BY,CREATE_DEPT,CREATE_TIME) values ( #{id}, #{offeror, jdbcType=VARCHAR}, #{contractNo, jdbcType=VARCHAR}, #{signingDate, jdbcType=TIMESTAMP}, #{inputBy, jdbcType=VARCHAR}, #{checkBy, jdbcType=VARCHAR}, #{inspector, jdbcType=VARCHAR}, #{totalAmount, jdbcType=DOUBLE}, #{importNum, jdbcType=INTEGER}, #{crequest, jdbcType=VARCHAR}, #{customName, jdbcType=VARCHAR}, #{deliveryPeriod, jdbcType=TIMESTAMP}, #{shipTime, jdbcType=TIMESTAMP}, #{tradeTerms, jdbcType=VARCHAR}, #{remark, jdbcType=VARCHAR}, #{printStyle, jdbcType=CHAR}, #{oldState, jdbcType=INTEGER}, #{state, jdbcType=INTEGER}, #{outState, jdbcType=INTEGER}, #{createBy, jdbcType=VARCHAR}, #{creatDept, jdbcType=VARCHAR}, #{creatTime, jdbcType=TIMESTAMP} )</insert><!-- 修改 --><update id="update" parameterType="cn.hpu.jk.domain.Contract"> update contract_c <set> <if test="offeror != null">OFFEROR=#{offeror},</if> <if test="contractNo != null">CONTRACT_NO=#{contractNo},</if> <if test="signingDate != null">SIGNING_DATE=#{signingDate},</if> <if test="inputBy != null">INPUT_BY=#{inputBy},</if> <if test="checkBy != null">CHECK_BY=#{checkBy},</if> <if test="inspector != null">INSPECTOR=#{inspector},</if> <if test="totalAmount != null">TOTAL_AMOUNT=#{totalAmount},</if> <if test="importNum != null">IMPORT_NUM=#{importNum},</if> <if test="crequest != null">CREQUEST=#{crequest},</if> <if test="customName != null">CUSTOM_NAME=#{customName},</if> <if test="deliveryPeriod != null">DELIVERY_PERIOD=#{deliveryPeriod},</if> <if test="shipTime != null">SHIP_TIME=#{shipTime},</if> <if test="tradeTerms != null">TRADE_TERMS=#{tradeTerms},</if> <if test="remark != null">REMARK=#{remark},</if> <if test="printStyle != null">PRINT_STYLE=#{printStyle},</if> <if test="oldState != null">OLD_STATE=#{oldState},</if> <if test="state != null">STATE=#{state},</if> <if test="outState != null">OUT_STATE=#{outState},</if> </set> where contract_id=#{id}</update><!-- 删除一条 --><delete id="deleteById" parameterType="string"> delete from contract_c where CONTRACT_ID=#{id}</delete><!-- 删除多条(一维字符串数组的形式) --><delete id="delete" parameterType="string"> delete from contract_c where CONTRACT_ID in <foreach collection="array" item="id" open="(" close=")" separator=","> #{id} </foreach></delete><!-- 修改状态 --><update id="updateState" parameterType="map"> update contract_c set state=#{state} where CONTRACT_ID in <foreach collection="ids" item="id" open="(" close=")" separator=","> #{id} </foreach></update>接下来我们来编写Dao层
ContractDao.java:
package cn.hpu.jk.dao;import java.util.Map;import cn.hpu.jk.domain.Contract;public interface ContractDao extends BaseDao<Contract>{ //我们这里可以直接使用继承的BaseDao的增删改查方法 //创建一系列其它Dao的原因是因为其它Dao有可能需要拓展 public void updateState(Map map);//修改状态}ContractDaoImpl.java:
package cn.hpu.jk.dao.impl;import java.util.Map;import org.springframework.stereotype.Repository;import cn.hpu.jk.dao.ContractDao;import cn.hpu.jk.domain.Contract;@Repository //为了包扫描的时候这个Dao被扫描到public class ContractDaoImpl extends BaseDaoImpl<Contract> implements ContractDao{ public ContractDaoImpl(){ //设置命名空间 super.setNs("cn.hpu.jk.mapper.ContractMapper"); } @Override public void updateState(Map map) { super.getSqlSession().update(super.getNs()+".updateState", map); }}回顾一下其中的BaseDao与 BaseDaoImpl
BaseDao.java:
package cn.hpu.jk.dao;import java.io.Serializable;import java.util.List;import java.util.Map;import cn.hpu.jk.pagination.Page;/** * @Description: 泛型类,基础的DAO接口 * @Author: nutony * @Company: http://java.itcast.cn * @CreateDate: 2014-2-25 */public interface BaseDao<T> { public List<T> findPage(Page page); //分页查询 public List<T> find(Map paraMap); //带条件查询,条件可以为null,既没有条件;返回list对象集合 public T get(Serializable id); //只查询一个,常用于修改 public void insert(T entity); //插入,用实体作为参数 public void update(T entity); //修改,用实体作为参数 public void deleteById(Serializable id); //按id删除,删除一条;支持整数型和字符串类型ID public void delete(Serializable[] ids); //批量删除;支持整数型和字符串类型ID}BaseDaoImpl.java:
package cn.hpu.jk.dao.impl;import java.io.Serializable;import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.support.SqlSessionDaoSupport;import org.springframework.beans.factory.annotation.Autowired;import cn.hpu.jk.dao.BaseDao;import cn.hpu.jk.pagination.Page;public abstract class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T>{ @Autowired //mybatis-spring 1.0无需此方法;mybatis-spring1.2必须注入。 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){ super.setSqlSessionFactory(sqlSessionFactory); } private String ns; //命名空间 public String getNs() { return ns; } public void setNs(String ns) { this.ns = ns; } public List<T> findPage(Page page){ List<T> oList = this.getSqlSession().selectList(ns + ".findPage", page); return oList; } public List<T> find(Map map) { List<T> oList = this.getSqlSession().selectList(ns + ".find", map); return oList; } public T get(Serializable id) { return this.getSqlSession().selectOne(ns + ".get", id); } public void insert(T entity) { this.getSqlSession().insert(ns + ".insert", entity); } public void update(T entity) { this.getSqlSession().update(ns + ".update", entity); } public void deleteById(Serializable id) { this.getSqlSession().delete(ns + ".deleteById", id); } public void delete(Serializable[] ids) { this.getSqlSession().delete(ns + ".delete", ids); }}接下来我们来编写Service层
ContractService.java:
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.pagination.Page;public interface ContractService { public List<Contract> findPage(Page page); //分页查询 public List<Contract> find(Map paraMap); //带条件查询,条件可以为null,既没有条件;返回list对象集合 public Contract get(Serializable id); //只查询一个,常用于修改 public void insert(Contract contract); //插入,用实体作为参数 public void update(Contract contract); //修改,用实体作为参数 public void deleteById(Serializable id); //按id删除,删除一条;支持整数型和字符串类型ID public void delete(Serializable[] ids); //批量删除;支持整数型和字符串类型ID public void submit(Serializable[] ids);//已上报 public void cancel(Serializable[] ids);//草稿}ContractServiceImpl.java:
package cn.hpu.jk.service.impl;import java.io.Serializable;import java.util.HashMap;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.ContractDao;import cn.hpu.jk.domain.Contract;import cn.hpu.jk.pagination.Page;import cn.hpu.jk.service.ContractService;@Servicepublic class ContractServiceImpl implements ContractService{ @Resource ContractDao contractDao; @Override public void delete(Serializable[] ids) { contractDao.delete(ids); } @Override public void deleteById(Serializable id) { contractDao.deleteById(id); } @Override public List<Contract> find(Map paraMap) { return contractDao.find(paraMap); } @Override public List<Contract> findPage(Page page) { // TODO Auto-generated method stub return null; } @Override public Contract get(Serializable id) { return contractDao.get(id); } @Override public void insert(Contract contract) { //设置UUID contract.setId(UUID.randomUUID().toString()); contract.setState(0);//默认为草稿 contractDao.insert(contract); } @Override public void update(Contract contract) { contractDao.update(contract); } @Override public void submit(Serializable[] ids) { //默认刚建完的是草稿,建完之后确定没问题就上报 //0草稿 1已上报 Map map=new HashMap(); map.put("state",1);//1已上报 map.put("ids", ids); contractDao.updateState(map); } @Override public void cancel(Serializable[] ids) { Map map=new HashMap(); map.put("state",0);//0 map.put("ids", ids); contractDao.updateState(map); }}然后在beans-service.xml增加contractService的注入
<?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"/></beans>接下来我们来编写Controller层
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.Contract;import cn.hpu.jk.service.ContractService;@Controllerpublic class ContractController extends BaseController{ @Autowired ContractService contractService; @RequestMapping("/cargo/contract/list.action") public String list(Model model){ List<Contract> datalist=contractService.find(null); model.addAttribute("datalist",datalist); return "/cargo/contract/jContractList.jsp"; }}我们先写了一个获取购销合同的方法,所以我们写一个购销合同列表显示的jsp页面,我们在src/main/webapp/WEBINF/pages/cargo/contract/下创建jContractList.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="#" οnclick="formSubmit('toview.action','_self');this.blur();">查看</a></li> <li id="new"><a href="#" οnclick="formSubmit('tocreate.action','_self');this.blur();">添加</a></li> <li id="update"><a href="#" οnclick="formSubmit('toupdate.action','_self');this.blur();">修改</a></li> <li id="delete"><a href="#" οnclick="formSubmit('delete.action','_self');this.blur();">删除</a></li> <li id="new"><a href="#" οnclick="formSubmit('submit.action','_self');this.blur();">上报</a></li> <li id="new"><a href="#" οnclick="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" οnclick="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> </tr> </thead> <tbody class="tableBody" > <c:forEach items="${datalist}" var="o" varStatus="status"> <tr class="odd" οnmοuseοver="this.className='highlight'" οnmοuseοut="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.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> </tr> </c:forEach> </tbody></table></div> </div> </form></body></html>我们的主界面用的是框架,所以我们在点击货运管理的时候在左侧添加相应功能,
我们在这里修改left.jsp:
修改之后的代码:
<pre name="code" class="html"><%@ page language="java" pageEncoding="UTF-8"%><%@ include file="../baselist.jsp" %><html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title> <script type="text/javascript" src="${ctx}/components/jquery-ui/jquery-1.2.6.js"></script></head> <body id="left_frame"><div class="PositionFrame_black" id="PositionFrame"></div> <!-- begin1 --><div id="sidebar" class="sidebar"> <div class="sidebar_t"> <div class="sidebar_t_l"></div> <div class="sidebar_t_c"></div> <div class="sidebar_t_r"></div> </div> <div class="panel"> <div class="panel_icon"><img src="${ctx}/skin/default/images/icon/document_into.png"/></div> <div class="panel-header"> <div class="panel-title">货运管理</div> <div class="panel-content"> <ul> <li><a href="${ctx }/cargo/contract/list.action" οnclick="linkHighlighted(this)" target="main" id="aa_1">购销合同</a></li> </ul> </div> </div> </div> <div class="sidebar_t"> <div class="sidebar_b_l"></div> <div class="sidebar_t_c"></div> <div class="sidebar_b_r"></div> </div> </div></body></html>
我们在数据库添加一些测试数据,看看能不能获取到数据:
insert into CONTRACT_C (CONTRACT_ID, OFFEROR, CONTRACT_NO, SIGNING_DATE, INPUT_BY, CHECK_BY, INSPECTOR, TOTAL_AMOUNT, IMPORT_NUM, CREQUEST, CUSTOM_NAME, DELIVERY_PERIOD, SHIP_TIME, TRADE_TERMS, REMARK, PRINT_STYLE, OLD_STATE, STATE, OUT_STATE, CREATE_BY, CREATE_DEPT, CREATE_TIME)values ('22a2e6ab-9711-44ec-adc7-f3b43425765c', '杰信商贸有限公司', '2', to_timestamp('06-10-2014 00:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), null, null, null, null, 3, null, '1', to_timestamp('15-10-2014 00:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('05-11-2014 00:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), null, null, '2', null, 0, null, null, null, null);insert into CONTRACT_C (CONTRACT_ID, OFFEROR, CONTRACT_NO, SIGNING_DATE, INPUT_BY, CHECK_BY, INSPECTOR, TOTAL_AMOUNT, IMPORT_NUM, CREQUEST, CUSTOM_NAME, DELIVERY_PERIOD, SHIP_TIME, TRADE_TERMS, REMARK, PRINT_STYLE, OLD_STATE, STATE, OUT_STATE, CREATE_BY, CREATE_DEPT, CREATE_TIME)values ('a3a522f5-8748-4046-95d5-1bb21927bd56', '杰信商贸有限公司2', '12', to_timestamp('01-10-2014 00:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), '12', '12', '22', null, 2, '12', '12', to_timestamp('02-10-2014 00:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('03-10-2014 00:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), '12', '12', '1', null, 1, null, null, null, null);commit;点击“货运管理”之后点击“购销合同”
之后获得数据
如果显示我们输入的测试数据,证明获取“购销合同”列表成功!