1. 程式人生 > >小學期實踐心得(2)

小學期實踐心得(2)

計算 servlet cond end vax turn isp xwork res

這次我了解了spring+hibernate 的包和其作用。在使用面向對象技術進行大型復雜系統的設計與開發中,通常需要設計與定義許多類,這些類中有些具有復雜的關系。如何對這些類進行有效的管理,java中引入了包的概念。java中對包的管理類似與操作系統中對文件系統的目錄管理,即java中通過多個層次的包把各類文件組織在一起,包的層次在計算機中保存為目錄。spring和hibernate包時myeclipse中兩個重要的包,接下來我來介紹一下他們的作用和運用。

spring包含有大量的發布包,如何選擇這些發布包,決定選用哪些發布包其實相當簡單。如果你正在構建Web 應用並將全程使用Spring,那麽最好就使用單個全部的spring.jar 文件;如果你的應用僅僅用到簡單的Inversion of Control / Dependency Injection(IoC/DI)容器,那麽只需spring-core.jar與spring-beans.jar 即可;如果你對發布的大小要求很高,那麽就得精挑細選了,只取包含自己所需特性的jar 文件了。采用獨立的發布包你可以避免包含自己的應用不需要的全部類。

當然你可以采用其它的一些工具來設法令整個應用包變小,節省空間的重點在於準確地找出自己所需的Spring 依賴類,然後合並所需的類與包就可以了。Eclispe 有個插件叫ClassPathHelper 可以幫你找找所依賴的類。

而Hibernate一共包括了23個jar包,令人眼花繚亂,不一一介紹了。

完成了這兩個包的加入後,就可以開始寫代碼的具體操作了。

存儲操作:

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.service.CustService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CustSaveAction extends ActionSupport {

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

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

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

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.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;
}

/*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;
}*/
public String execute() throws Exception{
Map map=(Map)ActionContext.getContext().get("request");
map.put("list", this.findCdtService.findCustByCondition(cust));
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;
}

@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
this.service.removeCustomer(cust);
return SUCCESS;
}

}

小學期實踐心得(2)