SSH-CRUD(增删改查)简单示例原文出自::增加(Create)、查询(Retrieve)(重新得到数据)、更新(Update)和删除(Delete);转载自:。我优化了一下排版,增加了项目例子的下载。本文讲的是一个S2SH框架的的人力资源管理小系统,包含最基本的增删改查(CRUD)功能,非常适合初学者学习。项目的下载地址为:整个工程建完后的目录截图:一、准备工作开发环境:tomcat5+jdk6+mysql5+eclipse:eclipse-jee-helios-SR1-win32.zip.struts2+Spring2.0.6+Hibernate3.1。本示例通过对一个人力资源的系统,提供基本的增加、删除、修改、查询等功能。lib包需要以下所示的这些包:antlr-2.7.6.jarcommons-collections-3.2.jarcommons-dbcp.jarcommons-digester-2.0.jarcommons-fileupload-1.2.1.jarcommons-io-1.3.2.jarcommons-logging-1.0.4.jarcommons-pool.jardom4j-1.6.1.jarfreemarker-2.3.16.jarhibernate3.jarjavassist-3.9.0.GA.jarjta-1.1.jarlog4j-1.2.15.jarmysql-connector-java-5.1.6-bin.jar(MySQLjar包)ognl-3.0.jarslf4j-api-1.5.8.jarslf4j-log4j12-1.5.0.jarspring.jarstruts2-core-2.2.1.jarstruts2-spring-plugin-2.2.1.jarxwork-core-2.2.1.jar二、建立数据库createdatabasestud;usestud;createtableEmployee(idint(11)notnullauto_increment,namevarchar(255),addressvarchar(255),phonevarchar(255),primarykey(id));三、建立数据化持久层建立Hibernate逆向工程1、建立实体类及books.hbm.xml映射文件。Employee.javapackagecom.ex.model;publicclassEmployee{privateLongid;privateStringname;privateStringaddress;privateStringphone;publicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetAddress(){returnaddress;}publicvoidsetAddress(Stringaddress){this.address=address;}publicStringgetPhone(){returnphone;}publicvoidsetPhone(Stringphone){this.phone=phone;}publicEmployee(){}publicEmployee(Longid,Stringname,Stringaddress,Stringphone){super();this.id=id;this.name=name;this.address=address;this.phone=phone;}}2、把实体类Employee的属性映射到Employee表,生成Employee.hbm.xml文件把实体类Employee的属性映射到Employee表,生成下面的Employee.hbm.xml文件(com/ex/model/Employee.hbm.xml):?xmlversion=1.0encoding=UTF-8?!DOCTYPEhibernate-mappingPUBLIC-//Hibernate/HibernateMappingDTD3.0//EN=com.ex.model.Employeetable=Employeelazy=trueidname=idtype=java.lang.Longcolumn=idgeneratorclass=native//idpropertyname=nametype=java.lang.Stringcolumnname=name//propertypropertyname=addresstype=java.lang.Stringcolumnname=address//propertypropertyname=phonetype=java.lang.Stringcolumnname=phone//property/class/hibernate-mapping四、建立DAO层1、建立DAO的接口类:EmployeeDaopackagecom.ex.dao;importjava.util.List;importcom.ex.model.Employee;publicinterfaceEmployeeDao{publicvoidadd(Employeeemployee);//添加新雇员publicEmployeegetEmployee(Longid);//根据雇员ID得到雇员信息publicListEmployeegetEmployees();//获取所有雇员信息publicvoiddelete(Longid);//根据雇员ID,删除雇员publicvoidsaveOrUpdate(Employeeemployee);//修改或更新雇员信息publicListEmployeegetEmployee(Stringname,Stringphone);//判断是否合法雇员}2、实现此接口的类文件,EmployeeDaoImplpackagecom.ex.dao.impl;importjava.util.List;importcom.ex.dao.EmployeeDao;importcom.ex.model.Employee;importorg.hibernate.SessionFactory;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;publicclassEmployeeDaoImplextendsHibernateDaoSupportimplementsEmployeeDao{privateSessionFactorysessionFactory;publicvoidsetFactory(SessionFactorysessionFactory){this.sessionFactory=sessionFactory;}publicSessionFactorygetFactory(){returnthis.sessionFactory;}publicvoidadd(Employeeemployee){this.getHibernateTemplate().save(employee);}publicEmployeegetEmployee(Longid){return(Employee)this.getHibernateTemplate().get(Employee.class,id);}@SuppressWarnings(unchecked)publicListEmployeegetEmployees(){returnthis.getHibernateTemplate().find(fromEmployeeorderbyid);}publicvoiddelete(Longid){this.getHibernateTemplate().delete(this.getEmployee(id));}publicvoidsaveOrUpdate(Employeeemployee){this.getHibernateTemplate().saveOrUpdate(employee);}@SuppressWarnings(unchecked)publicListEmployeegetEmployee(Stringname,Stringphone){Objectvalues[]={name,phone};System.out.println(查询的结果是:+name++phone);returnthis.getHibernateTemplate().find(fromEmployeeewheree.name=?ande.phone=?orderbyid,values);}}五、建立业务逻辑层注明:在业务逻辑层需要认真思考每个业务逻辑所能用到的持久层对象和DAO。DAO层之上是业务逻辑层,DAO类可以有很多个,但业务逻辑类应该只有一个,可以在业务逻辑类中调用各个DAO类进行操作。1、创建服务接口类EmployeeManagerpackagecom.ex.service;importjava.util.List;importcom.ex.model.Employee;publicinterfaceEmployeeManager{publicvoidaddEmployee(Employeeemployee);publicListEmployeelistEmployee();publicvoiddeleteEmployee(Longid);publicvoidsaveOrUpdate(Employeeemployee);publicEmployeegetEmployee(Longid);publicEmployeegetEmployee(Stringname,Stringphone);}2、实现此接口类:EmployeeManagerImpl:packagecom.ex.service.impl;importjava.util.List;importcom.ex.dao.EmployeeDao;importcom.ex.model.Employee;importcom.ex.service.EmployeeManager;publicclassEmployeeManagerImplimplementsEmployeeManager{privateEmployeeDaoemployeeDao;publicvoidsetEmployeeDao(EmployeeDaoemployeeDao){this.employeeDao=employeeDao;}publicEmployeeDaogetEmployeeDao(){returnthis.employeeDao;}publicvoidaddEmployee(Employeeemployee){this.employeeDao.add(employee);}public