OpenSessionInView模式

一 在Hibernate中实现DAO时,获取Session的方式要改变

1. openSession 从字面上可以看得出来,是打开一个新的session对象,而且每次使用都是打开一个新的session,

假如连续使用多次,则获得的session不是同一个对象,并且使用完需要调用close方法关闭session。

2. getCurrentSession,从字面上可以看得出来,是获取当前上下文一个session对象,当第一次使用此方法时,

会自动产生一个session对象,并且连续使用多次时,得到的session都是同一个对象,

这就是与openSession的区别之一,

简单而言,getCurrentSession 就是:如果有已经使用的,用旧的,如果没有,建新的。

不用手动关闭session,在事务提交后,会自动关闭。

要求增删改查都要有事务环境

二 实现OpenSessionInView模式的步骤

  • 实现过滤器(Spring有提供的)
// jsp  servlet  filter都是javaweb的原生组件,都是由web容器创建的组件
public class OpenSessionInViewFilter implements Filter {


	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		Transaction tx = null;
		try {
			//使用getCurrentSessoin()获取Session
			Session session = HibernateSessionUtils.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			chain.doFilter(request, response); // 访问下一个过滤器,如果没有,就访问目标资源
			tx.commit(); //提交事务后关闭session
		} catch (HibernateException e) {
			e.printStackTrace();
		} 

	}

	@Override
	public void destroy() {
	
	}

}

 

  • 配置过滤器
<!-- 配置OpenSessionInViewFilter -->
  <filter>
  	<filter-name>opensession</filter-name>
  	<filter-class>com.gec.hiber.utils.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>opensession</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  • 配置hibernate.cfg.xml,指定session的上下文
<property name="current_session_context_class">thread</property>
  • 优化DAO,DAO获取session的方式要和OpenSessionInViewFilter一致
@Override
	public List<Emp> findAllEmp() {
		Session session = HibernateSessionUtils.getSessionFactory().getCurrentSession();
		Criteria criteria = session.createCriteria(Emp.class);
		List<Emp> list = criteria.list();

		return list;
	}

 

三 综合案例

1. 技术 : hibernate+servlet/jsp

2. 数据库:

 

3. 使用反向工程生成双向一对多关系。

4.使用OpenSessionInView模式

5. 实现DAO: FilminfoDao和FilmtypeDao

6. 控制器 Servlet

- FilmSearchServlet

- FilmAddServlet

注意:表单数据提交后,非空判断,数据类型的转换!

7.视图层 Jsp

控制器参考:

1. FilmSearchServlet

public class FilmSearchServlet extends HttpServlet {

	
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		FilminfoDao filmInfoDaoImpl = new FilminfoDaoImpl();
		
		String filmname = request.getParameter("filmname");
	
		String actor = request.getParameter("actor");		
		String director = request.getParameter("director");
		
		Double lowPrice = null;
		if (request.getParameter("lowPrice") != null && !"".equals(request.getParameter("lowPrice"))) {
			lowPrice = Double.parseDouble(request.getParameter("lowPrice"));
		}	
		Double highPrice = null;
		if (request.getParameter("highPrice") != null && !"".equals(request.getParameter("highPrice"))) {
			highPrice = Double.parseDouble(request.getParameter("highPrice"));
		}
		
		Filmtype filmType = new Filmtype();
		filmType.setTypeid(Integer.parseInt(request.getParameter("typeid")));
	
		Filminfo filmInfo = new Filminfo(filmname, actor, director, lowPrice, highPrice );
		filmInfo.setFilmtype(filmType);
		
		List<Filminfo> list = filmInfoDaoImpl.searchFilminfos(filmInfo);
		System.out.println(list);
		
		request.setAttribute("result", list);
		request.getRequestDispatcher("/page/result.jsp").forward(request, response);
	}
	
}

2. FilmAddServlet

public class FilmAddServlet extends HttpServlet {

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		
	    request.setCharacterEncoding("UTF-8");
	    
		String filmname = request.getParameter("filmname");
		System.out.println(filmname); //日志调试输出
		int typeid = Integer.parseInt(request.getParameter("typeid"));
		String actor = request.getParameter("actor");
		String director = request.getParameter("director");
		Double ticketprice = Double.parseDouble(request.getParameter("ticketprice"));
		
		FilmtypeDao typeDao = new FilmtypeDaoImpl();
		Filmtype type = new Filmtype();  
		type.setTypeid(typeid);
		
		FilminfoDao FilmInfoDaoImpl = new FilminfoDaoImpl();
		
		Filminfo film = new Filminfo(filmname, actor, director, ticketprice);	
		//设置对象间的关联关系
		film.setFilmtype(type);
		
		FilmInfoDaoImpl.addFilm(film);
		response.sendRedirect(request.getContextPath()+"/cinema.jsp");
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值