• 我的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

  • 相关阅读:
    辅助性的“比较操作符”
    辅助性的“比较操作符”
    浙江一乘客没赶上火车退票不成把票撕了 结果"悲剧"了
    美国超震撼短片-梦想
    在HTML文件中加入空格
    揭秘人造肉
    不能发布网站简讯
    KMPlayer
    文件四处盖章签字等
    冬天到了如何御寒
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/6420040.html
Copyright © 2020-2023  润新知