如题。。我是新手,请高手指点。。。
------解决方案--------------------
- Java code
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.javapackage 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的接口类:EmployeeDaoEmployeeDao.javapackage 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、实现此接口的类文件,EmployeeDaoImplEmployeeDaoImpl.javapackage 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.HibernateDaoSupport; public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao { private SessionFactory sessionFactory ; public void setFactory(SessionFactory sessionFactory){ this.sessionFactory = sessionFactory ; } public SessionFactory getFactory(){ return this.sessionFactory ; } @Override public void add(Employee employee) { this.getHibernateTemplate().save( employee) ; } @Override public Employee getEmployee(Long id) { return (Employee) this.getHibernateTemplate().get(Employee.class, id) ; } @SuppressWarnings("unchecked") @Override public List<Employee> getEmployees() { return this.getHibernateTemplate().find("from Employee order by id" ) ; } @Override public void delete(Long id) { this.getHibernateTemplate().delete(this.getEmployee(id)) ; } @Override public void saveOrUpdate(Employee employee) { this.getHibernateTemplate().saveOrUpdate(employee) ; } @SuppressWarnings("unchecked") @Override public List<Employee> getEmployee(String name, String phone) { Object values[] ={name, phone} ; System.out.println("查询的结果是:"+ name + " "+ phone) ; return this.getHibernateTemplate().find("from Employee e where e.name=? and e.phone=? order by id", values) ; }}五、 建立业务逻辑层注明:在业务逻辑层需要认真思考每个业务逻辑所能用到的持久层对象和DAO。DAO层之上是业务逻辑层,DAO类可以有很多个,但业务逻辑类应该只有一个,可以在业务逻辑类中调用各个DAO类进行操作。1、创建服务接口类EmployeeManagerEmployeeManager.javapackage com.ex.service;import java.util.List;import com.ex.model.Employee;public interface EmployeeManager { public void addEmployee(Employee employee) ; public List< Employee> listEmployee() ; public void deleteEmployee(Long id) ; public void saveOrUpdate(Employee employee) ; public Employee getEmployee(Long id) ; public Employee getEmployee(String name, String phone) ; } 2、实现此接口类:EmployeeManagerImpl:EmployeeManagerImpl.javapackage com.ex.service.impl;import java.util.List;import com.ex.dao.EmployeeDao;import com.ex.model.Employee;import com.ex.service.EmployeeManager;public class EmployeeManagerImpl implements EmployeeManager { private EmployeeDao employeeDao ; public void setEmployeeDao(EmployeeDao employeeDao){ this.employeeDao = employeeDao ; } public EmployeeDao getEmployeeDao(){ return this.employeeDao ; } @Override public void addEmployee(Employee employee) { this.employeeDao.add(employee) ; } @Override public List<Employee> listEmployee() { return this.employeeDao.getEmployees() ; } @Override public void deleteEmployee(Long id) { this.employeeDao.delete(id) ; } @Override public void saveOrUpdate(Employee employee) { this.employeeDao.saveOrUpdate(employee) ; } @Override public Employee getEmployee(Long id) { return this.employeeDao.getEmployee(id) ; } @Override public Employee getEmployee(String name, String phone) { List< Employee> list = this.employeeDao.getEmployee(name, phone) ; if (list != null && list.size() >0){ return list.get( 0 ) ; }else{ return null ; } }}六、 创建Action类:EmployeeAction.java:EmployeeAction.javapackage com.ex.action;import java.util.List;import com.ex.model.Employee;import com.ex.service.EmployeeManager;import com.opensymphony.xwork2.ActionSupport;public class EmployeeAction extends ActionSupport { private static final long serialVersionUID = 1L; private EmployeeManager employeeManager; private List<Employee> employees; private Employee employee; private Long id; private Long[] ids; private String name; private String phone; public EmployeeManager getEmployeeManager() { return employeeManager; } public void setEmployeeManager(EmployeeManager employeeManager) { this.employeeManager = employeeManager; } public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long[] getIds() { return ids; } public void setIds(Long[] ids) { this.ids = ids; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } // action public String add() { if (this.employeeManager.getEmployee(employee.getName(), employee.getPhone()) != null) { return INPUT; } else { this.employeeManager.addEmployee(employee); return SUCCESS; } } public String list() { this.employees = this.employeeManager.listEmployee(); if (this.employees.size() > 0) { return SUCCESS; } else { return INPUT; } } public String delete() { if (null != this.id) { this.employeeManager.deleteEmployee(this.id); } else { if (ids.length > 0) { for (Long id : ids) { this.employeeManager.deleteEmployee(id); } } } if (this.employeeManager.listEmployee().size() > 0) { return SUCCESS; } else { return INPUT; } } public String saveOrUpdate() { this.employeeManager.saveOrUpdate(employee); return SUCCESS; } public String edit() { this.employee = this.employeeManager.getEmployee(id); return SUCCESS; } public String save() { Long id = this.employee.getId(); employee = this.employeeManager.getEmployee(id); return SUCCESS; } public String update() { Long id = this.employee.getId(); this.employeeManager.saveOrUpdate(employee); return SUCCESS; } public String login() { Employee emp = this.employeeManager.getEmployee(name, phone); List list = this.employeeManager.listEmployee(); if (null != emp) { return SUCCESS; } else { if (list.size() > 0) { return INPUT; } else { return ERROR; } } }}