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. import org.hibernate.Query;
    3. import org.hibernate.Session;
    4. import org.hibernate.SessionFactory;
    5. import org.springframework.context.ApplicationContext;
    6. import org.springframework.context.support.ClassPathXmlApplicationContext;
    7. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    8. import entity.Ssh;
    9. public class SshDAO extends HibernateDaoSupport {
    10. //  private SessionFactory sf = null;
    11. //
    12. //  public SessionFactory getSf() {
    13. //     return sf;
    14. //  }
    15. //
    16. //  public void setSf(SessionFactory sf) {
    17. //     this.sf = sf;
    18. //  }
    19. //  public String print(int id) {
    20. //     Session se = sf.openSession();
    21. //     String hql = "from Ssh where id=" + id;
    22. //     Query q = se.createQuery("hql");
    23. //     List<Ssh> list = q.list();
    24. //     String a = list.get(1).getName();
    25. //     return a;
    26. //  }
    27. public String print(int id) {
    28. Session se =this.getSession();//获取Session对象
    29. String hql = "from Ssh where id=" + id;
    30. //依据hql获取实体集合,此处不要用Query类来实现
    31. List<Ssh> list = this.getHibernateTemplate().find(hql);
    32. String a = list.get(0).getName();
    33. return a;
    34. }
    35. public static void main(String[] args) {
    36. ApplicationContext ac=newClassPathXmlApplicationContext
    37. ("spring/spring.xml");
    38. SshDAO ssh=(SshDAO)ac.getBean("sshD");
    39. System.out.println(ssh.print(1));
    40. }
    41. }

Spring.xml 文件篇:

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

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

HibernateDaoSupport类的使用的更多相关文章

  1. HibernateDaoSupport类的使用(转)

    看到一篇很好描述HibernateDaoSupport类使用的例子,特此在这和大家分享一下 核心提示:1. 继承了HibernateDaoSupport类的类获取session时,已不可用Sessio ...

  2. HibernateDaoSupport类的底层中hql操作使用

    spring的ApplicationContex.xml 中配置 sql 查询方法: 加载数据源的两种方式: <!--方式一:使用 c3p0 连接池 加载数据源 --> <bean ...

  3. HibernateTemplate和HibernateDaoSupport(spring注入问题)

    HibernateTemplate HibernateTemplate是spring提供的一个就hibernate访问持久层技术而言.支持Dao组件的一个工具.HibernateTemplate提供持 ...

  4. SessionFactory、HibernateTemplate、HibernateDaoSupport之间的关系说明

    在接触HibernateTemplate之前,我们知道,在对数据库进行CRUD操作之前,需要开启session.transaction等等.在hibernate学习过程中,我们知道了,得到sessio ...

  5. HibernateDaoSupport和HibernateTemplate

    hibernateTemplate的常用方法: Ø  void delete(Object entity):删除指定持久化实例 Ø  deleteAll(Collection entities):删除 ...

  6. HibernateTemplate、HibernateDaoSupport两种方法实现增删改查Good(转)

    Spring+Hibernate两种方法实现增删改查 首先,定义一个Customer的bean类,设置好Customer.hbm.xml文件.再定义好一个Dao接口.准备好一个jdbc.propert ...

  7. HibernateTemplate和HibernateDaoSupport

    Spring整合Hibernate后,为Hibernate的DAO提供了两个工具类:HibernateTemplate和HibernateDaoSupport HibernateTemplateHib ...

  8. spring-framework-3.2.4与hibernate-release-4.3.5下使用HibernateDaoSupport抛出异常

    spring-framework-3.2.4与hibernate-release-4.3.5下使用HibernateDaoSupport抛出异常java.lang.ClassCastException ...

  9. HibernateDaoSupport与JdbcDaoSupport

    Dao 的支持类可以有好多,如: JdbcDaoSupport , HibernateDaoSupport ,JdoDaoSupport等,下面对最常用的HibernateDaoSupport与Jdb ...

随机推荐

  1. INI 文件的读写操作

    在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static r ...

  2. jquery源码学习之extend

    jquery的extend方法现项目中经常使用,现在了解一下它的实现. 说起extend就要先了解一个jQuery的$.extend和$.fn.extend作用及区别 jQuery为开发插件提拱了两个 ...

  3. java自带BASE64工具进行图片和字符串转换

    java自带BASE64工具进行图片和字符串转换 import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...

  4. (转)MVC,MVP 和 MVVM 的图示

    作者: 阮一峰 日期: 2015年2月 1日 复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身 ...

  5. collectionView使用细节

    1.//创建组头组尾一个方法 - (UICollectionReusableView *)stCollectionView:(UICollectionView *)collectionView vie ...

  6. JAVA中implements的用法

    implements使用 interface Sport { void run(); void jump(); } class Athlete implements Sport { public vo ...

  7. 对需要聚类的数据使用canopy做初步的计算

    K值聚类的时候,需要自己指定cluster的数目. 这个cluster数目一般是通过canopy算法进行预处理来确定的. canopy具体描述可以参考这里. 下面是 golang语言的一个实现(对经纬 ...

  8. .net下各个数据类型所占用的字节

    Console.WriteLine(sizeof(int)); Console.WriteLine(sizeof(short)); Console.WriteLine(sizeof(char)); C ...

  9. Sublime 保存时自动转换tab成空格

    笔者最近学习c, c语言的清新代码风格让人眼前一亮,不禁爱上这种写作风格,变量名.等号.常量值之间空格分隔,清爽便于阅读. 于是笔者以此为代码写作规范,查阅自己以前写的java代码,以下用notepa ...

  10. Python:迭代器

    迭代器:可以被next()函数调用并不断返回下一个值的对象称为迭代器. 可迭代对象:可以直接作用于for循环的对象. 基本方法:iter()和next() 迭代器创建: 例1: list = ['a' ...