hibernate-search-5.1.1简易使用
系统要求java7和hibernate-core-4.3.8,此外还依赖如下jar包

使用demo如下:
package com.ciaos.controller; import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.hibernate.Transaction;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.ciaos.dao.Account;
import com.ciaos.dao.HibernateSessionFactory; @Controller
public class SearchController { //索引
@RequestMapping(value="/index", method={RequestMethod.GET})
public void index(HttpServletRequest req, HttpServletResponse resp){
try {
FullTextSession fullTextSession = Search.getFullTextSession(HibernateSessionFactory.getSession());
try {
fullTextSession.createIndexer().startAndWait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} PrintWriter out = resp.getWriter();
out.print("Done");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //搜索
@RequestMapping(value="/search", method={RequestMethod.POST,RequestMethod.GET})
public void search(HttpServletRequest req, HttpServletResponse resp){
try {
String keyword = req.getParameter("keyword");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html; charset=utf-8");
PrintWriter out = resp.getWriter(); FullTextSession fullTextSession = Search.getFullTextSession(HibernateSessionFactory.getSession());
Transaction tx = fullTextSession.beginTransaction(); // create native Lucene query using the query DSL
// alternatively you can write the Lucene query using the Lucene query parser
// or the Lucene programmatic API. The Hibernate Search DSL is recommended though
QueryBuilder qb = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity(Account.class).get();
org.apache.lucene.search.Query query = qb
.keyword()
.onFields("name")
.matching(keyword)
.createQuery(); // wrap Lucene query in a org.hibernate.Query
org.hibernate.Query hibQuery =
fullTextSession.createFullTextQuery(query, Account.class); // execute search
List result = hibQuery.list(); tx.commit(); System.out.println(keyword);
for(int i = 0;i < result.size();i++){
Account account = (Account) result.get(i);
out.print("Result " + keyword + " " + account.getName());
}
out.print("Search Done");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
POJO文件增加Field注解如下
package com.ciaos.dao; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id;
import javax.persistence.Table; import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store; /**
* Account entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "account", catalog = "test")
@Indexed(index="account")
public class Account implements java.io.Serializable { // Fields private Integer id; private String name; // Constructors /** default constructor */
public Account() {
} /** full constructor */
public Account(String name) {
this.name = name;
} // Property accessors
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} @Column(name = "name")
@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
}
配置文件
applicationContext.xml(hibernate4放弃CacheProvider,所以不能用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean)
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
</beans>
hibernate.cfg.xml,property增加如下配置
<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.impl.FSDirectoryProvider</property>
<property name="hibernate.search.default.indexBase">e:\test</property>
hibernate-search-5.1.1简易使用的更多相关文章
- hibernate search例子
		[TOC] 1. 概念介绍 1.1. Hibernate Search Hibernate Search是Hibernate的子项目,把数据库全文检索能力引入到项目中,并通过"透明" ... 
- S2SH+Hibernate search出现的问题
		一 java.lang.NoSuchMethodError: org.hibernate.engine.transaction.spi.TransactionEnvironment.getJtaPl ... 
- Hibernate search使用示例(基础小结-注解方式)
		(对于项目环境配置,一直没怎么看过.这次经历里从基础环境搭建到hibernate search示例的完成) 1.首先创建project,选择了web project. 2.导入hibernate se ... 
- [SpringBoot系列]--Spring Hibernate search 注解实现(未测试)
		1.maven项目pom.xml加入依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId& ... 
- Hibernate search与Lucene包异常学习心得
		最近使用了了一下Hibernate Search这个组件 这个组件是对域模型进行全文检索,在全文检索的底层实现上使用了Lucene技术 在进行小测试的时候费了很大的力气去搞定包的问题 我直接通过实例 ... 
- [Hibernate Search] (3) 基础查询
		基础查询 眼下我们仅仅用到了基于keyword的查询,实际上Hibenrate Search DSL还提供了其他的查询方式,以下我们就来一探到底. 映射API和查询API 对于映射API.我们能够通过 ... 
- Hibernate Search集与lucene分词查询
		lucene分词查询参考信息:https://blog.csdn.net/dm_vincent/article/details/40707857 
- Hibernate和IBatis对比
		[转自]http://blog.csdn.net/ya2dan/article/details/7396598 项目也做过几个, 使用IBatis就做一个项目, 基本上都是使用Hibernate, 也 ... 
- 备忘:hibernate, logback, slf4j实际应用一例
		用hibernate写一些简单的数据库的Java应用.主要是温习一下.之前弄过的一些都忘了.发现还是得记下来,不然很快就忘. 1. Eclipse版本,用Juno, J2EE版本.最好下载zip版本的 ... 
- Hibernate正向工程(实体类-->数据库)
		1,新建实体类News.java package com.hanqi.dao; import java.util.Date; public class News { private Integer i ... 
随机推荐
- Centos for php+mysql+apache
			一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终端,输入: [root@localhost ~]# yum install mysql mysql-server 安装完毕,让 MySQ ... 
- php 异步处理的gearman
			1. php 是进程处理,单线程到的,没有异步机制,在一些处理花费时间较多的情况导致用户体验较差.可以使用gearman 进行异步处理. 2. gearman 是一个异步处理的socket架构. 需要 ... 
- ETL中的数据增量抽取机制
			ETL中的数据增量抽取机制 ( 增量抽取是数据仓库ETL(extraction,transformation,loading,数据的抽取.转换和装载)实施过程中需要重点考虑的问 题.在ETL过 ... 
- J2SE知识点摘记(二十四)
			覆写hashCode() 在明白了HashMap具有哪些功能,以及实现原理后,了解如何写一个hashCode()方法就更有意义了.当然,在HashMap中存取一个键值对涉及到的另外一个方法为equa ... 
- The Best Coder and Why? (最牛气的程序员)——精彩!
			原文出处我已经找不到了,总之不是原创了,不过,重新看过,挺受震撼的.程序员出身的我们,或许记不住某些算法细节,但记住他们的名字,也许是应该的. MIT BBS上说微软电话面试的一道题就是“Who do ... 
- 二道shell面试题
			1.按照给出的运行结果,编写一个名为xunhuan 的shell过程(用循环语句). 0 10 210 3210 43210 543210 6543210 76543210 876543210 2.编 ... 
- SDL介绍
			SDL(Simple DirectMedia Layer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成.SDL提供了数种控制图像.声音.输出入的函数,让开发者只要用相同或是相似的代码就可以开发 ... 
- ceph伦理概念
			Preface: CEPH: THE FUTURE OF STORAGE(未来存储) Ceph was made possible by a global community of passionat ... 
- 美国TJX公司 - MBA智库百科
			美国TJX公司 - MBA智库百科 TJX公司总部设在美国波士顿,在北美地区和许多欧洲国家开有连锁分店,仅在美国就有2500多家分店. TJX Companies, Inc. 是美国和全世界的服装和家 ... 
- python-聊聊反射
			反射 对于初学python可能较难理解,但反射是非常有用. 试想一下,当别的程序传入给你写的这段代码一个变量(var=“math”),这个变量是一个字符串,这个字符串是一个模块或者一个模块下的某个方法 ... 
