这次我了解了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; }}