struts2第五课.doc_第1页
struts2第五课.doc_第2页
struts2第五课.doc_第3页
struts2第五课.doc_第4页
struts2第五课.doc_第5页
已阅读5页,还剩58页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java软件工程师实训 张海林第五课 使用struts2+spring2.5+hibernate3.3完成员工管理系统(全注解方式)准备数据库和表create database mydbgouse mydbgocreate table dep( depid int identity primary key, depname varchar(50) )insert into dep values(技术部)insert into dep values(市场部)insert into dep values(人事部)insert into dep values(财务部) create table emp( eid int identity primary key, ename varchar(50), sex varchar(10), address varchar(100), depid int not null, birthday datetime, photo image)create table welfare( wid int identity primary key, wname varchar(50)insert into welfare values(电费)insert into welfare values(水费)insert into welfare values(住房补贴)insert into welfare values(三金)create table empwelfare( ewid int identity primary key, eid int not null, wid int not null)alter table emp add constraint fk_depid foreign key (depid) references dep(depid)alter table empwelfare add constraint fk_eid foreign key (eid) references emp(eid)alter table empwelfare add constraint fk_wid foreign key (wid) references welfare(wid)select * from depselect * from empselect * from welfareselect * from empwelfare数据库表关系如下图所示:一、 在当前工程引入struts2.1x,spring2.5和hibernate3.3二、 生成Pojo类以及Dao类和hibernate的映射文件。package com.po;import java.util.HashSet;import java.util.Set;/* * Dep entity. author MyEclipse Persistence Tools */public class Dep implements java.io.Serializable / Fieldsprivate Integer depid;private String depname;private Set emps = new HashSet(0);/ Constructors/* default constructor */public Dep() /* full constructor */public Dep(String depname, Set emps) this.depname = depname;this.emps = emps;/ Property accessorspublic 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;public Set getEmps() return this.emps;public void setEmps(Set emps) this.emps = emps; package com.po;import java.util.*;import java.util.HashSet;import java.util.Set;/* * Emp entity. author MyEclipse Persistence Tools */public class Emp implements java.io.Serializable / Fieldsprivate Integer eid;private Dep dep=new Dep();private String ename;private String sex;private String address;private Date birthday;private byte photo;private Set empwelfares = new HashSet(0);/ Constructors/* default constructor */public Emp() /* minimal constructor */public Emp(Dep dep) this.dep = dep;/* full constructor */public Emp(Dep dep, String ename, String sex, String address,Date birthday, byte photo, Set empwelfares) this.dep = dep;this.ename = ename;this.sex = sex;this.address = address;this.birthday = birthday;this.photo = photo;this.empwelfares = empwelfares;/ Property accessorspublic Integer getEid() return this.eid;public void setEid(Integer eid) this.eid = eid;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() 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 Set getEmpwelfares() return this.empwelfares;public void setEmpwelfares(Set empwelfares) this.empwelfares = empwelfares;红色部分为修改后的实体属性 红色部分为修改后的实体对应关系package com.po;import java.util.HashSet;import java.util.Set;/* * Welfare entity. author MyEclipse Persistence Tools */public class Welfare implements java.io.Serializable / Fieldsprivate Integer wid;private String wname;private Set empwelfares = new HashSet(0);/ Constructors/* default constructor */public Welfare() /* full constructor */public Welfare(String wname, Set empwelfares) this.wname = wname;this.empwelfares = empwelfares;/ Property accessorspublic Integer 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 = empwelfares; package com.po;/* * Empwelfare entity. author MyEclipse Persistence Tools */public class Empwelfare implements java.io.Serializable / Fieldsprivate Integer ewid;private Emp emp;private Welfare welfare;/ Constructors/* default constructor */public Empwelfare() /* full constructor */public Empwelfare(Emp emp, Welfare welfare) this.emp = emp;this.welfare = welfare;/ Property accessorspublic 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(Welfare welfare) this.welfare = welfare; 三、 编写实体类,对Pojo类进行封装,编写分页显示的PageBeanpackage com.bean;import java.io.*;import com.po.*;public class EmpBean private String wid;/福利private File pic;private String picContentType;private String picFileName;private Emp emp=new Emp();public String getWid() return wid;public void setWid(String wid) this.wid = wid;public File getPic() return pic;public void setPic(File pic) this.pic = pic;public String getPicContentType() return picContentType;public void setPicContentType(String picContentType) this.picContentType = picContentType;public String getPicFileName() return picFileName;public void setPicFileName(String picFileName) this.picFileName = picFileName;public Emp getEmp() return emp;public void setEmp(Emp emp) this.emp = emp;package com.bean;import java.util.*;public class PageBean private int cpage;private int maxpage;private int psize;private List pagelist;public int getCpage() return cpage;public void setCpage(int cpage) this.cpage = cpage;public int getMaxpage() return maxpage;public void setMaxpage(int maxpage) this.maxpage = maxpage;public int getPsize() return psize;public void setPsize(int psize) this.psize = psize;public List getPagelist() return pagelist;public void setPagelist(List pagelist) this.pagelist = pagelist;Dao类如下,并引入注解:package com.dao;import java.util.List;import java.util.Set;import org.hibernate.LockMode;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.ApplicationContext;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.stereotype.Service;import com.po.Dep;/* * A data access object (DAO) providing persistence and search support for Dep * entities. Transaction control of the save(), update() and delete() operations * can directly support Spring container-managed transactions or they can be * augmented to handle user-managed Spring transactions. Each of these methods * provides additional information for how to configure it for the desired type * of transaction control. * * see com.po.Dep * author MyEclipse Persistence Tools */Service(depDao)public class DepDAO extends HibernateDaoSupport private static final Logger log = LoggerFactory.getLogger(DepDAO.class);/ property constantspublic static final String DEPNAME = depname;protected void initDao() / do nothingpublic void save(Dep transientInstance) log.debug(saving Dep instance);try getHibernateTemplate().save(transientInstance);log.debug(save successful); catch (RuntimeException re) log.error(save failed, re);throw re;public void delete(Dep persistentInstance) log.debug(deleting Dep instance);try getHibernateTemplate().delete(persistentInstance);log.debug(delete successful); catch (RuntimeException re) log.error(delete failed, re);throw re;public Dep findById(java.lang.Integer id) log.debug(getting Dep instance with id: + id);try Dep instance = (Dep) getHibernateTemplate().get(com.po.Dep, id);return instance; catch (RuntimeException re) log.error(get failed, re);throw re;public List findByExample(Dep instance) log.debug(finding Dep instance by example);try List results = getHibernateTemplate().findByExample(instance);log.debug(find by example successful, result size: + results.size();return results; catch (RuntimeException re) log.error(find by example failed, re);throw re;public List findByProperty(String propertyName, Object value) log.debug(finding Dep instance with property: + propertyName+ , value: + value);try String queryString = from Dep as model where model.+ propertyName + = ?;return getHibernateTemplate().find(queryString, value); catch (RuntimeException re) log.error(find by property name failed, re);throw re;public List findByDepname(Object depname) return findByProperty(DEPNAME, depname);public List findAll() log.debug(finding all Dep instances);try String queryString = from Dep;return getHibernateTemplate().find(queryString); catch (RuntimeException re) log.error(find all failed, re);throw re;public Dep merge(Dep detachedInstance) log.debug(merging Dep instance);try Dep result = (Dep) getHibernateTemplate().merge(detachedInstance);log.debug(merge successful);return result; catch (RuntimeException re) log.error(merge failed, re);throw re;public void attachDirty(Dep instance) log.debug(attaching dirty Dep instance);try getHibernateTemplate().saveOrUpdate(instance);log.debug(attach successful); catch (RuntimeException re) log.error(attach failed, re);throw re;public void attachClean(Dep instance) log.debug(attaching clean Dep instance);try getHibernateTemplate().lock(instance, LockMode.NONE);log.debug(attach successful); catch (RuntimeException re) log.error(attach failed, re);throw re;public static DepDAO getFromApplicationContext(ApplicationContext ctx) return (DepDAO) ctx.getBean(DepDAO);package com.dao;import java.sql.Timestamp;import java.util.List;import java.util.Set;import org.hibernate.LockMode;import org.hibernate.Query;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.ApplicationContext;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.stereotype.Service;import com.po.Emp;/* * A data access object (DAO) providing persistence and search support for Emp * entities. Transaction control of the save(), update() and delete() operations * can directly support Spring container-managed transactions or they can be * augmented to handle user-managed Spring transactions. Each of these methods * provides additional information for how to configure it for the desired type * of transaction control. * * see com.po.Emp * author MyEclipse Persistence Tools * Service表示类的注解名称,依赖类使用注解名称关联此类 * */Service(empDAO)public class EmpDAO extends HibernateDaoSupport private static final Logger log = LoggerFactory.getLogger(EmpDAO.class);/ property constantspublic static final String ENAME = ename;public static final String SEX = sex;public static final String ADDRESS = address;public static final String PHOTO = photo;protected void initDao() / do nothingpublic void save(Emp transientInstance) log.debug(saving Emp instance);try getHibernateTemplate().save(transientInstance);log.debug(save successful); catch (RuntimeException re) log.error(save failed, re);throw re;public void delete(Emp persistentInstance) log.debug(deleting Emp instance);try getHibernateTemplate().delete(persistentInstance);log.debug(delete successful); catch (RuntimeException re) log.error(delete failed, re);throw re;public Emp findById(java.lang.Integer id) log.debug(getting Emp instance with id: + id);try Emp instance = (Emp) getHibernateTemplate().get(com.po.Emp, id);return instance; catch (RuntimeException re) log.error(get failed, re);throw re;public List findByExample(Emp instance) log.debug(finding Emp instance by example);try List results = getHibernateTemplate().findByExample(instance);log.debug(find by example successful, result size: + results.size(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论