openSession()
SessionFactory sessionFactory = config.buildSessionFactory(); Session session = sessionFactory.openSession();
openSession(Connection connection,Interceptor interceptor)
引數說明: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
openSession(Interceptor interceptor)throws HibernateException
引數說明:SessionFactory sessionFactory = config.buildSessionFactory(); //MyIntereptor為自定義的實現Interceptor介面的攔截器 Interceptor it = new MyInterceptor(); Session session = sessionFactory.openSession(it);
openSession(Connection connection)
引數說明: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,"sa",""); }catch(Exception e){ e.printStackTrace(); } Session session = sessionFactory.openSession(conn); //開啟Session