HibernateDaoSupport类的使用
核心提示:1、 继承了HibernateDaoSupport类的类获取session时,已不可用SessionFactory.OpenSessioon的形式来获取Session了,由于HibernateDaoSupport本身已有获取session的方法getSession(),所以直接用Session se=this.getSession();来获取, 2、 在依据hql获取用户
1        继承了HibernateDaoSupport类的类获取session时,已不可用SessionFactory.OpenSessioon的形式来获取Session了,由于HibernateDaoSupport本身已有获取session的方法getSession(),所以直接用Session se=this.getSession();来获取,
2        在依据hql获取用户信息时,继承了HibernateDaoSupport类的类中不能在使用Query类了,而是用List<Ssh> list = this.getHibernateTemplate().find(hql);形式来获取实体类集合
例:import java.util.List;       
1.      import org.hibernate.Query;      
2.      import org.hibernate.Session;      
3.      import org.hibernate.SessionFactory;      
4.      import org.springframework.context.ApplicationContext;   
5.      import org.springframework.context.support.ClassPathXmlApplicationContext; 
6.       import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
7.      import entity.Ssh;    
8.      public class SshDAO extends HibernateDaoSupport {      
9.      //  private SessionFactory sf = null;      
10.   //  public SessionFactory getSf() {      
11.   //     return sf;      
12.   //  }      
13.   //  public void setSf(SessionFactory sf) {
14.   //     this.sf = sf;      
15.   //  }   
16.   //  public String print(int id) {      
17.   //     Session se = sf.openSession();     
18.   //     String hql = "from Ssh where id=" + id;     
19.   //     Query q = se.createQuery("hql");      
20.   //     List<Ssh> list = q.list();      
21.   //     String a = list.get(1).getName();      
22.   //     return a;      
23.   //  }      
24.       public String print(int id) {      
25.          Session se =this.getSession();//获取Session对象      
26.          String hql = "from Ssh where id=" + id;      
27.          //依据hql获取实体集合,此处不要用Query类来实现      
28.          List<Ssh> list = this.getHibernateTemplate().find(hql);      
29.          String a = list.get(0).getName();      
30.          return a;      
31.       }      
32.       public static void main(String[] args) {      
33.          ApplicationContext ac=newClassPathXmlApplicationContext      
34.   ("spring/spring.xml");      
35.          SshDAO ssh=(SshDAO)ac.getBean("sshD");      
36.          System.out.println(ssh.print(1));      
37.       }      
38.   }
Spring.xml 文件篇: 
spring中配置继承了HibernateDaoSupport的类时此处的sessionFactory不能自定义! Sfspring中的SessionFacotryid
1.      <bean id="sshD" class="dao.SshDAO">     
2.             <property name="sessionFactory">     
3.                 <ref bean="sf" />     
4.             </property>     
5.          </bean>  
注意:此种情况适应于实体Dao类时系统自动生成时