hibernate+struts2

一、环境

  添加struts2和hibernate的jar包。创建web.xml(配置struts的过滤器)、struts2.xml和hibernate.cfg.xml(设置一些数据库参数)。

二、创建自定义的sessionFactory类(类似之前的DBhelper)

  注意hibernate5.2.2的创建方式与之前的版本不同。

public class HibernateSessionFactory {
    
    private static SessionFactory sessionFactory;
    
    //private HibernateSessionFactory(){}
    
    public static SessionFactory getSessionFactory(){
        if(sessionFactory==null){
      //创建服务注册对象
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
      //创建会话工厂对象 sessionFactory
= new MetadataSources(registry).buildMetadata().buildSessionFactory(); return sessionFactory; }else { return sessionFactory; } }
原文地址:https://www.cnblogs.com/jasonlixuetao/p/5931457.html