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" xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <aop:aspectj-autoproxy/> <context:component-scan base-package="cn.itcast"/> <!-- 使用数据源和指定persistence.xml位置的方式创建entityManagerFactory,如果使用的不是hibernate JPA实现, 需要在tomcat作一些特殊配置.具体参考手册 注意:使用该方式需要把persistence.xml中的hibernate.connection.driver_class,hibernate.connection.username,hibernate.connection.password,hibernate.connection.url配置删除 --> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${driverClass}"/> <property name="jdbcUrl" value="${jdbcUrl}"/> <property name="user" value="${user}"/> <property name="password" value="${password}"/> <!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="1"/> <!-- 连接池中保留的最小连接数。 --> <property name="minPoolSize" value="1"/> <!-- 连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="300"/> <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="60"/> <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="5"/> <!-- 每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60"/> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="compass" class="org.compass.spring.LocalCompassBean"> <property name="classMappings"> <list> <value>cn.itcast.bean.product.ProductInfo</value> <value>cn.itcast.bean.product.Brand</value> <value>cn.itcast.bean.product.ProductStyle</value> <value>cn.itcast.bean.product.ProductType</value> </list> </property> <property name="compassSettings"> <props> <prop key="compass.engine.analyzer.default.type">net.paoding.analysis.analyzer.PaodingAnalyzer</prop> <prop key="compass.engine.connection">file://d:/index</prop> <!-- 在内存中建立索引 <prop key="compass.engine.connection">ram://index</prop> --> <prop key="compass.engine.highlighter.default.formatter.simple.pre"><![CDATA[<font color='red'>]]></prop> <prop key="compass.engine.highlighter.default.formatter.simple.post"><![CDATA[</font>]]></prop> <prop key="compass.transaction.factory">org.compass.spring.transaction.SpringSyncTransactionFactory</prop> </props> </property> <property name="transactionManager" ref="transactionManager" /> </bean> <!-- 自动完成 索引的添加/更新/删除操作--> <!-- 创建org.compass.gps.impl.SingleCompassGps 对象是,start 这个方法启动实体的接听. --> <bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" init-method="start" destroy-method="stop"> <property name="compass" ref="compass" /> <property name="gpsDevices"> <list> <!-- 设置JPA驱动 --> <bean class="org.compass.gps.device.jpa.JpaGpsDevice"> <property name="name" value="jpaDevice" /> <property name="entityManagerFactory" ref="entityManagerFactory" /> <property name="injectEntityLifecycleListener" value="true"/> </bean> </list> </property> </bean> </beans> paoding分词器的应用: 1、考入jar包 2、把dic目录( dictionary) 拷贝到src目录下。 3、paoding-dic-home.properties 拷贝到 src目录下 搜索的应用: package cn.itcast.service.product; import cn.itcast.bean.QueryResult; import cn.itcast.bean.product.ProductInfo; public interface ProductSearchService { /** * 搜索商品 * @param keyword 关键字 * @param firstResult 开始索引 * @param maxResult 每页获取的记录数 * @return */ public QueryResult<ProductInfo> query(String keyword, int firstResult, int maxResult); } package cn.itcast.service.product.impl; import javax.annotation.Resource; import org.compass.core.Compass; import org.compass.core.CompassTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import cn.itcast.bean.QueryResult; import cn.itcast.bean.product.ProductInfo; import cn.itcast.service.product.ProductSearchService; @Service @Transactional public class ProductSearchServiceBean implements ProductSearchService { private CompassTemplate compassTemplate; @Resource public void setCompass(Compass compass){ this.compassTemplate = new CompassTemplate(compass); } public QueryResult<ProductInfo> query(String keyword, int firstResult, int maxResult){ return compassTemplate.execute(new QueryCallback(keyword, firstResult, maxResult)); } } package cn.itcast.service.product.impl; import java.util.ArrayList; import java.util.List; import org.compass.core.CompassCallback; import org.compass.core.CompassException; import org.compass.core.CompassHits; import org.compass.core.CompassQueryBuilder; import org.compass.core.CompassSession; import org.compass.core.CompassQuery.SortDirection; import org.compass.core.CompassQuery.SortPropertyType; import cn.itcast.bean.QueryResult; import cn.itcast.bean.product.ProductInfo; public class QueryCallback implements CompassCallback<QueryResult<ProductInfo>> { private String keyword; private int firstResult; private int maxResult; public QueryCallback(String keyword, int firstResult, int maxResult) { this.firstResult = firstResult; this.maxResult = maxResult; this.keyword = keyword; } public QueryResult<ProductInfo> doInCompass(CompassSession session) throws CompassException { /*查询指定类别的匹配记录,并按position降序排序 CompassQueryBuilder queryBuilder = session.queryBuilder(); CompassHits hits = queryBuilder.bool() .addMust(queryBuilder.spanEq("typeid", typeid)) .addMust(queryBuilder.queryString(keyword).toQuery()) .toQuery().addSort("position", SortPropertyType.FLOAT, SortDirection.REVERSE) .hits();//sql: typeid=1 and (xxxx like ?) order by positoin desc */ CompassHits hits = session.find(keyword);//5 QueryResult<ProductInfo> qr = new QueryResult<ProductInfo>(); qr.setTotalrecord(hits.length());//获取匹配记录的总数 int length = firstResult + maxResult; if(length>hits.length()) length = hits.length(); List<ProductInfo> products = new ArrayList<ProductInfo>(); for(int i = firstResult ; i < length ; i++){ ProductInfo product = (ProductInfo)hits.data(i); if(hits.highlighter(i).fragment("productName")!=null) product.setName(hits.highlighter(i).fragment("productName")); if(hits.highlighter(i).fragment("description")!=null) product.setDescription(hits.highlighter(i).fragment("description")); products.add(product); } qr.setResultlist(products); return qr; } }