Hibernate opensession method: open a session
•
Java
Grammar 1
openSession()
Example
SessionFactory sessionFactory = config.buildSessionFactory(); Session session = sessionFactory.openSession();
Grammar 2
openSession(Connection connection,Interceptor interceptor)
Example
Connection conn = null; try{ Class.forName("com.microsoft.jdbc.sqlserver.sqlServerDriver"); //加载数据库驱动 String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_test Hibernate"; //定义连接数据库的url conn = DriverManager.getConnection(url,"sa",""); //获取数据库连接对象 }catch(Exception e){ e.printStackTrace(); } //MyIntereptor为自定义的实现Interceptor接口的拦截器 Interceptor it = new MyInterceptor(); Session session = sessionFactory.openSession(conn,it); //打开Session
Grammar 3
openSession(Interceptor interceptor)throws HibernateException
Example
SessionFactory sessionFactory = config.buildSessionFactory(); //MyIntereptor为自定义的实现Interceptor接口的拦截器 Interceptor it = new MyInterceptor(); Session session = sessionFactory.openSession(it);
Grammar 4
openSession(Connection connection)
Example
Connection conn = null; //定义参数Connection对象 try{ Class.forName("com.microsoft.jdbc.sqlserver.sqlServerDriver"); //加载数据库驱动 String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_testHibernate"; conn = DriverManager.getConnection(url,""); }catch(Exception e){ e.printStackTrace(); } Session session = sessionFactory.openSession(conn); //打开Session
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码