日期:2014-05-16 浏览次数:20346 次
public class LocalSessionFactoryBeanTest { public static DataSource getDataSource(){ BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); ds.setUrl("jdbc:microsoft:sqlserver://192.168.0.100:1433;DatabaseName=CMCS3"); ds.setUsername("sa"); ds.setPassword("developer"); return (DataSource)ds; } public static SessionFactory getSessionFactory(DataSource ds) { LocalSessionFactoryBean factory = new LocalSessionFactoryBean(); Properties p = new Properties(); p.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect"); p.put("hibernate.show_sql", "true"); Properties p1 = new Properties(); ArrayList<String> list = new ArrayList<String>(); list.add("classpath*:com/sky/frame/domain/*.hbm.xml"); p1.put("mappingLocations", list); factory.setDataSource(ds); factory.setHibernateProperties(p); return (SessionFactory)factory.getObject(); } public static void main(String[] args){ DataSource ds = getDataSource(); SessionFactory sf = getSessionFactory(ds); Session session = sf.openSession(); System.out.println(session.toString()); session.close(); }