我们希望让所有的数据库使用HQL记录在Spring方面和Hibernate

问题描述:

Here i tried 2 write cod 2 get list from Mysql Database using spring nd Hibernate. 

但问题是,在这里,如何初始化**的org.hibernate.Session SE通过豆“的applicationContext.xml”文件类....我们希望让所有的数据库使用HQL记录在Spring方面和Hibernate

public void getList(**Session se**){ 
    String liststudent="from StudentList stud"; 
    Query q=se.createQuery(liststudent); 

    List<Object> list=q.list(); 
    for(Object obj:list){ 
     Object studarr[]=(Object[])obj; 
     System.out.println("Data at Zero Index"+studarr[0]); 
    } 
} 


As here property name **template** has been initialized by the ref template. 
Is there any way to initialize Session se. 


<bean name="mydao" class="dao.MyDao"> 
    <property name="template" ref="template"></property> 

</bean> 

使用context.xml文件

定义SessionFactory的

<bean id="sessionFactory" class="LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <!--define other properties...mapping files 
</bean> 

定义的HibernateTemplate

<bean id="template" class="*HibernateTemplate"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

然后

<bean name="mydao" class="dao.MyDao"> 
    <property name="template" ref="template"></property> 
</bean> 

注:类包已经ommitted