- 浏览: 297625 次
- 性别:
- 来自: 东京
文章分类
最新评论
-
80后的童年2:
企业级分布式搜索平台Solr视频教程网盘地址:https:// ...
企业级搜索引擎Solr使用入门指南 -
springdata_spring:
apache lucene开源框架demo使用实例教程源代码下 ...
Lucene / Solr 开发经验 -
springdata-jpa:
java web开发分页demo源代码下载:http://ww ...
简易java分页标签 -
zjf_sdnu:
兄弟,script写错了
jqGrid初学备注 -
85600367:
你好,请教一个问题。当进行分布式查询时solr无法查询到Luc ...
Lucene / Solr 开发经验
1,假设存在Thesis对象:
package org.cms.po; import java.util.Date; import java.util.HashSet; import java.util.Set; /** * Thesis generated by MyEclipse Persistence Tools */ public class Thesis implements java.io.Serializable { // Fields private Integer thesisid; private Project project; private User userByReader; private Type type; private User userByUploader; private String name; private String author; private String source; private Date publishdate; private Date uploadtime; private String summary; private Integer downloadtimes; private Integer status; private String remark; private String attachmentlink; private Integer responseamount; private Set responses = new HashSet(0); private Set attachments = new HashSet(0); // Constructors /** default constructor */ public Thesis() {} /** minimal constructor */ public Thesis(Integer thesisid, Project project, Type type, String name, String attachmentlink, Date uploadtime, Integer status) { this.thesisid = thesisid; this.project = project; this.type = type; this.name = name; this.uploadtime = uploadtime; this.status = status; this.attachmentlink = attachmentlink; } /** full constructor */ public Thesis(Integer thesisid, Project project, User userByReader, Type type, User userByUploader, String name, String author, String source, Date publishdate, String attachmentlink, Date uploadtime, String summary, Integer downloadtimes, Integer status, Integer responseamount, String remark, Set responses, Set attachments) { this.thesisid = thesisid; this.project = project; this.userByReader = userByReader; this.type = type; this.userByUploader = userByUploader; this.name = name; this.author = author; this.source = source; this.publishdate = publishdate; this.uploadtime = uploadtime; this.summary = summary; this.downloadtimes = downloadtimes; this.status = status; this.remark = remark; this.attachmentlink = attachmentlink; this.responseamount = responseamount; this.responses = responses; this.attachments = attachments; } // Property accessors public Integer getThesisid() { return this.thesisid; } public void setThesisid(Integer thesisid) { this.thesisid = thesisid; } public Project getProject() { return this.project; } public void setProject(Project project) { this.project = project; } ...... }
2,其映射文件如下:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="org.cms.po.Thesis" table="thesis" catalog="cms"> <id name="thesisid" type="java.lang.Integer"> <column name="THESISID" /> <generator class="native" /> </id> <many-to-one name="project" class="org.cms.po.Project" fetch="select" lazy="false"> <column name="PROJECTID" not-null="true"/> </many-to-one> <many-to-one name="userByReader" class="org.cms.po.User" fetch="select" lazy="false"> <column name="READER"/> </many-to-one> <many-to-one name="type" class="org.cms.po.Type" fetch="select" lazy="false"> <column name="TYPE" not-null="true"/> </many-to-one> <many-to-one name="userByUploader" class="org.cms.po.User" fetch="select" lazy="false"> <column name="UPLOADER"/> </many-to-one> <property name="name" type="java.lang.String"> <column name="NAME" length="100" not-null="true" /> </property> <property name="author" type="java.lang.String"> <column name="AUTHOR" length="100"/> </property> <property name="source" type="java.lang.String"> <column name="SOURCE" length="100"/> </property> <property name="publishdate" type="java.util.Date"> <column name="PUBLISHDATE" length="0"/> </property> <property name="uploadtime" type="java.util.Date"> <column name="UPLOADTIME" length="0" not-null="true"/> </property> <property name="summary" type="java.lang.String"> <column name="SUMMARY" length="65535"/> </property> <property name="downloadtimes" type="java.lang.Integer"> <column name="DOWNLOADTIMES"/> </property> <property name="responseamount" type="java.lang.Integer"> <column name="RESPONSEAMOUNT" /> </property> <property name="status" type="java.lang.Integer"> <column name="STATUS" not-null="true"/> </property> <property name="remark" type="java.lang.String"> <column name="REMARK" length="65535" /> </property> <property name="attachmentlink" type="java.lang.String"> <column name="ATTACHMENTLINK" length="20" not-null="true"/> </property> <set name="responses" inverse="true"> <key> <column name="THESISID" not-null="true"/> </key> <one-to-many class="org.cms.po.Response" /> </set> <set name="attachments" inverse="true"> <key> <column name="THESISID" not-null="true" /> </key> <one-to-many class="org.cms.po.Attachment" /> </set> </class> </hibernate-mapping>
3,部分属性进行模糊查询,生成DetachedCriteria查询对象:
public List<Thesis> queryThesis(Map queryInfo) throws ExceptionHandle { try { DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Thesis.class, "t"); if (queryInfo.get("name") != null) { detachedCriteria.add(Restrictions.like("t.name", "%" + queryInfo.get("name") + "%")); } if (queryInfo.get("typeid") != null && !queryInfo.get("typeid").toString().equals("0")) { detachedCriteria.createAlias("type", "tp").add(Restrictions.eq("tp.typeid", Integer.parseInt(queryInfo.get("typeid").toString()))); } if (queryInfo.get("projectid") != null && !queryInfo.get("projectid").toString().equals("0")) { detachedCriteria.createAlias("project", "p").add(Restrictions.eq("p.projectid", Integer.parseInt(queryInfo.get("projectid").toString()))); } if (queryInfo.get("teacherid") != null && !queryInfo.get("teacherid").toString().equals("0")) { detachedCriteria.createAlias("userByUploader", "u").add( Restrictions.eq("u.userid", Integer.parseInt(queryInfo.get("teacherid").toString()))); } if (queryInfo.get("author") != null) { detachedCriteria.add(Restrictions.like("t.author", "%" + queryInfo.get("author") + "%")); } if (queryInfo.get("source") != null) { detachedCriteria.add(Restrictions.like("t.source", "%" + queryInfo.get("source") + "%")); } if (queryInfo.get("dateBegin") != null && queryInfo.get("dateEnd") != null) { detachedCriteria.add(Restrictions.between("t.uploadtime", (Date) queryInfo.get("dateBegin"), (Date) queryInfo.get("dateEnd"))); } List list = getDaoManager().getThesisDao().findByCriteria(detachedCriteria); if (list != null && list.size() != 0) { return list; } } catch (Exception e) { throw new ExceptionHandle(e, "查询文献异常"); } return null; }
4,DAO部分:
public List findByCriteria(final DetachedCriteria detachedCriteria) { return (List) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Criteria criteria = detachedCriteria.getExecutableCriteria(session); return criteria.list(); } }); }
发表评论
-
Struts2 Jsonplugin 详解
2009-07-08 21:41 7099本文转自: http://www.blogjava.net/f ... -
一点struts json备注
2009-07-05 00:30 1534下载struts json插件:jsonplugin-0.33 ... -
Hibernate annotation简单配置
2009-06-29 00:04 1656<?xml version="1.0" ... -
Liferay下整合Spring和Hibernate开发Plugin初试
2009-05-26 00:41 4895简单的说,完全不知道Liferay下应该如何做开发。所以完全凭 ... -
Liferay plugin开发入门
2009-05-19 00:10 2724本篇部分引用以下文章: http://daoger.itey ... -
Hibernate通用DAO
2009-01-02 17:46 3625/** * @author Clay Zhong, zjcl ... -
Acegi获取用户信息
2008-12-04 23:38 2856import org.acegisecurity.Authen ... -
Acegi详解
2008-11-16 21:15 1767本文转自:http://www.yuanma.org/data ... -
velocity.properties source code
2008-11-04 22:37 3003# Licensed to the Apache Softwa ... -
lucene常用搜索排序摘录
2008-09-16 23:38 3144一,查询 一个关键字,对一个字段进行查询 Query ... -
Solr中SimplePostTool的修改
2008-09-15 22:38 2985将源代码修改为传递字符串,去掉files以及stdin模式。 ... -
Struts2,Spring2,Hibernate3整合
2008-09-15 11:44 33791. web.xml <?xml version=&qu ... -
企业级搜索引擎Solr使用入门指南
2008-09-10 22:43 16680本文转自 http://chuanliang2 ... -
Lucene / Solr 开发经验
2008-09-10 22:12 20032转自 Lucene/Solr开发经验[原] 作者 张驰有道 ... -
Lucene初学备注
2008-09-07 19:51 12631,检索文件目录,创建index import java.io ... -
JR各Ajax框架基础教程收集
2008-08-24 12:19 1212JQuery: http://www.javaresearch ... -
Spring 2.0 中配置 JPA
2008-04-20 16:57 3894翻译:SpringSide团队 转载请注明出处。 本文提供了 ... -
Hibernate延迟加载
2008-02-09 16:22 2737延迟初始化错误是运用Hibernate开发项目时最常见的错误。 ... -
Properties资源文件编码
2008-01-26 17:26 15171、展开Eclipse的Help菜单,将鼠标移到Softwar ... -
struts+hibernate分页
2007-12-17 20:53 1726新建表 sql 代码 DROP DATABAS ...
相关推荐
对于Nhibernate不直接支持的模糊查询,可以编写自定义的HQL(Hibernate Query Language)来实现。 总结,Nhibernate的模糊查询提供了灵活的API,可方便地在C#代码中完成SQL的`LIKE`操作。然而,使用时要注意性能...
Criteria用于在线查询,直接与Hibernate Session关联,而DetachedCriteria则可在不依赖Session的情况下创建,适用于离线或延迟执行的查询。DetachedCriteria可以通过`forClass(Class)`或`forEntityName(String)`静态...
通过Example.create()方法,你可以忽略大小写,启用模糊匹配等,这在处理基于对象属性的查询时非常有用。 Projection接口用于查询结果的投影,即指定查询返回的字段。SimpleProjection如avg(), count(), max(), min...
你可以忽略大小写,设置模糊匹配等,使得查询更加灵活。 **Projection** 是用于报表查询和聚合操作的接口。它定义了如何从数据库中提取数据,例如平均值(avg)、计数(count)、最大值(max)、最小值(min)和...
此外,`Example`类允许根据一个对象实例创建查询条件,这对于基于对象属性的模糊匹配非常有用。 `Projection`则用于构建报表查询,它可以获取查询结果的聚合信息,如平均值、计数、最大值、最小值或求和。`...
Hibernate Criteria API 是 Hibernate 框架中一个强大的查询工具,它提供了一种面向对象的方式来构建SQL查询,使得开发者无需直接编写SQL语句,即可进行复杂的数据库操作。Criteria API 提供了高度抽象的接口,可以...
本文将详细介绍Hibernate中的Criteria API及其子类DetachedCriteria的使用方法,以及如何利用它们进行高效的数据查询。 #### 二、Criteria与DetachedCriteria ##### 1. Criteria - **定义**:`Criteria` 是...
Hibernate 3引入了`DetachedCriteria`,它允许你在不直接涉及Session的情况下构建查询条件。这有助于分离业务逻辑和数据访问层,使代码更易管理。创建DetachedCriteria的示例如下: ```java DetachedCriteria ...
而在Hibernate提供的多种查询方式中,Criteria API是一种非常灵活且强大的查询工具,它支持复杂的条件组合以及分组、排序等功能,为开发者提供了更为方便的查询手段。 #### 二、Criteria简介 Criteria是Hibernate...
2. **限制(Restrictions)**:这是添加查询条件的方式,例如等值比较、范围比较、模糊查询等。例如,查找名字为"John"的用户: ```java criteria.add(Restrictions.eq("name", "John")); ``` 3. **排序(Order...
通过深入探讨Hibernate的Criteria API和`Restrictions`的使用,我们不仅掌握了如何构建复杂查询的能力,还了解了如何利用`DetachedCriteria`提高代码的可维护性和灵活性。这些技术在日常开发中极为实用,能够显著...
### Criteria与Hibernate模糊查询 在Hibernate框架中,`Criteria`接口是执行动态SQL查询的关键工具之一,它允许开发者以编程的方式构建复杂的查询条件,而无需直接编写SQL语句。这在处理复杂的数据检索需求时尤为...
在基于Spring和Hibernate的项目开发中,Criteria API提供了灵活且强大的查询功能,使开发者能够轻松构建复杂的查询条件,从而提高数据检索的效率与精确度。本文将深入探讨Hibernate中Criteria API的全面应用,包括其...