67,513
社区成员
发帖
与我相关
我的任务
分享
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
baseDao=(BaseDao) context.getBean("baseDao");
}
@Test
public void testGetListMapBySql() throws SQLException {
String sql="select * from t_user";
List<Map<String,Object>> ulist=baseDao.getListMapBySql(sql, null, 0, 0);
for (Map<String, Object> map : ulist) {
for(String k:map.keySet()){
System.out.println("key:"+k+" value:"+map.get(k));
}
}
}
public Connection getConnection() throws SQLException {
Connection conn = null;
DataSource dataSource=SessionFactoryUtils.getDataSource(getSessionFactory());
conn = dataSource.getConnection();
return conn;
}
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="baseDao" class="com.share.dao.BaseDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
/* */ public static DataSource getDataSource(SessionFactory sessionFactory)
/* */ {
/* 123 */ if (sessionFactory instanceof SessionFactoryImplementor) {
/* 124 */ ConnectionProvider cp = ((SessionFactoryImplementor)sessionFactory).getConnectionProvider();
/* 125 */ if (cp instanceof LocalDataSourceConnectionProvider) {
/* 126 */ return ((LocalDataSourceConnectionProvider)cp).getDataSource();
/* */ }
/* */ }
/* 129 */ return null;
/* */ }
通过上面的这个方法后得到的DataSource就为空了。
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/myshare</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">2</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<!-- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="connection.pool_size">2</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>-->
<mapping resource="com/share/entity/User.hbm.xml"/>
</session-factory>
这个getSessionFactory()是hibernateDaoSupport中的,在启动的时候已经把sessionfactory注入到了hibernateDaoSupport中,
/* */ public final SessionFactory getSessionFactory()
/* */ {
/* 89 */ return ((this.hibernateTemplate != null) ? this.hibernateTemplate.getSessionFactory() : null);
/* */ }