Open Session In View模式的主要思想是:在用户的每一次请求过程始终保持一个Session对象打开着

实现步骤:

步骤一.创建一个Web项目,创建包cn.happy.util,创建HibernateUtil工具类

public class HibernateUtil{
private static final ThreadLocal sessionTL=new ThreadLocal();
private static Configuration cfg;
private final static SessionFactory factory;
static{
cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
}
public static Session currentSession()
{
Session session=(Session)sessionTL.get();
if(session==null)
{
session=factory.openSession();
sessionTL.set(session);
}
return session;
}
public static void closeSession()
{
Session session=(Session) sessionTL.get();
sessionTL.set(null);
session.close();
} }

步骤二.创建包cn.happy.entity,创建实体类Emp和Emp.hbm.xml小配置文件

public class Emp {
private Integer empId;
private String empName;
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.entity">
<class name="Emp" table="Emps">
<id name="empId">
<generator class="native">
</generator>
</id>
<property name="empName"></property>
<!-- 植入一个Dept对象 多对一 -->
</class>
</hibernate-mapping>

步骤三.创建包cn.happy.Dao创建类MyDao

public class MyDao{
public Object get(Class classz,Serializable id)
{
Object obj=HibernateUtil.currentSession().load(classz,id);
return obj;
} }

步骤四.创建包cn.happy.biz创建类MyBiz

public class MyBiz
{
public Object get(Class classz,Serializable id)
{
MyDao dao=new MyDao();
Obejct obj=dao.get(classz,id);
return obj;
}
}

步骤五.创建cn.happy.filter包创建类MyFilter,实现Filter接口,重写doFilter方法

public void doFilter(ServletRequet request,ServletResponse response,FilterChain arg2) throw IOException,ServletExption{
Session session=null;
Transaction tx=null;
session=HibernateUtil.currentSession();
tx=session.beginTransaction();
arg2.doFilter(request,response);
tx.commit();
HibernateUtil.closeSession();
}

步骤六.创建大配置文件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>
<!-- Database connection settings 数据库连接设置 -->
<!-- 驱动类 -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<!-- url地址 -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">Hibernate</property>
<property name="connection.password">orcl</property>
<!-- SQL dialect (SQL 方言) -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!--在控制台打印后台的SQL语句 -->
<property name="show_sql">true</property>
<!-- 格式化显示SQL -->
<!-- <property name="format_sql">true</property> -->
<!-- 自动生成表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 关联小配置 -->
<mapping resource="cn/happy/entity/Emp.hbm.xml" />
</session-factory>
</hibernate-configuration>

步骤七.在web.xml文件中配置fileter

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>openSessionInview</filter-name>
<filter-class>cn.happy.Filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInview</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

然后我们在index.xml文件中来显示数据

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.hibernate.Session"%>
<%@page import="cn.happy.Util.HibernateUtil" %>
<%@page import="org.hibernate.Transaction" %>
<%@page import="cn.happy.Biz.MyBiz" %>
<%@page import="cn.happy.entity.Emp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>OpenSessionInview</title> </head> <body>
<% MyBiz biz=new MyBiz();
    <!--拿到id为1的员工,在页面显示名字-->
Object object = biz.get(Emp.class, 1);
Emp emp=(Emp)object; %>
<%=emp.getEmpName() %>
</body>
</html>

这样我们就实现了OpenSessionInview

Open Session In View的更多相关文章

  1. Hibernate Open Session In View模式【转】

    来源:http://www.yybean.com/opensessioninviewfilter-role-and-configuration 一.作用 Spring为我们解决Hibernate的Se ...

  2. open Session In View和过滤器配置

    Open Session In View模式的主要思想是:当Web Request(浏览器请求)开始时,自动打开Session,当Web Request结束时,自动关闭Session.也就是说,Ses ...

  3. open Session In View模式

    首先看图说话: ****Open Session In View模式的主要思想是:在用户的每一次请求过程始终保持一个Session对象打开着*** 接下来就是代码: +++++++++++++++++ ...

  4. Spring Open Session In View

    提出:session在应用层就关闭,所以持久化要在应用层,但是到了view层持久化则session已经关闭 解决:session延迟到view层再关闭 原理:session(整个requestScop ...

  5. hibernate open session in view 抛出异常解决方法

    在使用open-session-in-view的时候,如果使用不当,有可能抛出两种异常1,NonUniqueObjectException2,在配合spring使用的时候会可能会抛出org.sprin ...

  6. Oracle V$SESSION详解

    V$SESSION是APPS用户下面对于SYS.V_$SESSION 视图的同义词. 在本视图中,每一个连接到数据库实例中的session都拥有一条记录.包括用户session及后台进程如DBWR,L ...

  7. No Hibernate Session bound to thread, and configuration does not allow creat

    No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...

  8. Spring与Hibernate集成中的Session问题

    主要讨论Spring与Hibernate集成中的session问题 1.通过getSession()方法获得session进行操作 public class Test extends Hibernat ...

  9. PHP学习2 — PHP Cookie 与 Session

    PHP Cookies cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 PHP,您能够创建并取回 ...

随机推荐

  1. Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现

    UI系列教程第八课:Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现 今天蓝老师要讲的是关于新浪新闻侧滑界面的实现.先看看原图: 如图所示,这种侧滑效果以另一种方式替 ...

  2. C语言排序算法复习

    排序算法有很多种,这里在复习和分析的基础上,做一个自己的总结: 首先要知道有哪些排序算法,google一下,有云C语言7大经典排序算法(也有8大).主要包括冒泡排序,快速排序,选择排序,插入排序,希尔 ...

  3. LOADRUNNER8.1卸载

    卸载LOADRUNNER8.1后,不能正常又一次安装的问题. Loadrunner 8.1 安装1.下载Loadrunner8.1 (官方英文版) 2.安装Loadrunner8.1 3.破解:htt ...

  4. Linux下Weblogic域的创建过程

    环境介绍:操作系统 :Redhat 5.5Weblogic :英文版 8.1.6 Weblogic安装目录 :/weblogic 一.域的建立执行下面语句进入weblogic的bin目录: cd /w ...

  5. 数据持久化之CoreData

    再次回归博客园, 已经实属不易了, 面临这近期忙忙碌碌的项目开发, 虽然并不是完全的没有闲暇时间, 但是怎么说呢, 也有着各种的无奈与曲折, 面临这产品需求的不断变化和页面的不断更新, 对于一个程序员 ...

  6. Android推送等耗电原因剖析

    原文链接:http://www.jianshu.com/p/584707554ed7 Android手机有两个处理器,一个是Application Processor(AP)基于ARM处理器,主要运行 ...

  7. 在浏览器中就可以写F#程序了,你试试吧

    学习F#有一种简单办法,不需要安装Visual Studio,在浏览器中就可以写F#了,非常方便,进入下面的链接,你可以试试. http://www.tryfsharp.org/Learn

  8. linux添加JAVA环境变量

    root用户: 1.修改文件vim /etc/profile 添加以下信息: export JAVA_HOME=/home/jdk1..0_79 (这里需要添加自己的JDK安装目录) export C ...

  9. zeromq源码分析笔记之架构(1)

    1.zmq概述 ZeroMQ是一种基于消息队列的多线程网络库,其对套接字类型.连接处理.帧.甚至路由的底层细节进行抽象,提供跨越多种传输协议的套接字.引用云风的话来说:ZeroMQ 并不是一个对 so ...

  10. IIS下 Yii Url重写

    下载URL重写组件 http://www.microsoft.com/zh-cn/download/details.aspx?id=7435 导入官方提供的.htaccess文件 Options +F ...