• Open Session In View


    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

  • 相关阅读:
    Daily Scrumming* 2015.12.19(Day 11)
    Daily Scrumming* 2015.12.18(Day 10)
    软件工程 个人作业1
    网络1711-12&信管1711-12 图 作业评分
    网络1711-12 数据结构第一次作业评分
    Java课程设计
    网络1711-1712班 c 语言评分总表一览
    网络1711班 C语言第八次作业批改总结
    201621123050 《Java程序设计》第14周学习总结
    201621123050 《Java程序设计》第13周学习总结
  • 原文地址:https://www.cnblogs.com/1And0/p/5839875.html
Copyright © 2020-2023  润新知