首页 > 代码库 > 第二次小学期软工实践随笔
第二次小学期软工实践随笔
小学期后面两天的学习,我们进一步完善了我们的ssh框架,在Dao和Service包下我们声明好我们需要的操作方法:
package com.crm.dao;
import java.util.List;
import com.crm.bean.Cust;
public interface CustDao {
/**
* 保存客户信息
* @param cust
*/
public void saveCustomer(Cust cust);
/**
* 删除客户信息
* @param cust
*/
public void removeCustomer(Cust cust);
public Cust findCustomerById(Integer id);
public List<Cust> findAllCust();
public List<Cust> findCustByCondition(Cust cust);
}
然后分别在他们的实现类中补充好具体的实现方法和参数的导入步骤,在CustDaoImpl中的实现主要是调用Hibernate的方法,所以调用起来简单方便,复杂一点的是我们的条件查询操作,需要捕捉信息源在打出查询列表:
package com.crm.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.crm.bean.Cust;
import com.crm.dao.CustDao;
public class CustDaoImpl extends HibernateDaoSupport implements CustDao{
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 custname = " and custno = ‘"+cust.getCustno()+"‘";
strBuffer.append(custname);
}
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;
}
}
因此我们在com.crm.action包名下添加了如下的action类:
具体代码如下:
package com.crm.action;
import com.crm.bean.Cust;
import com.crm.service.CustService;
import com.opensymphony.xwork2.ActionSupport;
public class CustSaveAction extends ActionSupport{
private CustService service;
private Cust cust;
public Cust getCust() {
return cust;
}
public void setCust(Cust cust) {
this.cust = cust;
}
public CustService getService() {
return service;
}
public void setService(CustService service) {
this.service = service;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.service.saveCustomer(cust);
return SUCCESS;
}
}
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;
}
@SuppressWarnings({ "unchecked", "unchecked" })
@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;
}
}
package com.crm.action;
import java.util.Map;
import com.crm.service.CustService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ListCustAction extends ActionSupport{
private CustService service;
public void setService(CustService service) {
this.service = service;
}
@Override
public String execute() throws Exception {
Map map = (Map)ActionContext.getContext().get("request");
map.put("list", this.service.findAllCust());
return SUCCESS;
}
}
package com.crm.action;
import com.crm.bean.Cust;
import com.crm.service.CustService;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class RemoveCustAction extends ActionSupport{
private Cust cust;
private CustService service;
public void setService(CustService service) {
this.service = service;
}
public Cust getCust() {
return cust;
}
public void setCust(Cust cust) {
this.cust = cust;
}
public String execute() throws Exception {
this.service.removeCustomer(cust);
return SUCCESS;
}
}
根据SSH框架的规则,我们在ApplicationContext中完成数据库连接,action配置等任务,这里的用到了许多我不了解的语法规则,我觉得后期可以认真学习一下,像是Bean,property的一类操作。
中间层用Struts.xml实现与前台的过渡,最后新建jsp文件进行前台界面的设计:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>客户信息维护</title>
<style>
.divcss5{width:600px;height:120px;border:1px solid #000}
body,table{
font-size:12px;
}
table{
table-layout:fixed;
empty-cells:show;
border-collapse: collapse;
margin:0 auto;
}
td{
height:30px;
}
h1,h2,h3{
font-size:12px;
margin:0;
padding:0;
}
.table{
border:1px solid #000;
color:#000;
}
.table th {
background-repeat:repeat-x;
height:30px;
}
.table td,.table th{
border:1px solid #000;
padding:0 1em 0;
}
.table tr.alter{
background-color:#000;
}
</style>
</head>
<script language="JavaScript" type="text/javascript">
function openwind(){
feature="dialogWidth:650px;dialogHeight:200px;status:no;help:no;scrollbars:no;dialogTop:150;";
window.showModalDialog("custSave.jsp",null,feature);
};
function funDelete(){
//window.onload = alert(111);
if(confirm(‘确定要执行此操作吗?‘)){
return true;
}else{
return false;
}
}
</script>
<body>
<CENTER>
<center><div><font color="red" size="6">客户信息维护</font></div></center>
<div style="width:20px"></div>
<div class="divcss5">
<s:form action="findbycdtCust" theme="simple">
<p></p>
<div style="width:10px"></div>
客户编号:<s:textfield name="cust.custno" label="custno"></s:textfield>
客户名称:<s:textfield name="cust.custname" label="custname"></s:textfield><br><br>
公司地址:<s:textfield name="cust.address" label="address"></s:textfield>
电话号码:<s:textfield name="cust.telephone" label="telephone"></s:textfield><br><br>
<div style="width:20px"></div>
<input width="100" type = "button" id = "add" name = "btn" value="http://www.mamicode.com/新增" onClick="openwind();"/>
<s:submit value="http://www.mamicode.com/查询"></s:submit>
<input width="100" type = "button" id = "smt" name = "btn" value="http://www.mamicode.com/返回" onClick="history.go(-1)"/>
</s:form>
</div>
<div style="width:20px"></div>
<table border="1" width="47%" class="table">
<tr>
<td>客户编号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>联系方式</td>
<td>公司职务</td>
<td>录入时间</td>
<td width="80">操作</td>
</tr>
<s:iterator value="http://www.mamicode.com/#request.list" id="cust">
<tr>
<td><s:property value="http://www.mamicode.com/#cust.custno"/></td>
<td><s:property value="http://www.mamicode.com/#cust.custname"/></td>
<td>
<s:if test="#cust.sex == 1">
<s:property value="http://www.mamicode.com/‘男‘"/>
</s:if>
<s:elseif test="#cust.sex == 2">
<s:property value="http://www.mamicode.com/‘女‘"/>
</s:elseif>
<s:elseif test="#cust.sex == 3">
<s:property value="http://www.mamicode.com/‘其他‘"/>
</s:elseif>
</td>
<td><s:property value="http://www.mamicode.com/#cust.age"/></td>
<td><s:property value="http://www.mamicode.com/#cust.telephone"/></td>
<td><s:property value="http://www.mamicode.com/#cust.position"/></td>
<td><s:property value="http://www.mamicode.com/#cust.logindate"/></td>
<td>
<s:a href="http://www.mamicode.com/updateCust.action?cust.id=%{#cust.id}">修改</s:a>
<s:a href="http://www.mamicode.com/deleteCust.action?cust.id=%{#cust.id}" onClick="return funDelete();">删除</s:a>
</td>
</tr>
</s:iterator>
</table>
</CENTER>
</body>
</html>
最终结果如下(后期我们还要添加修改记录的功能):
第二次小学期软工实践随笔