本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7624023
建立时间:2006年5月29日




[Java Open Source]基于JAVA 5.0特性的compass应用
软件技术

lhwork 发表于 2006/7/31 11:44:48

JAVA 5.0里面有个很鲜明的就是annotation 在这里我也就是用他来配置实体类,而不用写xml .其他的配置就差不多一样了//声明此类的别名为article,在COMPASS里面可以直接用article来访问@Searchable(alias = "article")public class Article extends BaseEntity {  ..............//这个是必须的,我也没仔细看为什么 public Article() {  }  public List<Special> getSpecialList() {  return specialList; }  /**  * @return Returns the author.  *///设置搜索的别名为author,在查询的时候可以用author=?来检索 @SearchableProperty(name = "author") public String getAuthor() {  return author; }  /**  * @param author  *            The author to set.  */ public void setAuthor(String author) {  this.author = author; }  /**  * @return Returns the brief.  */ public String getBrief() {  return brief; }  /**  * @param brief  *            The brief to set.  */ public void setBrief(String brief) {  this.brief = brief; }  /**  * @return Returns the category.  */ @SearchableComponent public Category getCategory() {  return category; }  /**  * @param category  *            The category to set.  */ public void setCategory(Category category) {  this.category = category; }  /**  * @return Returns the content.  */  public String getContent() {  return content; } 下一步就配置SPING了//一定要声明的 要不上面的类里面的设置就没法工作<bean id="annotationConfiguration" class="org.compass.annotations.config.CompassAnnotationsConfiguration"></bean>//LocalCompassBean是compass的一个实现类,用他来创建索引<bean id="compass" class="org.compass.spring.LocalCompassBean">  <property name="classMappings">   <list>//这里直接写他的类就可以了 以前是写的XML    <value>com.aurora.netschool.entity.Article</value>    <value>。。。。</value>   </list>  </property>  <property name="compassConfiguration" ref="annotationConfiguration" />  <property name="compassSettings">   <props>    <prop key="compass.engine.connection">file://${pathIndex}</prop>    <prop key="compass.transaction.factory">org.compass.spring.transaction.SpringSyncTransactionFactory</prop>   </props>  </property>  <property name="transactionManager" ref="transactionManager" /> </bean>//如果你配置了 HIBERNATE等 在这里你要加上autowire="no" 要不你肯定在启动的时候就出错<bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" init-method="start" destroy-method="stop" autowire="no">  <property name="compass" ref="compass" />  <property name="gpsDevices">   <list>    <bean class="org.compass.spring.device.hibernate.SpringHibernate3GpsDevice" autowire="no">     <property name="name">      <value>hibernateDevice</value>     </property>     <property name="sessionFactory" ref="sessionFactory" />     <property name="mirrorDataChanges">      <value>false</value>     </property>    </bean>   </list>  </property> </bean>下面就是写方法类了public class SearchEngineServiceImpl { ArticleDao articleDao;//这里的变量名要和SPRING里面的一样的 这里可以用GET SET得到SPRING里面的BEAN CompassGps compassGps;  compasscompass;  /**  * @return Returns the compass.  */ public compass getCompass() {  return compass; }  /**  * @param compass  *            The compass to set.  */ public void setCompass(Compass compass) {  this.compass = compass; }  /**  * @return Returns the compassGps.  */ public CompassGps getCompassGps() {  return compassGps; }  /**  * @param compassGps  *            The compassGps to set.  */ public void setCompassGps(CompassGps compassGps) {  this.compassGps = compassGps; }  /**  * @return Returns the articleDao.  */ public ArticleDao getArticleDao() {  return articleDao; }  /**  * @param articleDao  *            The articleDao to set.  */ public void setArticleDao(ArticleDao articleDao) {  this.articleDao = articleDao; }  public String getPathIndex() {  return com.aurora.Configuration.getInstance().getConfig("pathIndex")    + "/"; }  /*  * 创建检索文件  *   * @see com.aurora.netschool.service.SearchEngineService#creatIndexByCompass()  *      compass创建索引  */ public void creatIndexByCompass() throws IOException {  compassGps.index(); }  public List<Article> searchByCompass(String queryString, String type,   String categoryName) {  List<Article> articleList = new ArrayList<Article>();  CompassSession session=compass.openSession();。。。。。。。    CompassHits hits=query.hits();  for (int i = 0; i < hits.getLength(); i++) {   Article article = (Article) hits.data(i);   //hits.hit(i).getData().   if ("title".equals(type)) {    article.setTitle(hits.highlighter(i).fragment(type));   } else if ("author".equals(type)) {    article.setAuthor(hits.highlighter(i).fragment(type));       } else if ("content".equals(type)) {    article.setHtmlContent(hits.highlighter(i).fragment(type));   }   articleList.add(article);  }  compassTran.commit();  session.close();  return articleList; }}好了 自己配置用一下吧


阅读全文(3383) | 回复(2) | 编辑 | 精华
 


回复:基于JAVA 5.0特性的compass应用
软件技术

ytzhsh(游客)发表评论于2007/6/27 13:16:19

做人要厚道... 转帖说明出处呢.. 哎 也算了 我在blogjava的空间不用了


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


回复:基于JAVA 5.0特性的compass应用
软件技术

HELLOBOY(游客)发表评论于2006/12/13 16:11:23

SpringHibernate3GpsDevice mirrorDataChanges属性如果设置为false.索引将不会自动被同步

个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.359 second(s), page refreshed 144806075 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号