`

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  

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

相关推荐

Global site tag (gtag.js) - Google Analytics