• HibernateDaoSupport类的使用


    HibernateDaoSupport类的使用

    1、        继承了HibernateDaoSupport类的类获取session时,已不可用SessionFactory.OpenSessioon的形式来获 取Session了,由于HibernateDaoSupport本身已有获取session的方法getSession(),所以直接用Session se=this.getSession();来获取,

    2、        在依据hql获取用户信息时,继承了HibernateDaoSupport类的类中不能在使用Query类了,而是用List<Ssh> list = this.getHibernateTemplate().find(hql);形式来获取实体类集合

    Java类篇:

      1. import java.util.List;    
      2.   
      3. import org.hibernate.Query;   
      4.   
      5. import org.hibernate.Session;   
      6.   
      7. import org.hibernate.SessionFactory;   
      8.   
      9. import org.springframework.context.ApplicationContext;   
      10.   
      11. import org.springframework.context.support.ClassPathXmlApplicationContext;   
      12.   
      13. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
      14.   
      15. import entity.Ssh;    
      16.   
      17. public class SshDAO extends HibernateDaoSupport {   
      18.   
      19. //  private SessionFactory sf = null;   
      20.   
      21. //   
      22.   
      23. //  public SessionFactory getSf() {   
      24.   
      25. //     return sf;   
      26.   
      27. //  }   
      28.   
      29. //   
      30.   
      31. //  public void setSf(SessionFactory sf) {   
      32.   
      33. //     this.sf = sf;   
      34.   
      35. //  }   
      36.   
      37.     
      38.   
      39. //  public String print(int id) {   
      40.   
      41. //     Session se = sf.openSession();   
      42.   
      43. //     String hql = "from Ssh where id=" + id;   
      44.   
      45. //     Query q = se.createQuery("hql");   
      46.   
      47. //     List<Ssh> list = q.list();   
      48.   
      49. //     String a = list.get(1).getName();   
      50.   
      51. //     return a;   
      52.   
      53. //  }   
      54.   
      55.     public String print(int id) {   
      56.   
      57.        Session se =this.getSession();//获取Session对象   
      58.   
      59.        String hql = "from Ssh where id=" + id;   
      60.   
      61.        //依据hql获取实体集合,此处不要用Query类来实现   
      62.   
      63.        List<Ssh> list = this.getHibernateTemplate().find(hql);   
      64.   
      65.        String a = list.get(0).getName();   
      66.   
      67.        return a;   
      68.   
      69.     }   
      70.   
      71.     public static void main(String[] args) {   
      72.   
      73.        ApplicationContext ac=newClassPathXmlApplicationContext   
      74.   
      75. ("spring/spring.xml");   
      76.   
      77.        SshDAO ssh=(SshDAO)ac.getBean("sshD");   
      78.   
      79.        System.out.println(ssh.print(1));   
      80.   
      81.     }   
      82.   
      83. }   

    Spring.xml 文件篇: 

    在spring中配置继承了HibernateDaoSupport的类时此处的sessionFactory不能自定义! Sf为spring中的SessionFacotry的id

    1. <bean id="sshD" class="dao.SshDAO">  
    2.   
    3.        <property name="sessionFactory">  
    4.   
    5.            <ref bean="sf" />  
    6.   
    7.        </property>  
    8.   
    9.     </bean>  
    1. 注意:此种情况适应于实体Dao类时系统自动生成时

  • 相关阅读:
    Brocade FC Switch 光信号强度查看
    [Err]1418 This function has none of DETERMINISTIC,NO SQL,or R
    VBA 新手疑难杂症记录(不断更新中…)
    VBA 学习之旅(一) —— 数据类型
    ELO等级分制度
    Grunt上手指南<转>
    新开始新挑战
    html5大纲算法(目录树)
    隐居网V2.0
    长焦点图的解决方案(全兼容)
  • 原文地址:https://www.cnblogs.com/w1217/p/5500702.html
Copyright © 2020-2023  润新知