系统要求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简易使用的更多相关文章

  1. hibernate search例子

    [TOC] 1. 概念介绍 1.1. Hibernate Search Hibernate Search是Hibernate的子项目,把数据库全文检索能力引入到项目中,并通过"透明" ...

  2. S2SH+Hibernate search出现的问题

    一  java.lang.NoSuchMethodError: org.hibernate.engine.transaction.spi.TransactionEnvironment.getJtaPl ...

  3. Hibernate search使用示例(基础小结-注解方式)

    (对于项目环境配置,一直没怎么看过.这次经历里从基础环境搭建到hibernate search示例的完成) 1.首先创建project,选择了web project. 2.导入hibernate se ...

  4. [SpringBoot系列]--Spring Hibernate search 注解实现(未测试)

    1.maven项目pom.xml加入依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId& ...

  5. Hibernate search与Lucene包异常学习心得

    最近使用了了一下Hibernate  Search这个组件 这个组件是对域模型进行全文检索,在全文检索的底层实现上使用了Lucene技术 在进行小测试的时候费了很大的力气去搞定包的问题 我直接通过实例 ...

  6. [Hibernate Search] (3) 基础查询

    基础查询 眼下我们仅仅用到了基于keyword的查询,实际上Hibenrate Search DSL还提供了其他的查询方式,以下我们就来一探到底. 映射API和查询API 对于映射API.我们能够通过 ...

  7. Hibernate Search集与lucene分词查询

    lucene分词查询参考信息:https://blog.csdn.net/dm_vincent/article/details/40707857

  8. Hibernate和IBatis对比

    [转自]http://blog.csdn.net/ya2dan/article/details/7396598 项目也做过几个, 使用IBatis就做一个项目, 基本上都是使用Hibernate, 也 ...

  9. 备忘:hibernate, logback, slf4j实际应用一例

    用hibernate写一些简单的数据库的Java应用.主要是温习一下.之前弄过的一些都忘了.发现还是得记下来,不然很快就忘. 1. Eclipse版本,用Juno, J2EE版本.最好下载zip版本的 ...

  10. Hibernate正向工程(实体类-->数据库)

    1,新建实体类News.java package com.hanqi.dao; import java.util.Date; public class News { private Integer i ...

随机推荐

  1. NodeJS爬虫系统初探

    NodeJS爬虫系统 NodeJS爬虫系统 0. 概论 爬虫是一种自动获取网页内容的程序.是搜索引擎的重要组成部分,因此搜索引擎优化很大程度上是针对爬虫而做出的优化. robots.txt是一个文本文 ...

  2. 怎样在Eclipse中使用debug模式调试程序

    最基本的操作是: 1, 首先在一个java文件中设断点,然后运行,当程序走到断点处就会转到debug视图下, 2, F5键与F6键均为单步调试,F5是step into,也就是进入本行代码中执行,F6 ...

  3. [LeetCode][Python]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  4. linux下各种代理的设置

    http://los-vmm.sc.intel.com/wiki/OpenStack_New_Hire_Guide#Apply_JIRA_account Set up your proxy. The ...

  5. CSSOM View Module

    就在8月份,也就是上次gf大姨妈来的时候,W3C出炉了CSSOM视图模块(CSS Object Model View)草案.CSSOM视图模块(CSSOM View Module)定义了一些 API, ...

  6. SurfaceView 和 View 区别

    android.view.View 和 android.view.SurfaceView SurfaceView 是从 View 基类中派生出来的显示类,直接子类有 GLSurfaceView和Vid ...

  7. win8.1 安装webaccess遇到的写访问权限错误

    大学的时候闲暇时间都用来玩游戏了,现在工作了,就把工作中遇到的问题和学习心得跟大家共享下,若有不对的地方欢迎指出,谢谢! 前几天突然想换用vs2013开发,就重装了win8.1系统,在新系统上安装we ...

  8. rem单位

    rem单位 rem基础 px是固定单位,不同分辨率下效果不一样,导致网页布局出现偏差. em是根据父元素来改变字大小 rem是根据根元素html来改变字体大小,只要改变了根元素的font-size就可 ...

  9. ASPxGridview在对话框中无法编辑!!

     aspxgridview在使用window.showModelDialog(或者window.showModelessDialog)打开的窗体中居然无法进入编辑!好奇怪啊 . 点击后显示“无法显示网 ...

  10. 解决Adobe Acrobat “正在纠偏图像,正在旋转图像,正在分解页面”问题

    笔者最近遇到的一个问题:用acrobat Pro X 打开pdf显示“正在纠偏图像,正在旋转图像,正在分解页面”,此时acrobat没有响应,要等待其完成,出现就得等一会儿,总出现,总得停顿,看一篇文 ...