《软件工程综合实践》第三次小结

这是最后一次的小结,在这次的实践中,老师带领着我们编写了订单管理系统,其中实现的功能有新增订单信息,删除订单信息,查找订单信息,修改订单信息和将订单信息输出为excel表格。每个功能的实现依次都是从网页上触发相应action然后action启动相应的配置服务,服务启动DAO来执行相应的功能对数据库中的数据进行修改。

新增订单信息的action类:

package com.crm.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.crm.bean.Cust;
import com.crm.bean.Type;
import com.crm.service.CustService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SaveCustAction extends ActionSupport{

private Cust cust;
private CustService saveService;
List strList = new ArrayList();
public Cust getCust() {
return cust;
}

public void setCust(Cust cust) {
this.cust = cust;
}

public CustService getSaveService() {
return saveService;
}
public void setSaveService(CustService saveService) {
this.saveService = saveService;
}

public List getStrList() {
return strList;
}

public void setStrList(List strList) {
this.strList = strList;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
ActionContext ctx=ActionContext.getContext();
HttpServletRequest req=ServletActionContext.getRequest();
for(int i=0;i<strList.size();i++){
//if(strList.get(i))
//Type type=(Type)this.strList.get(i);
}
this.cust.setSex(this.strList.get(0).toString());
this.saveService.saveCustomer(cust);
return SUCCESS;
}
}

删除订单的action类:

package com.crm.action;

import com.crm.bean.Cust;
import com.crm.service.CustService;
import com.opensymphony.xwork2.ActionSupport;

public class RemoveCustAction extends ActionSupport{
private Cust cust;
private CustService removeService;
public Cust getCust() {
return cust;
}
public void setCust(Cust cust) {
this.cust = cust;
}
public CustService getRemoveService() {
return removeService;
}
public void setRemoveService(CustService removeService) {
this.removeService = removeService;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.removeService.removeCustomer(cust);
return SUCCESS;
}

}

查找订单的action类:

package com.crm.action;

import java.util.Map;

import com.crm.bean.Cust;
import com.crm.service.CustService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FindCustByCdtAction extends ActionSupport{
private Cust cust;
private CustService findCdtService;
public Cust getCust() {
return cust;
}
public void setCust(Cust cust) {
this.cust = cust;
}
public CustService getFindCdtService() {
return findCdtService;
}
public void setFindCdtService(CustService findCdtService) {
this.findCdtService = findCdtService;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
Map map=(Map)ActionContext.getContext().get("request");
map.put("list", this.findCdtService.findCustByCondition(cust));
return SUCCESS;
}
}

修改订单的action类:

package com.crm.action;

import java.util.ArrayList;
import java.util.List;

import com.crm.bean.Cust;
import com.crm.service.CustService;
import com.opensymphony.xwork2.ActionSupport;

public class UpdateCustAction extends ActionSupport {
private CustService updateService;
private Cust cust;
List strList = new ArrayList();

public CustService getUpdateService() {
return updateService;
}

public void setUpdateService(CustService updateService) {
this.updateService = updateService;
}

public Cust getCust() {
return cust;
}

public void setCust(Cust cust) {
this.cust = cust;
}

public List getStrList() {
return strList;
}

public void setStrList(List strList) {
this.strList = strList;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.cust.setSex(this.strList.get(0).toString());
this.updateService.updateCustomer(cust);
return SUCCESS;
}
}

生成excel表格的action类:

package com.crm.action;

import java.io.InputStream;

import com.crm.service.CustService;
import com.opensymphony.xwork2.ActionSupport;

public class GenerateExcelAction extends ActionSupport {

private static final long serialVersionUID = 7213178640352795420L;
private CustService excelService;

public CustService getExcelService() {
return excelService;
}

public void setExcelService(CustService excelService) {
this.excelService = excelService;
}

public InputStream getDownloadFile(){
return this.excelService.getInputStream();
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}

}

DAO的实现类:

package com.crm.impl;

import java.util.List;

import com.crm.bean.Cust;
import com.crm.dao.*;
import org.springframework.orm.hibernate3.support.*;

public class CustDaoImpl extends HibernateDaoSupport implements CustDao{

@SuppressWarnings("unchecked")
public List<Cust> findAllCust() {
// TODO Auto-generated method stub
String hql="from Cust cust order by cust.id desc";
return (List<Cust>)this.getHibernateTemplate().find(hql);
}

public Cust findCustomerById(Integer id) {
// TODO Auto-generated method stub
Cust cust=(Cust)this.getHibernateTemplate().get(Cust.class,id);
return cust;
}

public void removeCustomer(Cust cust) {
// TODO Auto-generated method stub
this.getHibernateTemplate().delete(cust);
}

public void saveCustomer(Cust cust) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(cust);
}

public List<Cust> findCustByCondition(Cust cust) {
// TODO Auto-generated method stub
StringBuffer strBuffer=new StringBuffer();
String hql=" from Cust cust where 1=1 ";
strBuffer.append(hql);
if(cust==null){
throw new NullPointerException("查询条件不能为空!");
}
if(!"".equals(cust.getCustname())){
String custname="and custname ='"+cust.getCustname()+"'";
strBuffer.append(custname);
}
if(!"".equals(cust.getCustno())){
String custno="and custno='"+cust.getCustno()+"'";
strBuffer.append(custno);
}
String orderBy=" order by cust.id desc";
strBuffer.append(orderBy);
List<Cust> custList=this.getHibernateTemplate().find(strBuffer.toString());
//return (List<Cust>this.getHibernateTemplate().find(strBuffer.toString());
return custList;
}

public void updateCustomer(Cust cust) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(cust);
}
}

服务的实现类:

package com.crm.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.crm.bean.Cust;
import com.crm.dao.CustDao;

import com.crm.service.CustService;

public class CustServiceImpl implements CustService{

CustDao custDao;

public CustDao getCustDao() {
return custDao;
}

public void setCustDao(CustDao custDao) {
this.custDao = custDao;
}

public List<Cust> findAllCust() {
// TODO Auto-generated method stub
return this.custDao.findAllCust();
}

public Cust findCustomerById(Integer id) {
// TODO Auto-generated method stub
return this.custDao.findCustomerById(id);
}

public void removeCustomer(Cust cust) {
// TODO Auto-generated method stub
this.custDao.removeCustomer(cust);
}

public void saveCustomer(Cust cust) {
// TODO Auto-generated method stub
this.custDao.saveCustomer(cust);
}

public List<Cust> findCustByCondition(Cust cust) {
// TODO Auto-generated method stub
return this.custDao.findCustByCondition(cust);
}

public void updateCustomer(Cust cust) {
// TODO Auto-generated method stub
this.custDao.updateCustomer(cust);
}

public InputStream getInputStream() {
// TODO Auto-generated method stub
//Apache poi hssf对象
HSSFWorkbook wb=new HSSFWorkbook();
//创建sheet
HSSFSheet sheet=wb.createSheet("sheet1");
//表头开始
//创建行
HSSFRow row=sheet.createRow(0);
//创建单元格 第一个单元格从零开始 第一列
HSSFCell cell=row.createCell((short)0);
//设置编码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("客户编号");
//第二列
cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("姓名");
//第三列
cell=row.createCell((short)2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("性别");
//第四列
cell=row.createCell((short)3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("年龄");
//第五列
cell=row.createCell((short)4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("联系方式");
//表头结束
//查询数据库
List<Cust> list=this.custDao.findAllCust();
for(int i=0;i<list.size();++i){
Cust cust=list.get(i);
//把数据放到表格中
row=sheet.createRow(i+1);
cell=row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(cust.getCustno());
cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(cust.getCustname());
cell=row.createCell((short)2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
if("1".equals(cust.getSex())){
cell.setCellValue("男");
}
else if("2".equals(cust.getSex())){
cell.setCellValue("女");
}
else if("3".equals(cust.getSex())){
cell.setCellValue("其它");
}
cell=row.createCell((short)3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(cust.getAge());
cell=row.createCell((short)4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(cust.getTelephone());
}
//根据输出流,输出到文件中
File file=new File("cust.xls");
try{
OutputStream os=new FileOutputStream(file);
//输出写入到文件中
wb.write(os);
//关闭输出流
os.close();
}
catch(Exception e){
e.printStackTrace();
}
InputStream is=null;
try{
is=new FileInputStream(file);
}
catch(FileNotFoundException e){
e.printStackTrace();
}
return is;
}
}

经过这次的实践我们初步体验到了一个小组一起做一个web工程的过程,大家一起的讨论,对功能标准的协商,使我们的编程能力和团队合作能力有很大的提升。

posted @ 2017-07-05 21:47  Gaho  阅读(157)  评论(0编辑  收藏  举报