搭建好SSH环境后,实现对客户信息的操作:
applicationContext.xml
<?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/Cust.hbm.xml</value>
</list>
</property>
</bean>
<!--配置DAO -->
<bean id="custDao" class="com.crm.impl.CustDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!--配置service -->
<bean id="custService" class="com.crm.service.impl.CustServiceImpl">
<property name="custDao" ref="custDao"></property>
</bean>
<!--配置-新增SaveAction -->
<bean id="custSaveAction" class="com.crm.action.CustSaveAction">
<property name="service" ref ="custService"></property>
</bean>
<!-- 配置-查询ListCustAction -->
<bean id="listCustAction" class="com.crm.action.ListCustAction">
<property name="custService" ref="custService"></property>
</bean>
<!--配置-删除deleteAction -->
<bean id="removeCustAction" class="com.crm.action.RemoveCustAction">
<property name="service" ref="custService"></property>
</bean>
<!--配置-条件查询findCdtAction -->
<bean id="findCdtAction" class="com.crm.action.FindCustByCdtAction">
<property name="findCdtService" ref="custService"></property>
</bean>
<!--配置-typeAction -->
<bean id="typeAction" class="com.crm.action.TypeAction">
</bean>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 保存 -->
<package name="customer" extends="struts-default">
<action name="saveCust" class="custSaveAction">
<result name="input">/custAdd.jsp</result>
</action>
<!-- 查询 -->
<action name="listCust" class="listCustAction">
<result>/jsp/custInfo.jsp</result>
</action>
<!-- 删除 -->
<action name="delectCust" class="removeCustAction">
<result>/jsp/custInfo.jsp</result>
</action>
<!-- typeAction下拉列表 -->
<action name="typeAction" class="typeAction">
<result></result>
</action>
<!-- 条件查询 -->
<action name="findCdtCustList" class="findCdtAction">
<result>/jsp/custInfo.jsp</result>
</action>
</package>
</struts>
信息存储类CustSaveAction.java
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;
}
}
信息删除类RemoveCustAction.java
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 static final long serialVersionUID = 1L;
private CustService custService;
public CustService getCustService() {
return custService;
}
public void setCustService(CustService custService) {
this.custService = custService;
}
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
Map map = (Map)ActionContext.getContext().get("request");
map.put("list", this.custService.findAllCust());
return SUCCESS;
}
}
按条件查询FindCustByCdtAction.java
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;
}
}