单例模式下的SessionFactory

xiaoxiao2021-02-28  10

需要在 hibernate.cfg.xml 中添加的配置

[java] view plain copy print ? <property name=“hibernate.current_session_context_class”>thread</property>   <property name="hibernate.current_session_context_class">thread</property>

创建类 HibernateUtil.java实现单例模式的SessionFactory。

[java] view plain copy print ? import org.hibernate.SessionFactory;    import org.hibernate.cfg.Configuration;        public class HibernateUtil {                    private static final Configuration config;            private static final SessionFactory factory;            static {  // 静态初始化块,加载配置信息              config = new Configuration().configure();  // 读取文件              factory = config.buildSessionFactory();            }            public static Session getSessionFactory(){                return factory;                            }           }       import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final Configuration config; private static final SessionFactory factory; static { // 静态初始化块,加载配置信息 config = new Configuration().configure(); // 读取文件 factory = config.buildSessionFactory(); } public static Session getSessionFactory(){ return factory; } }

实现了session对象只被创建一次,避免了多次加载sessionfactory的负担。

[java] view plain copy print ? import org.hibernate.Session;    import org.hibernate.Transaction;    import org.junit.Test;        public class Test {        @Test            public void select(){                  Session session = HibernateUtils.getSession();                  Transaction t = session.beginTransaction();                  }    }    import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; public class Test { @Test public void select(){ Session session = HibernateUtils.getSession(); Transaction t = session.beginTransaction(); } }

在测试类中去调用静态代码块类就可以进行操作了。

使用getCurrentSession()获得session

[java] view plain copy print ? import org.hibernate.Session;  import org.hibernate.SessionFactory;  import org.hibernate.Transaction;  import demo.entity.User;    public class Test {      public static void main(String[] args) {          SessionFactory factory = HibernateUtil.getSessionFactory();          //getCurrentSession()生成的Session不需要手动关闭,会在事务提交后自动关闭。          Session session = factory.getCurrentSession();          User user = new User();          user.setUsername(”qqqqq”);          user.setPassword(”qqqqq”);          user.setNickname(”name1”);          Transaction transaction = null;          try {              transaction = session.beginTransaction();              session.save(user);              //session.delete(user);//删除操作              //User tmp = session.find(User.class, 1);//查找操作              //session.update(user);//更新操作                     transaction.commit();          } catch (Exception e) {              if (transaction != null) {                  transaction.rollback();              }              e.printStackTrace();          }      }  }   import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import demo.entity.User; public class Test { public static void main(String[] args) { SessionFactory factory = HibernateUtil.getSessionFactory(); //getCurrentSession()生成的Session不需要手动关闭,会在事务提交后自动关闭。 Session session = factory.getCurrentSession(); User user = new User(); user.setUsername("qqqqq"); user.setPassword("qqqqq"); user.setNickname("name1"); Transaction transaction = null; try { transaction = session.beginTransaction(); session.save(user); //session.delete(user);//删除操作 //User tmp = session.find(User.class, 1);//查找操作 //session.update(user);//更新操作 transaction.commit(); } catch (Exception e) { if (transaction != null) { transaction.rollback(); } e.printStackTrace(); } } }
转载请注明原文地址: https://www.6miu.com/read-1600163.html

最新回复(0)