1、Hibernate第五课第五课 员工管理系统的升级一、 使用Struts+Hibernate完成员工管理系统二、 建立数据库和表create database empdbgouse empdbgo-部门表create table dep( depid int identity primary key, depname varchar(50) unique)-职位表create table positions( pid int identity primary key, pname varchar(50) not null unique)-福利表create table welfare( wid
2、 int identity primary key, wname varchar(50) not null unique)-员工表create table emp( eid int identity primary key, ename varchar(50), sex varchar(4), address varchar(150), birthday datetime, depid int not null references dep(depid), pid int not null references positions(pid), photo image)-员工福利表create
3、table empwelfare( ewid int identity primary key, eid int not null references emp(eid), wid int not null references welfare(wid)-薪资表create table salary( sid int identity primary key, smoney money, eid int not null unique references emp(eid)-插入基础数据insert into dep values(技术部)insert into dep values(财务部)
4、insert into dep values(市场部)insert into dep values(行政部)insert into dep values(人事部)insert into dep values(研发部)insert into positions values(软件工程师)insert into positions values(系统设计师)insert into positions values(财务专员)insert into positions values(测试工程师)insert into positions values(行政专员)insert into positio
5、ns values(人事专员)insert into welfare values(三金)insert into welfare values(交通补助)insert into welfare values(住房补助)insert into welfare values(取暖费)insert into welfare values(降温费)insert into welfare values(通讯费)insert into welfare values(午餐费)关系图如下:三、 实现步骤1、 在当前工程引入struts和hibernate组件包2、 生成pojo类和dao类生成的实体类如下所示
6、:(1)Dep.javapackage com.po;import java.util.HashSet;import java.util.Set;public class Dep implements java.io.Serializable / Fields private Integer depid; private String depname; private Set emps = new HashSet(0); / Constructors /* default constructor */ public Dep() /* full constructor */ public Dep
7、(String depname, Set emps) this.depname = depname; this.emps = emps; / Property accessors public Integer getDepid() return this.depid; public void setDepid(Integer depid) this.depid = depid; public String getDepname() return this.depname; public void setDepname(String depname) this.depname = depname
8、; public Set getEmps() return this.emps; public void setEmps(Set emps) this.emps = emps; (2)Positions.javapackage com.po;import java.util.HashSet;import java.util.Set;public class Positions implements java.io.Serializable / Fields private Integer pid; private String pname; private Set emps = new Has
9、hSet(0); / Constructors /* default constructor */ public Positions() /* minimal constructor */ public Positions(String pname) this.pname = pname; /* full constructor */ public Positions(String pname, Set emps) this.pname = pname; this.emps = emps; / Property accessors public Integer getPid() return
10、this.pid; public void setPid(Integer pid) this.pid = pid; public String getPname() return this.pname; public void setPname(String pname) this.pname = pname; public Set getEmps() return this.emps; public void setEmps(Set emps) this.emps = emps; (3)Emp.javapackage com.po;import java.util.*;import java
11、.util.HashSet;import java.util.Set;/* * Emp entity. author MyEclipse Persistence Tools */public class Emp implements java.io.Serializable / Fields private Integer eid; private Positions positions=new Positions(); private Dep dep=new Dep(); private String ename; private String sex=男; private String a
12、ddress; private Date birthday; private byte photo; private Set empwelfares = new HashSet(0); private Set salaries = new HashSet(0); / Constructors /* default constructor */ public Emp() /* minimal constructor */ public Emp(Positions positions, Dep dep) this.positions = positions; this.dep = dep; /*
13、full constructor */ public Emp(Positions positions, Dep dep, String ename, String sex, String address, Date birthday, byte photo, Set empwelfares, Set salaries) this.positions = positions; this.dep = dep; this.ename = ename; this.sex = sex; this.address = address; this.birthday = birthday; this.phot
14、o = photo; this.empwelfares = empwelfares; this.salaries = salaries; / Property accessors public Integer getEid() return this.eid; public void setEid(Integer eid) this.eid = eid; public Positions getPositions() return this.positions; public void setPositions(Positions positions) this.positions = pos
15、itions; public Dep getDep() return this.dep; public void setDep(Dep dep) this.dep = dep; public String getEname() return this.ename; public void setEname(String ename) this.ename = ename; public String getSex() return this.sex; public void setSex(String sex) this.sex = sex; public String getAddress(
16、) return this.address; public void setAddress(String address) this.address = address; public Date getBirthday() return this.birthday; public void setBirthday(Date birthday) this.birthday = birthday; public byte getPhoto() return this.photo; public void setPhoto(byte photo) this.photo = photo; public
17、 Set getEmpwelfares() return this.empwelfares; public void setEmpwelfares(Set empwelfares) this.empwelfares = empwelfares; public Set getSalaries() return this.salaries; public void setSalaries(Set salaries) this.salaries = salaries; (4)Empwelfare.javapackage com.po;/* * Empwelfare entity. author My
18、Eclipse Persistence Tools */public class Empwelfare implements java.io.Serializable / Fields private Integer ewid; private Emp emp; private Welfare welfare; / Constructors /* default constructor */ public Empwelfare() /* full constructor */ public Empwelfare(Emp emp, Welfare welfare) this.emp = emp;
19、 this.welfare = welfare; / Property accessors public Integer getEwid() return this.ewid; public void setEwid(Integer ewid) this.ewid = ewid; public Emp getEmp() return this.emp; public void setEmp(Emp emp) this.emp = emp; public Welfare getWelfare() return this.welfare; public void setWelfare(Welfar
20、e welfare) this.welfare = welfare; (5)Welfare.javapackage com.po;import java.util.HashSet;import java.util.Set;/* * Welfare entity. author MyEclipse Persistence Tools */public class Welfare implements java.io.Serializable / Fields private Integer wid; private String wname; private Set empwelfares =
21、new HashSet(0); / Constructors /* default constructor */ public Welfare() /* minimal constructor */ public Welfare(String wname) this.wname = wname; /* full constructor */ public Welfare(String wname, Set empwelfares) this.wname = wname; this.empwelfares = empwelfares; / Property accessors public In
22、teger getWid() return this.wid; public void setWid(Integer wid) this.wid = wid; public String getWname() return this.wname; public void setWname(String wname) this.wname = wname; public Set getEmpwelfares() return this.empwelfares; public void setEmpwelfares(Set empwelfares) this.empwelfares = empwe
23、lfares; (6)Salary.javapackage com.po;/* * Salary entity. author MyEclipse Persistence Tools */public class Salary implements java.io.Serializable / Fields private Integer sid; private Emp emp; private Double smoney; / Constructors /* default constructor */ public Salary() /* minimal constructor */ p
24、ublic Salary(Emp emp) this.emp = emp; /* full constructor */ public Salary(Emp emp, Double smoney) this.emp = emp; this.smoney = smoney; / Property accessors public Integer getSid() return this.sid; public void setSid(Integer sid) this.sid = sid; public Emp getEmp() return this.emp; public void setE
25、mp(Emp emp) this.emp = emp; public Double getSmoney() return this.smoney; public void setSmoney(Double smoney) this.smoney = smoney; 3、 打开struts-config.xml文件,在设计视图建立ForemBean类和Action类,并设计访问流程。EmpForm类设计如下:/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package com.
26、zhhlk.struts.form;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;import org.apache.struts.upload.FormFile;import com.po.Emp;/* * MyEclipse Struts * Creation date: 05-03
27、-2012 * * XDoclet definition: * struts.form name=empForm */public class EmpForm extends ActionForm /* * Generated fields */ /* birthday property */ private String birthday; /* wid property */ private String wid;/用于获取页面显示的福利信息 /* upfile property */ private FormFile upfile;/文件上传的属性 /* salary property
28、*/ private String salary; /* emp property */ private Emp emp=new Emp(); /* * Generated Methods */ /* * Method validate * param mapping * param request * return ActionErrors */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) / TODO Auto-generated method stub return null; /* * Method reset * param mapping * param request */ public void reset(ActionMapping mapping, HttpServletRequest request) / TODO Auto-generated method stub /* * Returns the birthday. * return String
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1