SSH增删查改详解Word文档下载推荐.docx
《SSH增删查改详解Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《SSH增删查改详解Word文档下载推荐.docx(23页珍藏版)》请在冰豆网上搜索。
returnthis.id;
publicvoidsetId(Longid){
this.id=id;
publicStringgetName(){
returnthis.name;
publicvoidsetName(Stringname){
publicStringgetAddress(){
returnthis.address;
publicvoidsetAddress(Stringaddress){
publicStringgetPhone(){
returnthis.phone;
publicvoidsetPhone(Stringphone){
}
2、把实体类Books的属性映射到books表,生成下面的books.hbm.xml文件:
<
?
xmlversion="
1.0"
encoding="
utf-8"
>
!
DOCTYPEhibernate-mappingPUBLIC"
-//Hibernate/HibernateMappingDTD3.0//EN"
"
--
MappingfileautogeneratedbyMyEclipsePersistenceTools
-->
hibernate-mapping>
<
classname="
panyrm.model.Employee"
table="
Employee"
schema="
dbo"
catalog="
CompanyRM"
idname="
id"
type="
java.lang.Long"
columnname="
ID"
/>
generatorclass="
native"
/id>
propertyname="
name"
java.lang.String"
Name"
length="
50"
/property>
address"
Address"
phone"
Phone"
/class>
/hibernate-mapping>
四、建立DAO层
1、建立DAO的接口类:
EmployeeDao
packagepanyrm.dao;
importjava.util.List;
importpanyrm.model.Employee;
publicinterfaceEmployeeDao{
publicvoidadd(Employeee);
//添加新雇员
publicEmployeegetEmployee(Longid);
//根据雇员的ID显示雇员的所有信息
publicListgetEmployees();
//显示所有雇员信息
publicvoiddelete(Longid);
//根据雇员ID删除雇员
publicvoidsaveOrUpdate(Employeee);
//根据雇员ID,修改或更新某个雇员信息
publicListgetEmployee(Stringname,Stringphone);
//判断是否为合法雇员
2、实现此接口的类文件,EmployeeDaoImpl
packagepanyrm.dao.impl;
importjava.util.Iterator;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
importpanyrm.dao.EmployeeDao;
publicclassEmployeeDaoImplextendsHibernateDaoSupportimplementsEmployeeDao{
privateSessionFactorysessionFactory;
publicSessionFactorygetFactory(){
returnsessionFactory;
//hibernate SessionFactory对象,由spring注入.
publicvoidsetFactory(SessionFactorysessionFactory){
this.sessionFactory=sessionFactory;
//get/set方法在spring注入时使用
publicvoidadd(Employeee){
/**
*//**
*
函数说明:
添加雇员
参数说明:
对象
返回值:
*/
this.getHibernateTemplate().save(e);
publicvoiddelete(Longid){
删除雇员
对象
this.getHibernateTemplate().delete(this.getEmployee(id));
publicEmployeegetEmployee(Longid){
获得一个雇员的所有信息
ID
return(Employee)this.getHibernateTemplate().get(Employee.class,id);
publicListgetEmployees(){
/**
获得所有雇员的信息
信息的集合
returnthis.getHibernateTemplate().find("
fromEmployee"
);
publicListgetEmployee(Stringname,Stringphone){
Objecto[]={name,phone};
System.out.println("
查询的结果是:
+name+phone);
selectemployeefromEmployeeasemployeewhereemployee.name=?
andemployee.phone=?
o);
publicvoidsaveOrUpdate(Employeee){
修改雇员信息
this.getHibernateTemplate().saveOrUpdate(e);
五、建立业务逻辑层
注明:
在业务逻辑层需要认真思考每个业务逻辑所能用到的持久层对象和DAO。
DAO层之上是业务逻辑层,DAO类可以有很多个,但业务逻辑类应该只有一个,可以在业务逻辑类中调用各个DAO类进行操作。
1、创建服务接口类EmployeeManager
packagepanyrm.service;
publicinterfaceEmployeeManager{
publicvoidaddEmployee(Employeee);
//增加雇员
publicListlistEmployee();
//获得雇员的集合
publicvoiddeletEmployee(Longid);
//删除雇员
publicvoidsavaOrUpdate(Employeee);
//修改雇员
publicEmployeegetEmployee(Stringname,Stringphone);
//根据雇员的某个记录查询雇员信息
publicEmployeeggetEmployee(Longid);
//根据ID查询雇员,实现修改操作
2、实现此接口类:
EmployeeManagerImpl:
packagepanyrm.service.impl;
importpanyrm.service.EmployeeManager;
publicclassEmployeeManagerImplimplementsEmployeeManager{
privateEmployeeDaoemployeeDao;
publicvoidsetEmployeeDao(EmployeeDaoemployeeDao){
this.employeeDao=employeeDao;
publicvoidaddEmployee(Employeee){
this.employeeDao.add(e);
publicvoiddeletEmployee(Longid){
this.employeeDao.delete(id);
publicListlistEmployee(){
returnthis.employeeDao.getEmployees();
publicEmployeegetEmployee(Stringname,Stringphone){
Listlist=employeeDao.getEmployee(name,phone);
if(list!
=null&
&
list.size()==1){
System.out.println("
return(Employee)list.get(0);
}
else
returnnull;
publicvoidsavaOrUpdate(Employeee){
employeeDao.saveOrUpdate(e);
publicEmployeeggetEmployee(Longid){
获得一个雇员的信息
returnemployeeDao.getEmployee(id);
六、创建Action类:
EmployeeAction.java:
packagepanyrm.action;
importcom.opensymphony.xwork2.ActionSupport;
importcom.opensymphony.xwork2.ModelDriven;
publicclassEmployeeActionextendsActionSupportimplementsModelDriven{
privateEmployeeManageremployeeManager;
privateListemployees;
privateEmployeeemployee;
publicvoidsetEmployeeManager(EmployeeManageremployeeManager){
this.employeeManager=employeeManager;
publicStringadd(){
employeeManager.addEmployee(employee);
returnSUCCESS;
publicStringlist(){
this.employees=employeeManager.listEmployee();
publicStringdelete(){
employeeManager.deletEmployee(id);
publicStringsaveOrUpdate(){
employeeManager.savaOrUpdate(employee);
publicStringedit(){
employee=employeeManager.ggetEmployee(id);
publicStringsave(){
Longid=this.getEmployee().getId();
publicStringupdate(){
employeeManager.savaOrUpdate(this.getEmployee());
publicEmployeegetEmployee(){
returnemployee;
publicvoidsetEmployee(Employeeemployee){
this.employee=employee;
returnemployees;
publicvoidsetEmployees(Listemployees){
this.employees=employees;
returnid;
publicEmployeegetModel(){
七、Web页面
1、主页面:
index.jsp,其代码如下
%@pagelanguage="
java"
import="
java.util.*"
pageEncoding="
GBK"
%>
html>
head>
/head>
body>
center>
h1>
人力资源管理系统<
/h1>
/center>
hrcolor="
red"
ahref="
add.jsp"
雇员登记<
/a>
list.action"
显示用户列表<
/body>
/html>
2、增加页面:
%@tagliburi="
/struts-tags"
prefix="
s"
%>
title>
add.jsp<
/title>
--增加雇员信息页面-->
请输入雇员信息:
s:
formaction="
add"
textfieldname="
employee.name"
label="
姓名"
/s:
textfield>
employee.address"
地址"
employee.phone"
电话"
submit>
form>
3、列表页面:
list.jsp
list.jsp<
--显示用户列表页面-->
输出雇员的信息:
br/>
iteratorvalue="
employees"
table>
tr>
td>
编号:
/td>
propertyvalue="
/>
/tr>
姓名:
地址:
电话:
tdalign="
center"
edit.action?
id=<
propertyvalue='
id'
"
更新<
ahref='
urlaction="
delete"
paramname="
value="
param>
url>
'
删除<
/table>
iterator>
(1)、<
property>
:
得到'
value'
的属性,如果value没提供,默认为堆栈顶端的元素。
其相关的参数及使用如下表所示:
名称
必需
默认