Struts与Hibernate整合完成一个小案例

一,概述

1)终于将Struts与Hibernate学习完了,现在就同时在一个web项目中完成一个简单小案例的开发!

2)在前面博客中已经介绍了怎样导入项目必须的jar包及环境的配置,就不再赘述,直接进入主题吧!

二,开发

1)实体及映射文件准备

a)Dept.java(限制篇幅,setter和getter方法省略,下面也是一样)

package com.bighuan.entity;

import java.util.HashSet;
import java.util.Set;
/**
 * 部门表对应的实体类
 * @author bighuan
 *
 */
public class Dept {

	private int deptId;
	private String deptName;
	// 【一对多】 部门对应的多个员工
	private Set emps = new HashSet();

	public Dept() {
	}

	public Dept(int deptId, String deptName) {
		this.deptId = deptId;
		this.deptName = deptName;
	}
}
b)Dept.hbm.xml






	
		
			
		
		
		
			
			
		
	


c)Employee.java

package com.bighuan.entity;
/**
 * 员工表对应的实体类
 * @author bighuan
 *
 */
public class Employee {

	private int empId;
	private String empName;
	private double salary;
	// 【多对一】员工与部门
	private Dept dept;
	
	public Employee(){
		
	}
}

 d)Employee.hbm.xml






	
		
			
		
		
		

		

	


2)dao层开发

package com.bighuan.dao;

import com.bighuan.entity.Dept;
import com.bighuan.utils.HibernateUtils;

public class DeptDao {

	/**
	 * 主键查询
	 * @param id
	 * @return
	 */
	public Dept findById(int id){
		//获取session
		return (Dept) HibernateUtils.getSession().get(Dept.class, id);
	}
}

3)service层开发

package com.bighuan.service;

import com.bighuan.dao.DeptDao;
import com.bighuan.entity.Dept;
import com.bighuan.utils.HibernateUtils;

public class DeptService {

	//调用dao
	private DeptDao dao=new DeptDao();
	
	/**
	 * 主键查询
	 * @param id
	 * @return
	 */
	public Dept findById(int id){
		//获取session
		return dao.findById(id);
	}
}

4)HibernateUtils工具类准备

package com.bighuan.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

	//初始化SessionFactory
	private static SessionFactory sf;
	
	static{
		sf=new Configuration().configure().buildSessionFactory();
	}
	/**
	 * 创建(获取)session
	 * @return
	 */
	public static Session getSession(){
		//1,线程的方式创建session,必须要进行配置;
		//2,不用关闭,会自动关闭
		return sf.getCurrentSession();
	}
}


注意:通过线程方式获取session必须要在hibernate.cfg.xml中配置

5)Action开发

Action中主要就是通过主键查询一条数据,保存到request域中

package com.bighuan.action;

import com.bighuan.entity.Dept;
import com.bighuan.service.DeptService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DeptAction extends ActionSupport{

	private DeptService service=new DeptService();
	@Override
	public String execute() throws Exception {
		//主键查询,模拟数据
		Dept dept = service.findById(11);
		
		//保存到request域中
		ActionContext.getContext().getContextMap().put("dept", dept);
		return SUCCESS;
	}
}

6)session拦截器

package com.bighuan.interceptor;

import org.hibernate.Session;

import com.bighuan.utils.HibernateUtils;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
 * Session管理拦截器
 * 	当访问Action的时候,创建session
 * 	action->service->dao,获取的是这里创建的session
 * @author bighuan
 *
 */
public class SessionInterceptor extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		try {
			//1,先创建session
			Session session=HibernateUtils.getSession();
			
			//2,开启事务
			session.beginTransaction();
			
			//3,执行action
			String result = invocation.invoke();
			
			//4,提交事务
			session.getTransaction().commit();//不需要关闭session
			
			//5,返回结果视图
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
}

7)hibernate.cfg.xml




	
	
	
		
		com.mysql.jdbc.Driver
		jdbc:mysql:///hib_demo
		root
		abc
		
		org.hibernate.dialect.MySQL5Dialect
		
		
		
		true
		
		
		update
		
		thread
		
		
		
		org.hibernate.connection.C3P0ConnectionProvider
		
		5	
		2	
		5000	
		100	
		30000 
		2	
		
		
		
		
		
	

8)struts.xml

a)配置拦截器,action返回"success"就跳转到index.jsp,并显示数据!

b)代码






	
	

	
		
		
			
			
			
				
				
				
				
			
		
		
		
		
		
		
			
			/index.jsp
			
		
		

	


	


c)为了使用struts的核心功能,在web.xml中记得配置strtus的核心过滤器.


	
		struts2
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	
	
		struts2
		/*
	

9)显示数据

a)引入struts标签:<%@ taglib uri="/struts-tags" prefix="s" %>

b)显示数据:


   		

部门:

员工编号 员工姓名 薪水
没有员工信息!
c)访问action,效果图:

Struts与Hibernate整合完成一个小案例_第1张图片










你可能感兴趣的:(Hibernate)