日期:2014-05-17 浏览次数:20756 次
S2SH -CRUD简单示例 一、 准备工作 开发环境:tomcat5+jdk6+mysql5+eclipse:eclipse-jee-helios-SR1-win32.zip. struts2+Spring2.0.6+Hibernate3.1。本示例通过对一个人力资源的系统,提供基本的增加、删除、修改、查询等功能。 lib包需要以下所示的这些包: antlr-2.7.6.jar commons-collections-3.2.jar commons-dbcp.jar commons-digester-2.0.jar commons-fileupload-1.2.1.jar commons-io-1.3.2.jar commons-logging-1.0.4.jar commons-pool.jar dom4j-1.6.1.jar freemarker-2.3.16.jar hibernate3.jar javassist-3.9.0.GA.jar jta-1.1.jar log4j-1.2.15.jar mysql-connector-java-5.1.6-bin.jar(MySQL jar包) ognl-3.0.jar slf4j-api-1.5.8.jar slf4j-log4j12-1.5.0.jar spring.jar struts2-core-2.2.1.jar struts2-spring-plugin-2.2.1.jar xwork-core-2.2.1.jar 二、建立数据库 create database stud ; use stud ; create table Employee(id int(11) not null auto_increment,name varchar(255) ,address varchar(255) ,phone varchar(255) ,primary key (id)) ; 三、建立数据化持久层 建立Hibernate逆向工程 1、建立实体类及books.hbm.xml映射文件。 Employee.java package com.ex.model; public class Employee { private Long id ; private String name ; private String address ; private String phone ; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Employee(){ } public Employee(Long id, String name, String address, String phone) { super(); this.id = id; this.name = name; this.address = address; this.phone = phone; } } 2、把实体类Employee的属性映射到Employee表,生成下面的Employee.hbm.xml文件(com/ex/model/Employee.hbm.xml): <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.ex.model.Employee" table="Employee" lazy="true"> <id name="id" type="java.lang.Long" column="id"> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="name" /> </property> <property name="address" type="java.lang.String"> <column name="address" /> </property> <property name="phone" type="java.lang.String"> <column name="phone" /> </property> </class> </hibernate-mapping> 四、建立DAO层 1、建立DAO的接口类:EmployeeDao EmployeeDao.java package com.ex.dao; import java.util.List; import com.ex.model.Employee; public interface EmployeeDao { public void add(Employee employee) ;//添加新雇员 public Employee getEmployee(Long id) ;//根据雇员ID得到雇员信息 public List< Employee> getEmployees() ;//获取所有雇员信息 public void delete(Long id ) ;//根据雇员ID,删除雇员 public void saveOrUpdate(Employee employee) ;//修改或更新雇员信息 public List< Employee> getEmployee(String name, String phone) ;//判断是否合法雇员 } 2、实现此接口的类文件,EmployeeDaoImpl EmployeeDaoImpl.java package com.ex.dao.impl; import java.util.List; import com.ex.dao.EmployeeDao; import com.ex.model.Employee; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.support.HibernateDaoS