Hibernate与spring整合总结AbstractHibernateDAOImpl_第1页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、hibernate与spring整合总结abstracthibernatedaoi("操作实体:" + domainclass.getname(); return (t) gethibernatetemplate().get(domainclass.getname(),id); / 按照主键猎取实体并加锁。假如没有相应的实体,返回 null。 public t getwithlock(pk id, lockmode lock) t t = (t) gethibernatetemplate().get(domainclass.getname(), id,

2、 lock); if (t != null) this.flush(); / 立刻刷新,否则锁不会生效。 return t; / 按照主键猎取实体。假如没有相应的实体,抛出异样。 public t load(pk id) return (t) gethibernatetemplate().load(domainclass.getname(), id); / 按照主键猎取实体并加锁。假如没有相应的实体,抛出异样。 public t loadwithlock(pk id, lockmode lock) t t = (t) gethibernatetemplate().load(domainclas

3、s.getname(), id, lock); if (t != null) this.flush(); / 立刻刷新,否则锁不会生效。 return t; / 猎取所有实体。 public list t loadall() return (list t ) gethibernatetemplate().loadall(domainclass); / loadallwithlock() ? / 更新实体 public void update(t entity) throws exception ("操作实体:" + entity); gethibernate

4、template().update(entity); / 更新实体并加锁 public void updatewithlock(t entity, lockmode lock) gethibernatetemplate().update(entity, lock); this.flush(); / 立刻刷新,否则锁不会生效。 / 存储实体到数据库 public void save(t entity) throws exception gethibernatetemplate().save(entity); / savewithlock()? / 增强或更新实体 public void save

5、orupdate(t entity) gethibernatetemplate().saveorupdate(entity); / 增强或更新集合中的所有实体 public void saveorupdateall(collection t entities) gethibernatetemplate().saveorupdateall(entities); / 删除指定的实体 public void delete(t entity) gethibernatetemplate().delete(entity); / 加锁并删除指定的实体 public void deletewithlock(t

6、 entity, lockmode lock) gethibernatetemplate().delete(entity, lock); this.flush(); / 立刻刷新,否则锁不会生效。 / 按照主键删除指定实体 public void deletebykey(pk id) this.delete(this.load(id); / 按照主键加锁并删除指定的实体 public void deletebykeywithlock(pk id, lockmode lock) this.deletewithlock(this.load(id), lock); / 删除集合中的所有实体 publ

7、ic void deleteall(collection t entities) gethibernatetemplate().deleteall(entities); / - hsql - / 用法hsql语句挺直增强、更新、删除实体 public int bulkupdate(string querystring) return gethibernatetemplate().bulkupdate(querystring); / 用法带参数的hsql语句增强、更新、删除实体 public int bulkupdate(string querystring, object values) re

8、turn gethibernatetemplate().bulkupdate(querystring, values); / 用法hsql语句检索数据 public list find(string querystring) return gethibernatetemplate().find(querystring); / 用法带参数的hsql语句检索数据 public list t find(string querystring, object values) return gethibernatetemplate().find(querystring, values); / 用法带命名的

9、参数的hsql语句检索数据 public list findbynamedparam(string querystring, string paramnames, object values) return gethibernatetemplate().findbynamedparam(querystring, paramnames, values); / 用法命名的hsql语句检索数据 public list findbynamedquery(string queryname) return gethibernatetemplate().findbynamedquery(queryname)

10、; / 用法带参数的命名hsql语句检索数据 public list findbynamedquery(string queryname, object values) return gethibernatetemplate().findbynamedquery(queryname, values); / 用法带命名参数的命名hsql语句检索数据 public list findbynamedqueryandnamedparam(string queryname, string paramnames, object values) return gethibernatetemplate().f

11、indbynamedqueryandnamedparam(queryname, paramnames, values); / 用法hsql语句检索数据,返回 iterator public iterator iterate(string querystring) return gethibernatetemplate().iterate(querystring); / 用法带参数hsql语句检索数据,返回 iterator public iterator iterate(string querystring, object values) return gethibernatetemplate

12、().iterate(querystring, values); / 关闭检索返回的 iterator public void closeiterator(iterator it) gethibernatetemplate().closeiterator(it); / - criteria - / 创建与会话无关的检索标准 public detachedcriteria createdetachedcriteria() return detachedcriteria.forclass(domainclass); / 创建与会话绑定的检索标准 public criteria createcrit

13、eria() return this.createdetachedcriteria().getexeablecriteria(this.getsession(); / 检索满足标准的数据 public list findbycriteria(detachedcriteria criteria) return gethibernatetemplate().findbycriteria(criteria); / 检索满足标准的数据,返回指定范围的记录 public list findbycriteria(detachedcriteria criteria, int firstresult, int

14、 maxresults) return gethibernatetemplate().findbycriteria(criteria, firstresult, maxresults); / 用法指定的实体及属性检索(满足除主键外属性实体值)数据 public list t findequalbyentity(t entity, string propertynames) criteria criteria = this.createcriteria(); example exam = example.create(entity); exam.excludezeroes(); string d

15、efpropertys = getsessionfactory().getclassmetadata(domainclass.getname().getpropertynames(); for(string defproperty : defpropertys) int ii = 0; for(ii = 0; ii propertynames.length; +ii) if(defproperty.equals(propertynamesii) criteria.aorder(order.asc(defproperty); break; if(ii = propertynames.length

16、) exam.excludeproperty(defproperty); criteria.add(exam); return (list t ) criteria.list(); / 用法指定的实体及属性检索(满足属性 like 串实体值)数据 public list t findlikebyentity(t entity, string propertynames) criteria criteria = this.createcriteria(); for(string property : propertynames) try object value = propertyutils.

17、getproperty(entity, property); if(value instanceof string) criteria.add(restrictions.like(property, (string) value, matchmode.anywhere); criteria.addorder(order.asc(property); else criteria.add(restrictions.eq(property, value); criteria.addorder(order.asc(property); ch(exception ex) / 忽视无效的检索参考数据。 r

18、eturn (list t ) criteria.list(); / 用法指定的检索标准猎取满足标准的记录数 public integer getrowcount(detachedcriteria criteria) criteria.setprojection(projections.rowcount(); list list = this.findbycriteria(criteria, 0, 1); return (integer) list.get(0); / 用法指定的检索标准检索数据,返回指定统计值(max,min,avg,sum) public object getstatval

19、ue(detachedcriteria criteria, string propertyname, string statname) if (statname.tolowercase().equals("max") criteria.setprojection(projections.max(propertyname); else if(statname.tolowercase().equals("min") criteria.setprojection(projections.min(propertyname); else if(statname.t

20、olowercase().equals("avg") criteria.setprojection(projections.avg(propertyname); else if(statname.tolowercase().equals("sum") criteria.setprojection(projections.sum(propertyname); else return null; list list = this.findbycriteria(criteria, 0, 1); return list.get(0); / - others - / 加锁指定的实体 public void lock(t entity, lockmode lock) gethibernatetemplate().lock(entity, lock); / 强制初始化指定

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论