• open Session In View和过滤器配置



    Open Session In View模式的主要思想是:Web Request(浏览器请求)开始时,自动打开Session,当Web Request结束时,自动关闭Session。也就是说,Session的生命周期与页面请求保持同步。


     实现步骤:(分层架构)(web工程)

      1.entity层(实体层)

      2.dao层(数据访问层)

      3.util层(工具层)

      4.biz层(业务逻辑层)

      5.filter层(过滤器)

      6.进行过滤器在网站xml的配置

    结构图如下:

     


     1.entity层


       《1》.进行封装字段,小配置书写。

     实体类:


     


     Emp.hbm.xml: 



    2. dao层:


     


     3.util层(工具层)


    package cn.hq.util;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtils {
    
            //getCurrentSession()底层实现原理
            //ThreadLocal变量
            public static final ThreadLocal<Session> threadTL=new ThreadLocal<Session>();
    
           //我想直接调用一个方法,获取Session
           //定义一个sessionFactory对象
            private static SessionFactory factory;
            private static Configuration cfg;
            static{
                cfg=new Configuration().configure();
                factory=cfg.buildSessionFactory();
            }
            //提供一个静态方法
            public static Session currentSession(){
                Session session=threadTL.get();
                if(session==null){  //当前线程中没有session对象
                    session=factory.openSession();
                    threadTL.set(session);
                }
                return session;
                
            }
            //关闭session
            public static void closeSession(){
                //获取线程中的session
                Session session = threadTL.get();
                if (session!=null) {
                    threadTL.set(null);
                    session.close();
                }
            }
            
        }

    4.biz层(业务逻辑层)


      


     5.filter层(过滤器)


    @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            Session session = null;
            Transaction tx = null;
            try {
                session = HibernateUtils.currentSession();
                System.out.println("filter	"+session.hashCode());
                tx = session.beginTransaction();
                // 执行请求处理链
                chain.doFilter(request, response);
                // 返回响应时,提交事务
                tx.commit();
            } catch (HibernateException e) {
                e.printStackTrace();
                tx.rollback();
            } finally {
                // 关闭session
                HibernateUtils.closeSession();
            }
            
        }

    6.进行过滤器在网站xml的配置


    web.xml进行配置:


    index.jsp界面:


    实现效果:


    注意:(常见问题)

    解决方案:

    查看一下biz层是否开启事务以下界面:

    如果是将其事务开启与提交进行注释。在这不用重复进行事务操作。


  • 相关阅读:
    404. Sum of Left Leaves
    400. Nth Digit
    42. Trapping Rain Water
    154. Find Minimum in Rotated Sorted Array II
    [USACO4.2]草地排水Drainage Ditches
    [NOIP2010提高组]关押罪犯
    [洛谷P1580]yyy loves Easter_Egg I
    [洛谷P1144]最短路计数
    [洛谷P1346]电车
    [codevs1243]网络提速
  • 原文地址:https://www.cnblogs.com/hq-123/p/5839466.html
Copyright © 2020-2023  润新知