我的HibernateSearch笔记
话不多说,直接上代码:
实体类:
package com.smt.pojo; import java.io.Serializable; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
@Entity
@Indexed
@Table(name="XFZ_TXJP_POOL",catalog = "ecrm")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Analyzer(impl=SmartChineseAnalyzer.class)
public class Txjp implements Serializable { /**
*
*/
private static final long serialVersionUID = 1L; public Txjp(){ } public Txjp(String pid){
super();
this.pid = pid;
} private String pid; @Id
@Column(name = "PID", unique = true, nullable = false, length = 50)
@DocumentId
public String getPid() {
return pid;
} public void setPid(String pid) {
this.pid = pid;
} private String sdate; @Column(name = "SDATE", nullable = false)
public String getSdate() {
return sdate;
} public void setSdate(String sdate) {
this.sdate = sdate;
} private String keyWord; @Column(name = "KEY_WORD", nullable = true)
public String getKeyWord() {
return keyWord;
} public void setKeyWord(String keyWord) {
this.keyWord = keyWord;
} private String productId; @Column(name = "PRODUCTID", nullable = true)
public String getProductId() {
return productId;
} public void setProductId(String productId) {
this.productId = productId;
} private String plink; @Column(name = "PLINK", nullable = true)
public String getPlink() {
return plink;
} public void setPlink(String plink) {
this.plink = plink;
} private String sku; @Column(name = "SKU", nullable = true)
public String getSku() {
return sku;
} public void setSku(String sku) {
this.sku = sku;
} private String skuLink; @Column(name = "SKU_LINK", nullable = true)
public String getSkuLink() {
return skuLink;
} public void setSkuLink(String skuLink) {
this.skuLink = skuLink;
} private String fromArea; @Column(name = "FROMAREA", nullable = true)
public String getFromArea() {
return fromArea;
} public void setFromArea(String fromArea) {
this.fromArea = fromArea;
} private String label; @Column(name = "LABEL", nullable = true)
public String getLabel() {
return label;
} public void setLabel(String label) {
this.label = label;
} private String skuParty; @Column(name = "SKU_PARTY", nullable = true)
@Field(store=Store.NO)
public String getSkuParty() {
return skuParty;
} public void setSkuParty(String skuParty) {
this.skuParty = skuParty;
} private String storeUrl; @Column(name = "STOREURL", nullable = true)
public String getStoreUrl() {
return storeUrl;
} public void setStoreUrl(String storeUrl) {
this.storeUrl = storeUrl;
} private String productName; @Column(name = "PRODUCTNAME", nullable = true)
@Field(store=Store.NO)
public String getProductName() {
return productName;
} public void setProductName(String productName) {
this.productName = productName;
}
}
查询方法(dao层):
package com.smt.dao.impl; import java.util.Iterator;
import java.util.List; import org.apache.log4j.Logger;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.Version;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.SearchFactory;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import com.smt.dao.ITxjpDao;
import com.smt.pojo.Txjp; @Repository("txjpDao")
public class TxjpDaoImpl implements ITxjpDao { private static final Logger LOGGER = Logger.getLogger(TxjpDaoImpl.class); @Autowired
private SessionFactory sessionFactory; private Session getCurrentSession(){
return this.sessionFactory.getCurrentSession();
} @Override
public Txjp load(String id) {
// TODO Auto-generated method stub
return null;
} @Override
public Txjp get(String id) {
// TODO Auto-generated method stub
return null;
} @Override
public List<Txjp> findAll() {
LOGGER.info("查询全部数据BySql");
Session session = this.getCurrentSession();
List<Txjp> list = null;
try {
list = session.createQuery("from Txjp").list();
} catch (Exception e) {
LOGGER.error(e.getMessage());
}finally{
session.close();
}
return list;
} @Override
public void persist(Txjp entity) {
// TODO Auto-generated method stub } @Override
public String save(Txjp entity) {
// TODO Auto-generated method stub
return null;
} @Override
public void saveOrUpdate(Txjp entity) {
// TODO Auto-generated method stub } @Override
public void delete(String id) {
// TODO Auto-generated method stub } @Override
public void flush() {
// TODO Auto-generated method stub } @Override
public List<Txjp> LoadAll() {
Index();
Session session = sessionFactory.openSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
//在字段content中检索
//QueryParser queryParser = new QueryParser(Version.LUCENE_36, "productName", new SmartChineseAnalyzer(Version.LUCENE_36));
Query luceneqQuery=null; //多项检索
SearchFactory sf = fullTextSession.getSearchFactory();
QueryBuilder qb = sf.buildQueryBuilder().forEntity(Txjp.class).get();
//多项检索
luceneqQuery = qb.keyword().onFields("productName").matching("酱油").createQuery();
//执行检索,得到结果集
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneqQuery, Txjp.class);
List<Txjp> list = fullTextQuery.list();
//查看结果做验证
fullTextSession.close();
LOGGER.info("测试一下");
//这里会提示报Session已经关闭
//session.close();
return list;
} @Override
public void Index() {
Session session = sessionFactory.openSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
//查出结果
List<Txjp> list = session.createCriteria(Txjp.class).list();
session.beginTransaction();
//依次建立索引
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Txjp model = (Txjp) iterator.next();
fullTextSession.index(model);
}
session.getTransaction().commit();
session.close();
}
}
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MvcTest</groupId>
<artifactId>MvcTest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MvcTest Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.4.RELEASE</spring.version>
<hibernate.version>4.2.11.Final</hibernate.version>
<jackson.version>2.5.0</jackson.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- 使用springMVC配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency> <!-- 关系型数据库整合时配置 如hibernate jpa等 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency> <!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency> <!-- 二级缓存ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.9.0</version>
</dependency> <!-- mysql连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.1</version>
</dependency>
<!-- c3p0数据源 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5-pre10</version>
</dependency> <!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency> <!-- aop -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency> <!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end --> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>4.2.0.Final</version>
</dependency> <dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-smartcn</artifactId>
<version>3.6.2</version>
</dependency> </dependencies>
<build>
<finalName>MvcTest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
另外分享三个我学习到的博客,谢谢这三位博主:
http://blog.csdn.net/zhengwei223/article/details/11952763
http://blog.csdn.net/dm_vincent/article/details/40649203
http://www.tuicool.com/articles/MZfiAb
我的HibernateSearch笔记的更多相关文章
- git-简单流程(学习笔记)
这是阅读廖雪峰的官方网站的笔记,用于自己以后回看 1.进入项目文件夹 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- SQL Server技术内幕笔记合集
SQL Server技术内幕笔记合集 发这一篇文章主要是方便大家找到我的笔记入口,方便大家o(∩_∩)o Microsoft SQL Server 6.5 技术内幕 笔记http://www.cnbl ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- NET Core-学习笔记(三)
这里将要和大家分享的是学习总结第三篇:首先感慨一下这周跟随netcore官网学习是遇到的一些问题: a.官网的英文版教程使用的部分nuget包和我当时安装的最新包版本不一致,所以没法按照教材上给出的列 ...
- springMVC学习笔记--知识点总结1
以下是学习springmvc框架时的笔记整理: 结果跳转方式 1.设置ModelAndView,根据view的名称,和视图渲染器跳转到指定的页面. 比如jsp的视图渲染器是如下配置的: <!-- ...
- 读书笔记汇总 - SQL必知必会(第4版)
本系列记录并分享学习SQL的过程,主要内容为SQL的基础概念及练习过程. 书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL i ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
随机推荐
- js里面如何才能让成员方法去调用类中其他成员
function fun(){ var _this = this; //如果函数是用var定义的私有函数,如下 var func1 = function(){ } //那么类中其他函数都可以直接通过f ...
- oracle 子查询详解 in和exists的区别
sql允许多层嵌套,子查询是嵌套在其他查询中的查询.我们可以把子查询当做一张表来看到,即外层语句可以把内嵌的查询结果当做一张表使用. 子查询查询结果有三种情况 不返回查询记录.若子查询不返回记录则主查 ...
- ZC_02_获取Constructor
1. package reflectionZ; import java.lang.reflect.Constructor; import java.lang.reflect.Type; public ...
- flash滚动图片遮住二级下拉菜单解决方法
如上图所示,在进行排版时,如果不注意会遇到二级下拉菜单被下边的img图片遮住.此种情况在ie7 8 中出现. 解决方法:给二级下拉菜单添加z-index:9999;position:relative; ...
- sizeof结构体
规则1:结构体的对折长度为其基本数据成员的长度的最大值. 规则2:指定边界情况下,结构体的对折长度为自身对折长度和指定对折长度中较小者. 规则3:当行内结构体的基本数据成员的起始地址必须为其长度的整数 ...
- oepnstack笔记
openstack简介: 组件:Nova 提供计算资源池neutron 网络资源管理horizon 基于openstack API借口使用django开发的web管理 组件:Nova 提供计算资源池n ...
- 33-THREE.JS 圆柱体
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- Win7系统搭建WiFi热点详细攻略
(转自:http://blog.csdn.net/gisredevelopment/article/details/16113889) 一.如果你之前没有在笔记本上搭建过WiFi,那么恭喜你,你的笔记 ...
- 四则运算生成与校检 Python实现
GitHub地址 https://github.com/little-petrol/Arithmetic.git 合作者: 郭旭 和 卢明凯 设计实现过程 代码的组织主要分为两个部分: 算法与结构体的 ...
- 原创:项目管理的理论与实践 讲座的PPT
业余时间做的两个PPT,曾经给公司同事讲过,PPT内容毕竟还是不够全面,如果有不清楚的地方,欢迎提问 项目管理的理论与实践 虚拟案例-超市管理系统