短学期实践随笔(二)

一直在跟着老师敲代码,几天下来做出了客户信息主界面以及客户信息存储、查询、修改、删除功能。

开始时applicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--数据库-配置数据连接池 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/dbssh">
</property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="maxActive" value="100"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<!--sessionFactory配置与管理 -->
<bean id = "sessionFactory"
class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/crm/bean/Customer.hbm.xml</value>
</list>
</property>
</bean>

<!-- 配置Dao -->
<bean id = "CustomerDao" class = "com.crm.impl.CustomerDaoImpl">
<property name = "sessionFactory">
<ref bean = "sessionFactory"/>
</property>
</bean>

<!--配置service -->
<bean id="CustomerService" class="com.crm.service.impl.CustomerServiceImpl">
<property name="customerdao" ref="CustomerDao"></property>
</bean>

<!--配置-新增saveAction -->
<bean id="customerSaveAction" class="com.crm.action.CustomerSaveAction">
<property name="service" ref="CustomerService"></property>
</bean>

<!--配置-查询listAction -->
<bean id="customerListAction" class="com.crm.action.CustomerListAction">
<property name="service" ref="CustomerService"></property>
</bean>

<!--配置-条件查询findCdAction -->
<bean id="findCustomerByCdAction" class="com.crm.action.CustomerFindByCdAction">
<property name="findCdService" ref="CustomerService"></property>
</bean>

<!--配置-删除deleteAction -->
<bean id="customerRemoveAction" class="com.crm.action.CustomerRemoveAction">
<property name="removeService" ref="CustomerService"></property>
</bean>

<!--配置-修改预览updatePreviewAction -->
<bean id="customerUpdatePreviewAction" class="com.crm.action.CustomerUpdatePreviewAction">
<property name="updatePvService" ref="CustomerService"></property>
</bean>

<!--配置-修改updateAction -->
<bean id="customerUpdateAction" class="com.crm.action.CustomerUpdateAction">
<property name="updateService" ref="CustomerService"></property>
</bean>

</beans>

然后是action中的各个操作:

CustomerFindByCdAction:

package com.crm.action;

import java.util.Map;

import com.crm.bean.Customer;
import com.crm.service.CustomerService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerFindByCdAction extends ActionSupport{
private Customer customer;
private CustomerService findCdService;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public CustomerService getFindCdService() {
return findCdService;
}
public void setFindCdService(CustomerService findCdService) {
this.findCdService = findCdService;
}

public String execute() throws Exception {
// TODO Auto-generated method stub
Map map = (Map)ActionContext.getContext().get("request");
map.put("list", this.findCdService.findCustomerByCondition(customer));
return SUCCESS;
}

}

 

CustomerListAction.java:

package com.crm.action;

import java.util.Map;

import com.crm.service.CustomerService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerListAction extends ActionSupport{
private CustomerService service;
public void setService(CustomerService service){
this.service = service;
}

@Override
public String execute() throws Exception{
Map map = (Map)ActionContext.getContext().get("request");
map.put("list",this.service.findAllCustomer());
return SUCCESS;
}
}

 

 

CustomerRemoveAction:

package com.crm.action;

import com.crm.bean.Customer;
import com.crm.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerRemoveAction extends ActionSupport {
private Customer customer;
private CustomerService removeService;

public void setService(CustomerService removeService) {
this.removeService = removeService;
}

public CustomerService getRemoveService() {
return getRemoveService();
}

public void setRemoveService(CustomerService removeService) {
this.removeService = removeService;
}

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.removeService.deleteCustomer(customer);
return SUCCESS;
}

}

 

 

CustomerSaveAction.java:

package com.crm.action;

import com.crm.bean.Customer;
import com.crm.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerSaveAction extends ActionSupport {

private Customer customer;
private CustomerService service;

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

public void setService(CustomerService service) {
this.service = service;
}


public CustomerService getService() {
return service;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.service.saveCustomer(customer);
return SUCCESS;
}
}

 

CustomerUpdateAction:

package com.crm.action;

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


import com.crm.bean.Customer;
import com.crm.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerUpdateAction extends ActionSupport{
private CustomerService updateService;
private Customer customer;


public CustomerService getUpdateService() {
return updateService;
}

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

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}



@Override
public String execute() throws Exception {
// TODO Auto-generated method stub

this.updateService.updateCustomer(customer);
return SUCCESS;
}
}

 

CustomerUpdatePreviewAction:

package com.crm.action;

import com.crm.bean.Customer;
import com.crm.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerUpdatePreviewAction extends ActionSupport{
private CustomerService updatePvService;
private Customer customer;

public CustomerService getUpdatePvService() {
return updatePvService;
}

public void setUpdatePvService(CustomerService updatePvService) {
this.updatePvService = updatePvService;
}

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
customer = this.updatePvService.findCustomerById(customer.getID());
return SUCCESS;
}
}

 

还有其他包的各项操作:

 

Customer.java:

package com.crm.bean;

public class Customer {
private int ID;
private String cusno;
private String cusname;
private String custele;
private String cussex;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getCusno() {
return cusno;
}
public void setCusno(String cusno) {
this.cusno = cusno;
}
public String getCusname() {
return cusname;
}
public void setCusname(String cusname) {
this.cusname = cusname;
}
public String getCustele() {
return custele;
}
public void setCustele(String custele) {
this.custele = custele;
}
public String getCussex() {
return cussex;
}
public void setCussex(String cussex) {
this.cussex = cussex;
}

}


Customer.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.crm.bean.Customer" table="customer">
<id name="ID" type="java.lang.Integer" column="ID">
<generator class="increment"></generator>
</id>
<property name="cusno" type="string" column="cusno" length="20"/>
<property name="cusname" type="string" column="cusname" length="100"/>
<property name="cussex" type="string" column="cussex" length="6"/>
<property name="custele" type="string" column="custele" length="11"/>
<!--<property name="age" type="int" column="age"/>
<property name="telephone" type="string" column="telephone" length="20"/>
<property name="position" type="string" column="position" length="80"/>
<property name="logindate" type="string" column="logindate" length="10"/>-->
</class>
</hibernate-mapping>


CustomerDao.java:

package com.crm.dao;
import java.util.List;

import com.crm.bean.Customer;

public interface CustomerDao {

/**
* 保存客户信息
* @param customer
*/
public void saveCustomer(Customer customer);
/**
* 删除客户信息
* @param customer
*/
public void deleteCustomer(Customer customer);
/**
* 查询客户信息
* @param ID
* @return
*/
public Customer findCustomerById(Integer ID);
/**
* 查询所有客户信息
* @return
*/
public List<Customer> findAllCustomer();
/**
* 条件查询
* @param customer
* @return
*/
public List<Customer> findCustomerByCondition(Customer customer);
/**
* 修改客户信息
* @param cust
*/
public void updateCustomer(Customer customer);

}

 

 

CustomerDao的实现类CustomerDaoImpl.java:

package com.crm.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.crm.bean.Customer;
import com.crm.dao.CustomerDao;

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao{


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

public Customer findCustomerById(Integer ID) {
// TODO Auto-generated method stub
Customer customer = (Customer)this.getHibernateTemplate().get(Customer.class,ID);
return customer;
}

public void deleteCustomer(Customer customer) {
// TODO Auto-generated method stub
this.getHibernateTemplate().delete(customer);
}

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

public List<Customer> findCustomerByCondition(Customer customer) {
// TODO Auto-generated method stub
StringBuffer strBuffer = new StringBuffer();
String hql = "from Customer customer where 1=1 ";
strBuffer.append(hql);
if(customer == null){
throw new NullPointerException("查询条件不能为空!");
}
if(!"".equals(customer.getCusname())){
String cusname = " and cusname = '"+customer.getCusname()+"'";
strBuffer.append(cusname);
}
if(!"".equals(customer.getCusno())){
String cusname = " and cusno = '"+customer.getCusno()+"'";
strBuffer.append(cusname);
}
String orderBy = " order by customer.ID desc";
strBuffer.append(orderBy);
List<Customer> customerList = this.getHibernateTemplate().find(strBuffer.toString());
//return (List<Customer>)this.getHibernateTemplate().find(strBuffer.toString());
return customerList;
}

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

}


CustomerService.java:

package com.crm.service;

import java.util.List;

import com.crm.bean.Customer;
import com.crm.dao.CustomerDao;

public interface CustomerService {


/**
* 保存客户信息
* @param cust
*/
public void saveCustomer(Customer customer);


/**
* 删除客户信息
* @param cust
*/
public void deleteCustomer(Customer customer);


/**
* 查询客户信息
* @param ID
* @return
*/
public Customer findCustomerById(Integer ID);


/**
* 查询所有客户信息
* @return
*/
public List<Customer> findAllCustomer();


/**
* 通过条件查询客户信息
* @param customer
* @return
*/
public List<Customer> findCustomerByCondition(Customer customer);


/**
* 修改客户信息
* @param customer
*/
public void updateCustomer(Customer customer);
}

 

CustomerService的实现类CustomerServiceImpl.java:

package com.crm.service.impl;

import java.util.List;

import com.crm.bean.Customer;
import com.crm.dao.CustomerDao;
import com.crm.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {

CustomerDao customerdao;

public CustomerDao getCustomerDao() {
return customerdao;
}

public void setCustomerdao(CustomerDao customerdao) {
this.customerdao = customerdao;
}

public List<Customer> findAllCustomer() {
// TODO Auto-generated method stub
return this.customerdao.findAllCustomer();
}

public Customer findCustomerById(Integer ID) {
// TODO Auto-generated method stub
return this.customerdao.findCustomerById(ID);
}

public void deleteCustomer(Customer customer) {
// TODO Auto-generated method stub
this.customerdao.deleteCustomer(customer);
}

public void saveCustomer(Customer customer) {
// TODO Auto-generated method stub
this.customerdao.saveCustomer(customer);
}

public List<Customer> findCustomerByCondition(Customer customer) {
// TODO Auto-generated method stub
return this.customerdao.findCustomerByCondition(customer);
}


public void updateCustomer(Customer customer) {
// TODO Auto-generated method stub
this.customerdao.updateCustomer(customer);
}

}

还有jsp文件:

 

customerInfo.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("customerSave.jsp",null,feature);
};

function funDelete(){
//window.onload = alert(111);
if(confirm('确定要执行此操作吗?')){
return true;
}else{
return false;
}
};

function updatewindow(){
feature="dialogWidth:650px;dialogHeight:200px;status:no;help:no;scrollbars:no;dialogTop:150;";
window.showModalDialog("customerUpdate.jsp",null,feature);
};


</script>
<body>
<CENTER>
<center><div><font color="red" size="6">客户信息维护</font></div></center>
<div style="width:20px"></div>
<div class="divcss5">
<s:form action="listCustomer" theme="simple">
<div style="width:10px"></div>
客户编号:<s:textfield name="customer.cusno" label="cusno"></s:textfield>
客户名称:<s:textfield name="customer.cusname" label="cusname"></s:textfield><br>
公司地址:<s:textfield name="customer.cusaddress" label="cusaddress"></s:textfield>
电话号码:<s:textfield name="customer.custele" label="custele"></s:textfield><br>
<div style="width:20px"></div>
<input width="100" type = "button" id = "add" name = "btn" value="新增" onClick="openwind();"/>
<s:submit action="findCustomerByCd" value="查询"> </s:submit>
<input width="100" type = "button" id = "smt" name = "btn" value="返回" 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="#request.list" id="customer">
<tr>
<td><s:property value="#customer.cusno"/></td>
<td><s:property value="#customer.cusname"/></td>
<td>
<s:if test="#customer.cussex == 1">
<s:property value="'男'"/>
</s:if>
<s:elseif test="#customer.cussex == 2">
<s:property value="'女'"/>
</s:elseif>
<s:elseif test="#customer.cussex == 3">
<s:property value="'其他'"/>
</s:elseif>
</td>
<td><s:property value="#customer.cusage"/></td>
<td><s:property value="#customer.custele"/></td>
<td><s:property value="#customer.position"/></td>
<td><s:property value="#customer.logindate"/></td>
<td>
<s:a href="updatePvCustomer.action?customer.ID=%{#customer.ID}">修改</s:a>
<s:a href="deleteCustomer.action?customer.ID=%{#customer.ID}" onClick="return funDelete();">删除</s:a>
</td>
</tr>

</s:iterator>
</table>
</CENTER>
</body>
</html>

 

 customerSave.jsp:

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!-- 下拉菜单 -->
<s:action name="typeAction" id="list"></s:action>
<!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:100px;border:1px solid #000}
</style>
</head>
<body>
<center>
<s:form action="saveCustomer">
<s:textfield name="customer.cusno" label="客户编号"></s:textfield>
<s:textfield name="customer.cusname" label="客户名称"></s:textfield>
<s:textfield name="customer.custele" label="客户号码"></s:textfield>
<s:submit value="保存"></s:submit>
</s:form>
</center>
</body>
</html>

 

customerUpdate.jsp:

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>修改客户信息</title>
<style>
.divcss5{width:600px;height:100px;border:1px solid #000}
</style>
</head>
<script type="text/javascript">
function updatewindow(){
alert(("保存成功");
window.showModalDialog("customerUpdate.jsp",null,feature);
};
</script>
<body>
<CENTER>
<div><font size="5" color="red">修改客户信息</font></div>
<center></center>
<div style="width:20px"></div>
<div class="divcss5">
<s:form action="updateCustomer" theme="simple">
<div style="width:10px"></div>
客户编号:<s:textfield name="customer.cusno" value="%{customer.cusno}" label="cusno"></s:textfield>

客户名称:<s:textfield name="customer.cusname" value="%{customer.cusname}" label="cusname"></s:textfield>
年龄:<s:textfield name="customer.custele" label="%{customer.tele}"></s:textfield><br>
<s:hidden name="customer.ID" value="%{customer.ID}" ></s:hidden>
<tr><td>&nbsp;</td></tr>
<s:submit value="保存" onClick="window.history.back(-1);"></s:submit>
<input width="100" type = "button" id = "smt" name = "btn" value="关闭" onClick="history.go(-1);"/>
</s:form>
</div>
<div style="width:20px"></div>
</CENTER>
</body>
</html>

效果图如下:

 

posted @ 2017-07-02 16:01  时过境迁zZ  阅读(383)  评论(1编辑  收藏  举报