7.7--SSH学习之Hibernate Session

the first picture:


这里写图片描述

the second picture:


这里写图片描述


首先来两张图

第一张图:大概意思是one.jsp把数据提交到servlet,(这里已开启一个新的事务,也是一个新的session),然后调用servive层,再调用dao层,最后在数据库查询数据,然后把数据返回到dao层,service层,servlet,最后在two.jsp中显示查询结果。在servlet向two.jsp提交查询结果之前,先解析jsp,然后提交事务,这时session也就关闭了。
总结:一次请求开启一个新的session


第二张图:jsp页面向servlet,service,dao层提交请求,要先经过过滤器,最后到数据库;返回数据也是一样,最后也要经过过滤器才能在jsp页面中显示查询结果


举个例子:


HibernateUtil .java

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration cfg = new  Configuration().configure();
            SessionFactory sf= cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
                                .applySettings(cfg.getProperties()).build());
            return sf;
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static Session getSession(){
        Session session = threadLocal.get();
        if(session == null || !session.isOpen()){   //判断session是否为空,或者没打开
            session = getSessionFactory().getCurrentSession();
            threadLocal.set(session);
        }
        return session;
    }


}



GenericDaoImpl .java//相当于baseDaoImpl,接口的 父类

public class GenericDaoImpl implements GenericDao {

    private Class genericEntity;


    public Class getGenericEntity() {
        return genericEntity;
    }

    public void setGenericEntity(Class genericEntity) {
        this.genericEntity = genericEntity;
    }

    @Override
    public void addEntity(Object obj) {
        // TODO Auto-generated method stub
        try{
            Session session = HibernateUtil.getSession();
            //session.beginTransaction();

            session.save(obj);

            //session.getTransaction().commit();

        }catch(Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void deleteEntity(Object obj) {
        // TODO Auto-generated method stub
        try{
            Session session = HibernateUtil.getSession();
            //session.beginTransaction();

            session.delete(obj);

            //session.getTransaction().commit();

        }catch(Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void updateEntity(Object obj) {
        // TODO Auto-generated method stub
        try{
            Session session = HibernateUtil.getSession();
            //session.beginTransaction();

            session.update(obj);

            //session.getTransaction().commit();

        }catch(Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public Object findById(Serializable entityId) {
        // TODO Auto-generated method stub
        Object obj = null;
        try{
            Session session = HibernateUtil.getSession();
            //session.beginTransaction();

            obj = session.load(genericEntity, entityId);

            //session.getTransaction().commit();

        }catch(Exception e){
            e.printStackTrace();
        }

        return obj;
    }

    @Override
    public List findAll() {
        // TODO Auto-generated method stub
        List list = new ArrayList();
        try{
            Session session = HibernateUtil.getSession();
            //session.beginTransaction();

            list = session.createQuery("from "+genericEntity.getName()).list();

            //session.getTransaction().commit();

        }catch(Exception e){
            e.printStackTrace();
        }

        return list;
    }

}



过滤器OpenSessionInViewFilter .java

public class OpenSessionInViewFilter implements Filter {

    @Override
    public void destroy() {

    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        System.out.println("客户端请求服务器执行代码");
        Session session = HibernateUtil.getSession();
        session.beginTransaction();
        chain.doFilter(request,response);
        System.out.println("服务器响应客户器执行代码");
        session.getTransaction().commit();
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}



运行程序后台显示信息为:

这里写图片描述

可见请求页面先经过过滤器,最后返回结果集时也要经过过滤器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ssy03092919

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值