这几天,我们做出了客户信息主界面以及客户信息存储、查询、修改、删除功能。
首先,建立一个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>
<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/Cust.hbm.xml</value>
</list>
</property>
</bean>
<bean id="custDao" class="com.crm.impl.CustDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="custService" class="com.crm.service.impl.CustServiceImpl">
<property name="custDao" ref="custDao"></property>
</bean>
<!-- 配置action -->
<bean id="custSaveAction" class="com.crm.action.CustSaveAction">
<property name="service">
<ref bean="custService"/>
</property>
</bean>
<bean id="listCustAction" class="com.crm.action.ListCustAction">
<property name="service">
<ref bean="custService"/>
</property>
</bean>
<bean id="findCustByCdAction" class="com.crm.action.FindCustByCdAction">
<property name="findCdService">
<ref bean="custService"/>
</property>
</bean>
<!--配置-删除deleteAction -->
<bean id="removeCustAction" class="com.crm.action.RemoveCustAction">
<property name="service">
<ref bean="custService"/>
</property>
</bean>
</beans>
其次,在src目录下建立action、bean、dao、impl、service等类,其作用是对客户信息的录入、查询、修改、删除等操作,为此,我们要先建立一个与之对应的数据库。
接下来,在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 FindCustByCdAction extends ActionSupport{
private Cust cust;
private CustService findCdService;
public Cust getCust() {
return cust;
}
public void setCust(Cust cust) {
this.cust = cust;
}
public CustService getFindCdService() {
return findCdService;
}
public void setFindCdService(CustService findCdService) {
this.findCdService = findCdService;
}
@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.findCdService.findCustByCondition(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 ListCustAction extends ActionSupport {
private Cust cust;
private CustService service;
public void setService(CustService service)
{
this.service=service;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
Map map=(Map)ActionContext.getContext().get("request");
map.put("list", this.service.findAllCust());
return SUCCESS;
}
public Cust getCust() {
return cust;
}
public void setCust(Cust cust) {
this.cust = cust;
}
}
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 service;
public void setService(CustService service) {
this.service = service;
}
public Cust getCust() {
return cust;
}
public void setCust(Cust cust) {
this.cust = cust;
}
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
this.service.removeCustomer(cust);
return SUCCESS;
}
}
接下来,要与之建立相应的映射关系:
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 null;
}
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.getCustnumber())){
String custnumber="and custnumber = ‘"+cust.getCustnumber()+"‘ ";
strBuffer.append(custnumber);
}
if(!"".equals(cust.getCustphone())){
String custphone="and custphone = ‘"+cust.getCustphone()+"‘ ";
strBuffer.append(custphone);
}
String orderBy="order by cust.id desc";
strBuffer.append(orderBy);
List<Cust> custList=this.getHibernateTemplate().find(strBuffer.toString());
return custList;
}
}
成品如图所示: