基于Spring的Hibernate Search全文检索功能示例

数据库:Oracle 9i
JDBC驱动:OJDBC14
开发环境:Eclipse-JEE
Spring版本:Spring 2.0.6
Hibernate版本:Hibernate Core 3.2.5/Hibernate Annotation 3.3.0/Hibernate Validator 3.0.0/Hibernate Search 3.0.0 Beta4

//jdbc.properties(JDBC配置文件)
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.0.3:1521:itone
jdbc.username=test
jdbc.password=test

//hibernate.properties(Hibernate配置文件)
hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
hibernate.show_sql=false
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.hbm2ddl.auto=update
######以下是Hibernate Search的简单配置######
hibernate.search.default.directory_provider=org.hibernate.search.store.FSDirectoryProvider
hibernate.search.default.indexBase=E:/indexes

//Foo.java(Hibernate POJO文件)
@Entity
@Table(name = "search_foo")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Indexed(index = "search_foo")
public class Foo implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@DocumentId
private Integer id;

@Column(nullable = false, length = 256)
@Field(name = "name", index = Index.TOKENIZED, store = Store.YES)
private String name;

@Lob
@Field(name = "content", index = Index.TOKENIZED, store = Store.YES)
private String content;

@Column(name = "post_time", nullable = false)
@DateBridge(resolution = Resolution.DAY)
private Calendar postTime;

public Integer getId() {
     return id;
}

public void setId(Integer id) {
     this.id = id;
}

public String getName() {
     return name;
}

public void setName(String name) {
     this.name = name;
}

public String getContent() {
     return content;
}

public void setContent(String content) {
     this.content = content;
}

public Calendar getPostTime() {
     return postTime;
}

public void setPostTime(Calendar postTime) {
     this.postTime = postTime;
}
}

//hibernate.cfg.xml(Hibernate配置文件)
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

     <mapping class="com.itone.search.pojo.Foo"/>
  
</session-factory>

</hibernate-configuration>

//applicationContext-conf.xml(加载.properties文件的配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="byName">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="false">
     <property name="locations">
      <list>
       <value>classpath*:jdbc.properties</value>
       <value>classpath*:hibernate.properties</value>
      </list>
     </property>
</bean>

</beans>

//applicationContext-database.xml(数据源及SessionFactory定义文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="byName">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     <property name="driverClassName" value="${jdbc.driverClassName}"/>
     <property name="url" value="${jdbc.url}"/>
     <property name="username" value="${jdbc.username}"/>
     <property name="password" value="${jdbc.password}"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
     <property name="configLocation" value="/WEB-INF/classes/hibernate.cfg.xml"/>
     <property name="hibernateProperties">
      <props>
       <prop key="hibernate.dialect">${hibernate.dialect}</prop>
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
       <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
       <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
    
       <prop key="hibernate.search.default.directory_provider">${hibernate.search.default.directory_provider}</prop>
       <prop key="hibernate.search.default.indexBase">${hibernate.search.default.indexBase}</prop>
      </props>
     </property>
</bean>

<bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator"/>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"/>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"/>

</beans>

//applicationContext-manager.xml(Manager定义文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="byName">

<bean id="baseTxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
     <property name="proxyTargetClass" value="true"/>
           <property name="transactionAttributes">
      <props>
       <prop key="*">PROPAGATION_REQUIRED</prop>
      </props>
     </property>
</bean>

<!-- Authentication manager for login and role checking -->
<bean id="fooManager" parent="baseTxService">
     <property name="target">
      <bean class="com.itone.search.manager.FooManager"/>
     </property>
</bean>

</beans>

//SpringMVC、Log4j、EHCache配置略

//FooManager.java(做全文检索示例)
package com.itone.search.manager;

import java.util.List;

import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;

import com.itone.search.pojo.Foo;

public class FooManager extends BaseManager<Foo> {

@SuppressWarnings("unchecked")
public void testQuery() throws Exception {
     QueryParser parser = new QueryParser("name", new StopAnalyzer());
     Query luceneQuery = parser.parse("name:Jack");
     FullTextSession s = Search.createFullTextSession(getSession());
     FullTextQuery query = s.createFullTextQuery(luceneQuery, Foo.class);
     List<Foo> result = query.list();
     for(Foo f : result) {
      System.out.println("ID:" + f.getId());
      System.out.println("NAME:" + f.getName());
      System.out.println("CONTENT:" + f.getContent());
      System.out.println("POSTTIME:" + f.getPostTime());
     }
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值