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类篇:
- import java.util.List;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import entity.Ssh;
- public class SshDAO extends HibernateDaoSupport {
- // private SessionFactory sf = null;
- //
- // public SessionFactory getSf() {
- // return sf;
- // }
- //
- // public void setSf(SessionFactory sf) {
- // this.sf = sf;
- // }
- // public String print(int id) {
- // Session se = sf.openSession();
- // String hql = "from Ssh where id=" + id;
- // Query q = se.createQuery("hql");
- // List<Ssh> list = q.list();
- // String a = list.get(1).getName();
- // return a;
- // }
- public String print(int id) {
- Session se =this.getSession();//获取Session对象
- String hql = "from Ssh where id=" + id;
- //依据hql获取实体集合,此处不要用Query类来实现
- List<Ssh> list = this.getHibernateTemplate().find(hql);
- String a = list.get(0).getName();
- return a;
- }
- public static void main(String[] args) {
- ApplicationContext ac=newClassPathXmlApplicationContext
- ("spring/spring.xml");
- SshDAO ssh=(SshDAO)ac.getBean("sshD");
- System.out.println(ssh.print(1));
- }
- }
Spring.xml 文件篇:
在spring中配置继承了HibernateDaoSupport的类时此处的sessionFactory不能自定义! Sf为spring中的SessionFacotry的id
- <bean id="sshD" class="dao.SshDAO">
- <property name="sessionFactory">
- <ref bean="sf" />
- </property>
- </bean>
- 注意:此种情况适应于实体Dao类时系统自动生成时
HibernateDaoSupport类的使用的更多相关文章
- HibernateDaoSupport类的使用(转)
看到一篇很好描述HibernateDaoSupport类使用的例子,特此在这和大家分享一下 核心提示:1. 继承了HibernateDaoSupport类的类获取session时,已不可用Sessio ...
- HibernateDaoSupport类的底层中hql操作使用
spring的ApplicationContex.xml 中配置 sql 查询方法: 加载数据源的两种方式: <!--方式一:使用 c3p0 连接池 加载数据源 --> <bean ...
- HibernateTemplate和HibernateDaoSupport(spring注入问题)
HibernateTemplate HibernateTemplate是spring提供的一个就hibernate访问持久层技术而言.支持Dao组件的一个工具.HibernateTemplate提供持 ...
- SessionFactory、HibernateTemplate、HibernateDaoSupport之间的关系说明
在接触HibernateTemplate之前,我们知道,在对数据库进行CRUD操作之前,需要开启session.transaction等等.在hibernate学习过程中,我们知道了,得到sessio ...
- HibernateDaoSupport和HibernateTemplate
hibernateTemplate的常用方法: Ø void delete(Object entity):删除指定持久化实例 Ø deleteAll(Collection entities):删除 ...
- HibernateTemplate、HibernateDaoSupport两种方法实现增删改查Good(转)
Spring+Hibernate两种方法实现增删改查 首先,定义一个Customer的bean类,设置好Customer.hbm.xml文件.再定义好一个Dao接口.准备好一个jdbc.propert ...
- HibernateTemplate和HibernateDaoSupport
Spring整合Hibernate后,为Hibernate的DAO提供了两个工具类:HibernateTemplate和HibernateDaoSupport HibernateTemplateHib ...
- 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 ...
- HibernateDaoSupport与JdbcDaoSupport
Dao 的支持类可以有好多,如: JdbcDaoSupport , HibernateDaoSupport ,JdoDaoSupport等,下面对最常用的HibernateDaoSupport与Jdb ...
随机推荐
- INI 文件的读写操作
在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static r ...
- jquery源码学习之extend
jquery的extend方法现项目中经常使用,现在了解一下它的实现. 说起extend就要先了解一个jQuery的$.extend和$.fn.extend作用及区别 jQuery为开发插件提拱了两个 ...
- java自带BASE64工具进行图片和字符串转换
java自带BASE64工具进行图片和字符串转换 import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...
- (转)MVC,MVP 和 MVVM 的图示
作者: 阮一峰 日期: 2015年2月 1日 复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身 ...
- collectionView使用细节
1.//创建组头组尾一个方法 - (UICollectionReusableView *)stCollectionView:(UICollectionView *)collectionView vie ...
- JAVA中implements的用法
implements使用 interface Sport { void run(); void jump(); } class Athlete implements Sport { public vo ...
- 对需要聚类的数据使用canopy做初步的计算
K值聚类的时候,需要自己指定cluster的数目. 这个cluster数目一般是通过canopy算法进行预处理来确定的. canopy具体描述可以参考这里. 下面是 golang语言的一个实现(对经纬 ...
- .net下各个数据类型所占用的字节
Console.WriteLine(sizeof(int)); Console.WriteLine(sizeof(short)); Console.WriteLine(sizeof(char)); C ...
- Sublime 保存时自动转换tab成空格
笔者最近学习c, c语言的清新代码风格让人眼前一亮,不禁爱上这种写作风格,变量名.等号.常量值之间空格分隔,清爽便于阅读. 于是笔者以此为代码写作规范,查阅自己以前写的java代码,以下用notepa ...
- Python:迭代器
迭代器:可以被next()函数调用并不断返回下一个值的对象称为迭代器. 可迭代对象:可以直接作用于for循环的对象. 基本方法:iter()和next() 迭代器创建: 例1: list = ['a' ...