项目结构图
实体类
Department1.java
import java.util.Set;
import java.util.HashSet;
public class Department1 {
private int did;
private String deptname;
//定义部门表对员工表的一对多关系
private Set<Employee1> emps=new HashSet<>(); //get和set方法以及构造器省略 }
Employee1.java
import java.util.HashSet;
import java.util.Set;
public class Employee1 {
private int eid;
private int did;
private String empname;
private String title;
private Department1 depts=new Department1(); //多对一关系(员工对部门的多对一关系)
private Set<ExtraApply1> extrs=new HashSet<>();//一对多关系(员工和申请加班表的一对多关系) }
//get和set方法以及构造器省略
ExtraApply1.java
import java.sql.Date;
public class ExtraApply1 {
private int oid;
private int eid;
private Date extradate;
private String starttime;
private String endtime;
private int hours;
private String reason;
//申请加班表对员工的多对一关系
private Employee1 emps=new Employee1();
//get和set方法以及构造器省略
}
hbm.xml文件配置
Department1.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">
<!-- Generated 2017-8-10 9:58:31 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.mingde.po.Department1" table="DEPARTMENT1">
<id name="did" type="int">
<column name="DID" />
<generator class="sequence" >
<param name="sequence">sequ_dept</param>
</generator>
</id>
<property name="deptname" type="java.lang.String">
<column name="DEPTNAME" />
</property>
<set name="emps" table="EMPLOYEE1" inverse="false" lazy="false" fetch="join">
<key>
<column name="DID" />
</key>
<one-to-many class="com.mingde.po.Employee1" />
</set>
</class>
</hibernate-mapping>
Employee1.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">
<!-- Generated 2017-8-10 9:58:31 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.mingde.po.Employee1" table="EMPLOYEE1">
<id name="eid" type="int">
<column name="EID" />
<generator class="sequence" >
<param name="sequence">sequ_emp</param>
</generator>
</id>
<property name="empname" type="java.lang.String">
<column name="EMPNAME" />
</property>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<many-to-one name="depts" class="com.mingde.po.Department1" lazy="false" fetch="join">
<column name="DID" />
</many-to-one>
<set name="extrs" table="EXTRAAPPLY1" inverse="false" lazy="false">
<key>
<column name="EID" />
</key>
<one-to-many class="com.mingde.po.ExtraApply1" />
</set>
</class>
</hibernate-mapping>
ExtraApply1.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">
<!-- Generated 2017-8-10 9:58:31 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.mingde.po.ExtraApply1" table="EXTRAAPPLY1">
<id name="oid" type="int">
<column name="OID" />
<generator class="sequence" >
<param name="sequence">sequ_ext</param>
</generator>
</id>
<property name="extradate" type="java.sql.Date">
<column name="EXTRADATE" />
</property>
<property name="starttime" type="java.lang.String">
<column name="STARTTIME" />
</property>
<property name="endtime" type="java.lang.String">
<column name="ENDTIME" />
</property>
<property name="hours" type="int">
<column name="HOURS" />
</property>
<property name="reason" type="java.lang.String">
<column name="REASON" />
</property>
<many-to-one name="emps" class="com.mingde.po.Employee1" lazy="false" fetch="join">
<column name="EID" />
</many-to-one>
</class>
</hibernate-mapping>
hibernate.cfg.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">123</property>
<property name="show_sql"></property>
<property name="format_sql"></property>
<mapping resource="com/mingde/po/Department1.hbm.xml"/>
<mapping resource="com/mingde/po/Employee1.hbm.xml"/>
<mapping resource="com/mingde/po/ExtraApply1.hbm.xml"/>
</session-factory>
</hibernate-configuration>
struts.xml文件配置(通配符配置)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.devMode" value="true"></constant>
<package name="struts" extends="struts-default">
<action name="*_*" class="com.mingde.action.DAction" method="{2}">
<result name="{2}">/WEB-INF/{1}/{2}.jsp</result>
<result name="to_list" type="redirect">{1}_list</result>
</action>
</package>
</struts>
utils包的HibernateSessionFactory.java
package com.mingde.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateSessionFactory {
private static SessionFactory sessionFactory;
private static Configuration config=new Configuration();
private static ThreadLocal<Session> threadLocal=new ThreadLocal<>();
static{
config.configure();
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory=config.buildSessionFactory(serviceRegistry);
}
//获取其session
public static Session getSession(){
Session session = threadLocal.get();
if(session==null || !session.isOpen()){
if(sessionFactory==null){
rebuildSessioinFactory();
}
}
session=(sessionFactory!=null)?sessionFactory.openSession():null;
threadLocal.set(session);
return session;
}
//重建session
private static void rebuildSessioinFactory() {
config.configure();
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory=config.buildSessionFactory(serviceRegistry);
}
//关闭session
public static void closeSessionFactory(){
Session session = threadLocal.get();
threadLocal.set(null);
if(session!=null)session.close();
}
}
dao层
IBaseDao.java
package com.mingde.dao;
import java.util.List;
import java.util.Map;
public interface IBaseDao {
//查询莫个类的所有信息
List findAll(String hql);
//通过id查找到某个类
Object findOne(Class<?> class1, int id);
//添加保存
void save(Object obj);
//修改保存
void update(Object obj);
//删除
void del(Object obj);
}
BaseDaoImpl.java
package com.mingde.dao.impl;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.mingde.dao.IBaseDao;
import com.mingde.utils.HibernateSessionFactory;
public class BaseDaoImpl implements IBaseDao {
@Override
public List findAll(String hql) {
List list=null;
//获取session
Session session = HibernateSessionFactory.getSession();
//创建事务
Transaction tx=null;
try {
//开启事务
tx=session.beginTransaction();
//执行命令
list=session.createQuery(hql).list();
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
tx.rollback();
}finally{
//关闭事务
HibernateSessionFactory.closeSessionFactory();
}
return list;
}
@Override
public Object findOne(Class<?> class1, int i) {
Object obj=null;
//获取session
Session session = HibernateSessionFactory.getSession();
//创建事务
Transaction tx=null;
try {
//开启事务
tx=session.beginTransaction();
//执行命令
obj=session.get(class1, i);
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
tx.rollback();
}finally{
//关闭事务
HibernateSessionFactory.closeSessionFactory();
}
return obj;
}
@Override
public void save(Object obj) {
//获取session
Session session = HibernateSessionFactory.getSession();
//创建事务
Transaction tx=null;
try {
//开启事务
tx=session.beginTransaction();
//执行命令
session.save(obj);
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
tx.rollback();
}finally{
//关闭事务
HibernateSessionFactory.closeSessionFactory();
}
}
@Override
public void update(Object obj) {
//获取session
Session session = HibernateSessionFactory.getSession();
//创建事务
Transaction tx=null;
try {
//开启事务
tx=session.beginTransaction();
//执行命令
session.merge(obj);
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
tx.rollback();
}finally{
//关闭事务
HibernateSessionFactory.closeSessionFactory();
}
}
@Override
public void del(Object obj) {
//获取session
Session session = HibernateSessionFactory.getSession();
//创建事务
Transaction tx=null;
try {
//开启事务
tx=session.beginTransaction();
//执行命令
session.delete(obj);
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
tx.rollback();
}finally{
//关闭事务
HibernateSessionFactory.closeSessionFactory();
}
}
}
Action类
package com.mingde.action;
import java.util.ArrayList;
import java.util.List;
import com.mingde.dao.IBaseDao;
import com.mingde.dao.impl.BaseDaoImpl;
import com.mingde.po.Department1;
import com.mingde.po.Employee1;
import com.mingde.po.ExtraApply1;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class DAction extends ActionSupport{
private IBaseDao bd=new BaseDaoImpl();
private List<Department1> depts=new ArrayList<>();
private List<ExtraApply1> exs=new ArrayList<>();
private List<Employee1> emps=new ArrayList<>();
private String ename;
private Integer did;
private String extradate1;
private String extradate2;
private ExtraApply1 ext=new ExtraApply1();
//模糊查询
public String list() throws Exception {
//因为要显示申请加班表,所以应查询加班表
/**若查询内容需关联到其他表时,可用该表的属性表(如:ExtraApply1中若要查询到Employee1表,可用到ExtraApply1中所关联到Employee1表的属性)
如下的模糊查询利用ExtraApply1表中的emps属性对象,即可查询到Employee1表中相关的内容信息
**/
String hql="from ExtraApply1 where 1=1 ";
if(ename!=null && !"".equals(ename)){
hql+=" and emps.empname like '%"+ename+"%' ";
}
if(extradate1!=null && !"".equals(extradate1) && extradate2!=null && !"".equals(extradate2)){
hql+=" and extradate between to_date('"+extradate1+"','yyyy-mm-dd') and to_date('"+extradate2+"','yyyy-mm-dd')";
}
if(did!=null && did!=0){
hql+=" and emps.depts.did="+did;
}
exs=bd.findAll(hql);
//加载部门(查询用到)
depts=bd.findAll("from Department1");
return "list";
}
//到添加页面
public String toAdd() throws Exception {
emps=bd.findAll("from Employee1");
return "toAdd";
}
//添加
public String add() throws Exception {
bd.save(ext);
return "to_list";
}
//到修改页面
public String toUpdate() throws Exception {
emps=bd.findAll("from Employee1");
ext=(ExtraApply1) bd.findOne(ext.getClass(), ext.getOid());
return "toUpdate";
}
//修改
public String update() throws Exception {
bd.update(ext);
return "to_list";
}
//删除
public String del() throws Exception {
ext=(ExtraApply1) bd.findOne(ext.getClass(), ext.getOid());
bd.del(ext);
return "to_list";
}
//处了dao不用get和set之外,其他都要添加get和set方法(在此省略不写)
}
JSP页面
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>加班请列表</h2>
<table width=800 align="center" border=1 rules="all">
<tr>
<td colspan=8>
<s:form action="d_list" theme="simple">
申请人姓名:<s:textfield name="ename" style="margin-right:50px" ></s:textfield>
加班日期:<s:textfield name="extradate1" ></s:textfield>
- <s:textfield name="extradate2"></s:textfield><br>
部门:<s:select name="did" headerKey="0" headerValue="全部部门" list="depts" listKey="did" listValue="deptname" ></s:select>
<s:submit value="查询" style="margin-left:30px" ></s:submit>
<input type="button" value="新建申请" style="margin-left:50px"
οnclick="location.href='d_toAdd'" />
</s:form>
</td>
</tr>
<tr>
<th>申请人</th><th>申请人部门</th><th>加班时间</th><th>开始时间</th><th>结束时间</th><th>小时数</th><th>加班事由</th><th>操作</th>
</tr>
<s:iterator value="exs">
<tr align="center">
<!-- 调用出所关联的对象的属性 -->
<td><s:property value="emps.empname"/></td>
<td><s:property value="emps.depts.deptname"/></td>
<td><s:date format="yyyy-MM-dd" name="extradate"/></td>
<td><s:property value="starttime"/></td>
<td><s:property value="endtime"/></td>
<td><s:property value="hours"/></td>
<td><s:property value="reason"/></td>
<td>
<a href="d_toUpdate?ext.oid=${oid }">修改</a>
<s:a href="d_del?ext.oid=%{oid }" οnclick="return confirm('确定删除?')" >删除</s:a>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>
toAdd.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="d_add">
<!-- 这里不能直接写ext.eid,虽然ExtraApply1对象也有该属性,
但是要用到ExtraApply1对象所关联的对象才能添加信息,
因为你之前设置了多对一映射关系,所以要用所关联的映射对象才能添加 -->
<s:select name="ext.emps.eid" list="emps" listKey="eid" listValue="empname" label="申请人" />
<s:textfield name="ext.extradate" label="加班日期"></s:textfield>
<s:textfield name="ext.starttime" label="开始时间"></s:textfield>
<s:textfield name="ext.endtime" label="结束时间"></s:textfield>
<s:textfield name="ext.hours" label="小时" value=""></s:textfield>
<s:textfield name="ext.reason" label="加班原因"></s:textfield>
<s:submit value="添加"></s:submit>
</s:form>
</body>
</html>
toUpdate.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="d_update">
<s:hidden name="ext.oid"></s:hidden>
<!-- 这里不能直接写ext.eid,虽然ExtraApply1对象也有该属性,
但是要用到ExtraApply1对象所关联的对象才能添加信息,
因为你之前设置了多对一映射关系,所以要用所关联的映射对象才能添加 -->
<s:select name="ext.emps.eid" list="emps" listKey="eid" listValue="empname" label="申请人" />
<s:textfield name="ext.extradate" label="加班日期">
<s:param name="value">
<s:date name="ext.extradate" format="yyyy-MM-dd"/>
</s:param>
</s:textfield>
<s:textfield name="ext.starttime" label="开始时间"></s:textfield>
<s:textfield name="ext.endtime" label="结束时间"></s:textfield>
<s:textfield name="ext.hours" label="小时" ></s:textfield>
<s:textfield name="ext.reason" label="加班原因"></s:textfield>
<s:submit value="修改"></s:submit>
</s:form>
</body>
</html>