`

ssh与compass结合的例子

阅读更多
SSH把compass加进来是要加5个jar,    compass-2.1.0.jar  compass-index-patch.jar  lucene-core.jar
lucene-highlighter.jar    paoding-analysis.jar
我贴出来下面一些主要的代码。。如果有问题请留言。。
ProductManagerImpl 实现类

package com.revic.example.servce.impl;

import java.util.ArrayList;
import java.util.List;

import org.compass.core.Compass;
import org.compass.core.CompassHits;
import org.compass.core.CompassSession;
import org.compass.core.CompassTemplate;
import org.compass.core.CompassTransaction;


import com.revic.example.dao.ProductDao;
import com.revic.example.model.Product;
import com.revic.example.model.Testuser;
import com.revic.example.servce.ProductManager;

public class ProductManagerImpl implements ProductManager {
   private ProductDao productDao;
   private CompassTemplate compassTemplate;
public CompassTemplate getCompassTemplate() {
    return compassTemplate;
}
public void setCompassTemplate(CompassTemplate compassTemplate) {
    this.compassTemplate = compassTemplate;
}
public ProductDao getProductDao() {
    return productDao;
}
public void setProductDao(ProductDao productDao) {
    this.productDao = productDao;
}
    public void addproduct(Product p) {
        productDao.addproduct(p);
    }
    public List searchProducts(String queryString) {
         System.out.println("searchProducts...");
        Compass compass = compassTemplate.getCompass();
        CompassSession session=compass.openSession();
        List list = new ArrayList();
        CompassTransaction tx=session.beginTransaction();
      
        CompassHits hits= session.queryBuilder().queryString("name:"+queryString).toQuery().hits();
        System.out.println("queryString:"+queryString);
        System.out.println("hits:"+hits.getLength());
        for(int i=0;i<hits.length();i++){
            Product hit=(Product)hits.data(i);
            list.add(hit);
        }
        tx.commit();
        session.close();
        return list;
    }
    public List searchtestuser(String queryString) {
         System.out.println("searchtestuser...");
        Compass compass = compassTemplate.getCompass();
        CompassSession session=compass.openSession();
        List list = new ArrayList();
        CompassTransaction tx=session.beginTransaction();
      
        CompassHits hits= session.queryBuilder().queryString("username:"+queryString).toQuery().hits();
        System.out.println("queryString:"+queryString);
        System.out.println("hits:"+hits.getLength());
        for(int i=0;i<hits.length();i++){
            Testuser hit=(Testuser)hits.data(i);
            list.add(hit);
        }
        tx.commit();
        session.close();
        return list;
    }

}

CompassIndexBuilder  类

package com.revic.example.servce.impl;
import org.compass.gps.CompassGps;
import org.springframework.beans.factory.InitializingBean;



public class CompassIndexBuilder implements InitializingBean { 
    // 是否需要建立索引,可被设置为false使本Builder失效.
    private boolean buildIndex = false;

    // 索引操作线程延时启动的时间,单位为秒
    private int lazyTime = 10;

    // Compass封装
    private CompassGps compassGps;

    // 索引线程
    private Thread indexThread = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(lazyTime * 1000);
                System.out.println("begin compass index...");
                long beginTime = System.currentTimeMillis();
                // 重建索引.
                // 如果compass实体中定义的索引文件已存在,索引过程中会建立临时索引,
                // 索引完成后再进行覆盖.
                compassGps.index();
                long costTime = System.currentTimeMillis() - beginTime;
                System.out.println("compss index finished.");
                System.out.println("costed " + costTime + " milliseconds");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

  
    public void afterPropertiesSet() throws Exception {
        if (buildIndex) {
            indexThread.setDaemon(true);
            indexThread.setName("Compass Indexer");
            indexThread.start();
        }
    }

    public void setBuildIndex(boolean buildIndex) {
        this.buildIndex = buildIndex;
    }

    public void setLazyTime(int lazyTime) {
        this.lazyTime = lazyTime;
    }

    public void setCompassGps(CompassGps compassGps) {
        this.compassGps = compassGps;
    }
}



applicationContext-compass.xml 文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
    default-lazy-init="true">


    <bean id="annotationConfiguration"
        class="org.compass.annotations.config.CompassAnnotationsConfiguration">
    </bean>


    <bean id="compass" class="org.compass.spring.LocalCompassBean">
        <property name="resourceDirectoryLocations">
            <list>
                <value>classpath:com/revic</value>
            </list>
        </property>
        <property name="connection">
            <value>/lucene/indexes</value>
        </property>


        <property name="classMappings">
            <list>
                 <value>com.revic.example.model.Product</value>
                <value>com.revic.example.model.Testuser</value>
            </list>
        </property>
        <property name="compassConfiguration"
            ref="annotationConfiguration" />

        <property name="compassSettings">
            <props>
                <prop key="compass.transaction.factory">
                    org.compass.spring.transaction.SpringSyncTransactionFactory
                </prop>
                  <prop key="compass.engine.analyzer.MMAnalyzer.CustomAnalyzer">net.paoding.analysis.analyzer.PaodingAnalyzer </prop>
            </props>
        </property>

        <property name="transactionManager" ref="transactionManager" />
    </bean>


    <bean id="hibernateGpsDevice"
        class="org.compass.gps.device.hibernate.HibernateGpsDevice">
        <property name="name">
            <value>hibernateDevice</value>
        </property>
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="mirrorDataChanges">
            <value>true</value>
        </property>
    </bean>
    <!-- 同步更新索引 -->
    <bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps"
        init-method="start" destroy-method="stop">
        <property name="compass" ref="compass" />
        <property name="gpsDevices">
            <list>
                <bean
                    class="org.compass.spring.device.SpringSyncTransactionGpsDeviceWrapper">
                    <property name="gpsDevice" ref="hibernateGpsDevice" />
                </bean>
            </list>
        </property>
    </bean>


       <bean id="compassTemplate"
        class="org.compass.core.CompassTemplate">
        <property name="compass" ref="compass" />
    </bean>

    <!-- 定时重建索引(利用quartz)或随Spring ApplicationContext启动而重建索引 -->
    <bean id="compassIndexBuilder"
        class="com.revic.example.servce.impl.CompassIndexBuilder"
        lazy-init="false">
        <property name="compassGps" ref="compassGps" />
        <property name="buildIndex" value="true" />
        <property name="lazyTime" value="10" />
    </bean>
</beans>

applicationContext-common.xml 文件

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
<bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="oracle.jdbc.driver.OracleDriver">
        </property>
        <property name="url"
            value="jdbc:oracle:thin:@192.168.0.1:1521:ora9i">
        </property>
        <property name="username" value="××"></property>
        <property name="password" value="××"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle9Dialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/revic/example/model/Product.hbm.xml</value>
                <value>com/revic/example/model/Testuser.hbm.xml</value>
                </list>
        </property></bean>
  
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>  
    </bean>
  
    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="search*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="modify*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
  
    <!-- 那些类的哪些方法参与事务 -->
    <aop:config>
        <aop:pointcut id="allManagerMethod" expression_r_r="execution(* com.revic.example.*.*(..))"/>
        <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
    </aop:config>
</beans>

applicationContext-bean.xml 文件、

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

        <bean id="productDao" class="com.revic.example.dao.impl.ProductDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <bean id="productManager"  class="com.revic.example.servce.impl.ProductManagerImpl">
        <property name="productDao" ref="productDao"></property>
        <property name="compassTemplate" ref="compassTemplate"></property>
        </bean>
        <bean id="addbean" class="com.revic.example.action.ProductAction">
        <property name="productManager" ref="productManager"></property>
        </bean>
        </beans>
分享到:
评论
1 楼 leon.s.kennedy 2012-11-14  

敢问大侠扣扣多少
求代码 呵呵

相关推荐

    ssh+compass实现站内搜索分页.rar

    本教程将重点讲解如何使用SSH框架与Compass结合,实现在网站内的搜索分页功能。 首先,让我们深入了解SSH框架的每个组件: 1. **Struts2**:这是一个MVC(Model-View-Controller)架构的开源框架,用于控制应用...

    SSH+compass

    SSH+compass整合的小例子,导入即可运行,数据库用的是mysql,用的都是注解技术,代码良好,dao层用了江南白衣的思想,根据泛型和反射封装,可以做基类,里面有注册和查询的小例子,基本可以在此基础上继续开放你所...

    Compass与ssh框架整合

    而Compass是一个强大的搜索引擎库,它能够将数据与Lucene搜索引擎紧密结合,提供对应用程序数据的实时搜索功能。这篇博客文章“Compass与ssh框架整合”将深入探讨如何将Compass集成到SSH框架中,以实现更高效的数据...

    SSH2+compass的模糊查询

    关于ssh+compass的源码仿写,功能简单,可以参考研究。

    SSH+compass 整合

    本资源是struts2 + spring2.5 + hibernate 3.2 + lucene 2.4 + compass 2.0整合实例,可以为初学入门者,提供个参考,本人也是网上找的,感觉很不错(因其中所用的jar文件太大,无法上传,大家可以自己添加各框架...

    Compass+SSH搜索引擎简单项目

    【SSH+Compass搜索引擎简单项目】是一个基于Struts2(S),Hibernate(H)和Spring(S)的Java Web应用程序,结合了Compass搜索引擎库,用于实现对数据库中两个表的高效检索功能。这个项目旨在提供一个清晰易懂的...

    SSH2整合compass做搜索引挚

    SSH2和Compass整合构建搜索引擎是一个在Java Web开发中实现高效全文检索的常见技术实践。SSH2是指Spring、Struts2和Hibernate这三个开源框架的组合,它们分别负责控制层、表现层和持久层的处理。而Compass是一个基于...

    compass2简单例子

    标题 "compass2简单例子" 指向的是一个关于 Compass 的使用教程,Compass 是一个基于 Sass 的 CSS 预处理器。它简化了编写复杂的 CSS3 规则,并提供了易于管理的项目结构。这个简单的例子可能包括如何设置 Compass、...

    compass完整可用项目

    此项目对于理解Compass和Lucene在实际应用中的使用,以及如何结合SSH框架开发Java Web应用具有很高的学习价值。通过分析和研究这个项目,开发者可以深入掌握Java全文搜索引擎的实现,以及如何在SSH框架下组织和管理...

    SSH2+compass2.2搜索实例(完整工程)

    SSH2+Compass2.2搜索实例是一个基于Java Web开发的综合项目,它结合了Struts2.2.3、Spring 3.0.6、Hibernate3.6以及Compass2.2这四个核心技术,旨在提供一个高效且灵活的搜索功能。这个完整的工程是一个很好的学习...

    compass-2.2.0+hibernate-3.2+struts-2.1.8.1+spring-framework-2.5.4

    标题 "compass-2.2.0+hibernate-3.2+struts-2.1.8.1+spring-framework-2.5.4" 指的是一个集成开发环境,它结合了四个关键的技术组件:Compass、Hibernate、Struts 2 和 Spring Framework,这些都是Java Web开发中的...

    struts2+spring2.5+hibernate3.26+compass2.1搜索引擎简单实现(无jar包)

    你需要下载并添加compass库的jar文件,然后配置Compass的连接器,使其与Hibernate集成。这样,当Hibernate进行数据操作时,Compass会自动更新索引。在Spring配置中,你可以声明一个CompassTemplate bean,它提供了与...

    基于Compass2.2与Spring 结合建立索引的实例

    ### 基于Compass2.2与Spring结合建立索引的实例 #### 一、Compass简介 Compass是一个开源的.NET和Java框架,它为应用程序提供了完整的文本搜索功能。该框架支持多种搜索引擎(如Elasticsearch, Solr等),使得开发...

    compass+ssh2集成 hibernategps问题

    标题中的“compass+ssh2集成 hibernategps问题”指的是在Java开发中,开发者尝试将Compass搜索引擎库与SSH2(Spring、Struts和Hibernate)框架整合时遇到的问题,特别是与Hibernate的GPS(Global Persistent ...

    基于Luncene的compass框架详解-java

    假设在一个基于SSH(Spring+Struts+Hibernate)架构的项目中使用Compass框架,首先需要在项目中加入Compass相关的jar包,然后对实体bean进行标注,例如: ```java @Searchable public class Product implements ...

    Compass技术文档

    Compass是一个高级开源的Java搜索引擎框架,旨在简化应用程序与搜索引擎之间的集成过程。它通过结合强大的Lucene搜索引擎以及流行的应用框架如Hibernate和Spring,使得开发者能够轻松地将搜索功能融入到Java应用程序...

    SSH 比Google搜索更强大的搜索

    SSH与J2EE结合,可以实现更高级别的服务器管理和应用监控。例如,在J2EE应用中,SSH可以用来远程管理应用服务器,如Tomcat、JBoss,或者执行SQL查询来检查数据库状态。 标签中的"功能最强大的搜索"可能指的是SSH...

    mongodb安装包和compass

    在Node.js学习过程中,MongoDB和Compass的结合使用可以帮助你更好地理解和操作数据库。通过Node.js的MongoDB驱动,你可以编写JavaScript代码来与MongoDB交互,创建和查询集合,执行CRUD(创建、读取、更新、删除)...

Global site tag (gtag.js) - Google Analytics