摘要: 项目结构;http://www.cnblogs.com/hongten/gallery/image/113449.html=======================================================需要引入compass的相关jar包:compass-2.1.0.jarcompass-index-patch.jarlucene-core ...
项目结构;
http://www.cnblogs.com/hongten/gallery/image/113449.html
=======================================================
需要引入compass的相关jar包:
compass-2.1.0.jar
compass-index-patch.jar
lucene-core.jar
lucene-highlighter.jar
paoding-analysis.jar
=======================================================
建表语句:
1 CREATE TABLE `product` (
2 `id` int(11) NOT NULL AUTO_INCREMENT,
3 `name` varchar(80) COLLATE utf8_unicode_ci DEFAULT NULL,
4 `price` double(6,0) DEFAULT NULL,
5 `brand` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
6 `description` varchar(2000) COLLATE utf8_unicode_ci DEFAULT NULL,
7 PRIMARY KEY (`id`)
8 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
注:这是我在mySQL中建立的product表,与本项目的表有一个地方有区别:id的类型
项目中的是varchar(40),用的uuid的策略。更详细的信息请看:/compass1/src/com/v512/example/model/Product.hbm.xml
=======================================================
/compass1/src/com/v512/example/action/ProductAction.java
1 package com.v512.example.action;
2
3 import java.util.List;
4
5 import com.opensymphony.xwork2.ActionSupport;
6 import com.v512.example.model.Product;
7 import com.v512.example.service.ProductManager;
8 import org.apache.struts2.ServletActionContext;
9
10 public class ProductAction extends ActionSupport {
11
12 private static final long serialVersionUID = 3795048906805728732L;
13 private ProductManager productManager;
14 private Product product;
15 private String queryString;
16
17 public void setQueryString(String queryString) {
18 this.queryString = queryString;
19 }
20
21 public Product getProduct() {
22 return product;
23 }
24
25 public void setProduct(Product product) {
26 this.product = product;
27 }
28
29 public void setProductManager(ProductManager productManager) {
30 this.productManager = productManager;
31 }
32
33 public String insert() {
34 productManager.insertProduct(product);
35 return SUCCESS;
36 }
37
38 public String search() {
39 List results = productManager.searchProducts(queryString);
40 System.out.println(results.size());
41 ServletActionContext.getRequest()
42 .setAttribute("searchresults", results);
43 return SUCCESS;
44 }
45
46 }
/compass1/src/com/v512/example/dao/ProductDao.java
1 package com.v512.example.dao;
2
3 import java.util.List;
4
5 import com.v512.example.model.Product;
6
7 public interface ProductDao {
8 public void create(Product p);
9
10 public Product getProduct(String id);
11
12 public List getProducts();
13
14 public void update(Product product);
15
16 public void remove(String id);
17
18 }
/compass1/src/com/v512/example/dao/hibernate/ProductDaoHibernate.java
1 package com.v512.example.dao.hibernate;
2
3 import java.util.List;
4
5 import com.v512.example.dao.ProductDao;
6 import com.v512.example.model.Product;
7 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
8
9 public class ProductDaoHibernate extends HibernateDaoSupport implements
10 ProductDao {
11
12 public void create(Product p) {
13 getHibernateTemplate().save(p);
14
15 }
16
17 public Product getProduct(String id) {
18 return (Product) getHibernateTemplate().get(Product.class, id);
19 }
20
21 public List getProducts() {
22 return getHibernateTemplate().find("from Product order by id desc");
23 }
24
25 public void remove(String id) {
26 getHibernateTemplate().delete(getProduct(id));
27
28 }
29
30 public void update(Product product) {
31 getHibernateTemplate().saveOrUpdate(product);
32
33 }
34
35 }
/compass1/src/com/v512/example/model/Product.java
在实体类中注入compass
1 package com.v512.example.model;
2
3 import org.compass.annotations.*;
4
5 /**
6 * Product entity.
7 *
8 * @author MyEclipse Persistence Tools
9 */
10 @Searchable
11 public class Product implements java.io.Serializable {
12
13 // Fields
14 //在实体类中注入compass
15 @SearchableId
16 private String id;
17 @SearchableProperty(name = "name")
18 private String name;
19 @SearchableProperty(name = "price")
20 private Double price;
21 @SearchableProperty(name = "brand")
22 private String brand;
23 @SearchableProperty(name = "description")
24 private String description;
25
26 // Constructors
27
28 /** default constructor */
29 public Product() {
30 }
31
32 /** full constructor */
33 public Product(String name, Double price, String brand, String description) {
34 this.name = name;
35 this.price = price;
36 this.brand = brand;
37 this.description = description;
38 }
39
40 // Property accessors
41
42 public String getId() {
43 return this.id;
44 }
45
46 public void setId(String id) {
47 this.id = id;
48 }
49
50 public String getName() {
51 return this.name;
52 }
53
54 public void setName(String name) {
55 this.name = name;
56 }
57
58 public Double getPrice() {
59 return this.price;
60 }
61
62 public void setPrice(Double price) {
63 this.price = price;
64 }
65
66 public String getBrand() {
67 return this.brand;
68 }
69
70 public void setBrand(String brand) {
71 this.brand = brand;
72 }
73
74 public String getDescription() {
75 return this.description;
76 }
77
78 public void setDescription(String description) {
79 this.description = description;
80 }
81
82 }
/compass1/src/com/v512/example/model/Product.hbm.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4 <!--
5 Mapping file autogenerated by MyEclipse Persistence Tools
6 -->
7 <hibernate-mapping>
8 <class name="com.v512.example.model.Product" table="PRODUCT" schema="SCOTT">
9 <id name="id" type="java.lang.String">
10 <column name="ID" length="40" />
11 <generator class="uuid.hex" />
12 </id>
13 <property name="name" type="java.lang.String">
14 <column name="NAME" length="80" />
15 </property>
16 <property name="price" type="java.lang.Double">
17 <column name="PRICE" precision="6" />
18 </property>
19 <property name="brand" type="java.lang.String">
20 <column name="BRAND" length="40" />
21 </property>
22 <property name="description" type="java.lang.String">
23 <column name="DESCRIPTION" length="2000" />
24 </property>
25 </class>
26 </hibernate-mapping>
/compass1/src/com/v512/example/service/ProductManager.java
1 package com.v512.example.service;
2
3 import java.util.List;
4
5 import com.v512.example.model.Product;
6
7 public interface ProductManager {
8 public void insertProduct(Product p);
9
10 public Product findProdcut(String id);
11
12 public List searchProducts(String queryString);
13
14 }
/compass1/src/com/v512/example/service/impl/CompassIndexBuilder.java
1 package com.v512.example.service.impl;
2
3 import org.compass.gps.CompassGps;
4 import org.springframework.beans.factory.InitializingBean;
5
6 /**
7 * 通过quartz定时调度定时重建索引或自动随Spring ApplicationContext启动而重建索引的Builder.
8 * 会启动后延时数秒新开线程调用compassGps.index()函数.
9 * 默认会在Web应用每次启动时重建索引,可以设置buildIndex属性为false来禁止此功能. 也可以不用本Builder,
10 * 编写手动调用compassGps.index()的代码.
11 *
12 */
13 public class CompassIndexBuilder implements InitializingBean {
14 // 是否需要建立索引,可被设置为false使本Builder失效.
15 private boolean buildIndex = false;
16
17 // 索引操作线程延时启动的时间,单位为秒
18 private int lazyTime = 10;
19
20 // Compass封装
21 private CompassGps compassGps;
22
23 // 索引线程
24 private Thread indexThread = new Thread() {
25
26 @Override
27 public void run() {
28 try {
29 Thread.sleep(lazyTime * 1000);
30 System.out.println("begin compass index...");
31 long beginTime = System.currentTimeMillis();
32 // 重建索引.
33 // 如果compass实体中定义的索引文件已存在,索引过程中会建立临时索引,
34 // 索引完成后再进行覆盖.
35 compassGps.index();
36 long costTime = System.currentTimeMillis() - beginTime;
37 System.out.println("compss index finished.");
38 System.out.println("costed " + costTime + " milliseconds");
39 } catch (InterruptedException e) {
40 e.printStackTrace();
41 }
42 }
43 };
44
45 /**
46 * 实现<code>InitializingBean</code>接口,在完成注入后调用启动索引线程.
47 *
48 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
49 */
50 public void afterPropertiesSet() throws Exception {
51 if (buildIndex) {
52 indexThread.setDaemon(true);
53 indexThread.setName("Compass Indexer");
54 indexThread.start();
55 }
56 }
57
58 public void setBuildIndex(boolean buildIndex) {
59 this.buildIndex = buildIndex;
60 }
61
62 public void setLazyTime(int lazyTime) {
63 this.lazyTime = lazyTime;
64 }
65
66 public void setCompassGps(CompassGps compassGps) {
67 this.compassGps = compassGps;
68 }
69 }
/compass1/src/com/v512/example/service/impl/ProductManagerImpl.java
1 package com.v512.example.service.impl;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.compass.core.Compas**s;
7 import org.compass.core.CompassSession;
8 import org.compass.core.CompassTemplate;
9 import org.compass.core.CompassTransaction;
10
11 import com.v512.example.dao.ProductDao;
12 import com.v512.example.model.Product;
13 import com.v512.example.service.ProductManager;
14 import org.compass.core.Compass;
15
16 public class ProductManagerImpl implements ProductManager {
17 private ProductDao productDao;
18 private CompassTemplate compassTemplate;
19
20 public void setCompassTemplate(CompassTemplate compassTemplate) {
21 this.compassTemplate = compassTemplate;
22 }
23
24 public void setProductDao(ProductDao productDao) {
25 this.productDao = productDao;
26 }
27
28 public Product findProdcut(String id) {
29 return productDao.getProduct(id);
30 }
31
32 public void insertProduct(Product p) {
33 productDao.create(p);
34
35 }
36
37 public List searchProducts(String queryString) {
38 Compass compass = compassTemplate.getCompass();
39 CompassSession session = compass.openSession();
40 List list = new ArrayList();
41
42 Compas**s hits = session.queryBuilder().queryString(
43 "name:" + queryString).toQuery().hits();
44 System.out.println("queryString:" + queryString);
45 System.out.println("hits:" + hits.getLength());
46 for (int i = 0; i < hits.length(); i++) {
47 Product hit = (Product) hits.data(i);
48 list.add(hit);
49 }
50
51 return list;
52 }
53
54 public CompassTemplate getCompassTemplate() {
55 return compassTemplate;
56 }
57
58 }
/compass1/src/paoding-dic-home.properties
paoding.dic.home=C:/paoding/dic
paoding.dic.detector.interval=60
/compass1/src/struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6 <struts>
7 <constant name="struts.objectFactory" value="spring" />
8
9 <include file="struts-default.xml" />
10
11 <package name="product" extends="struts-default" namespace="/product">
12 <!-- 配置Struts2的Action,class值要与applicationContext*.xml中的id的值一致。 -->
13 <action name="insert" class="productBean" method="insert">
14 <result>insertOk.jsp</result>
15 </action>
16 <action name="search" class="productBean" method="search">
17 <result>searchResults.jsp</result>
18 </action>
19
20
21 </package>
22
23
24
25
26 </struts>
/compass1/WebRoot/product/input.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <%@ taglib prefix="s" uri="/struts-tags"%>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5 <html>
6 <head>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8 <link href="style/oa.css" rel="stylesheet" type="text/css">
9
10 <title>添加信息</title>
11 </head>
12 <body>
13 <center>
14 <s:form action="insert.action" theme="simple">
15
16 <TABLE class="tableEdit" border="0" cellspacing="1" cellpadding="0" style="width:300px;">
17 <TBODY>
18 <TR>
19 <td align="center" class="tdEditTitle">添加商品名称</TD>
20 </TR>
21 <TR>
22 <td>
23
24
25 <table class="tableEdit" style="width:300px;" cellspacing="0" border="0" cellpadding="0">
26 <tr>
27 <td class="tdEditLabel" >商品名称</td>
28 <td class="tdEditContent"><s:textfield name="product.name" label="名称" /></td>
29 </tr>
30
31 <tr>
32 <td class="tdEditLabel" >商品品牌</td>
33 <td class="tdEditContent"><s:textfield name="product.brand" label="品牌" /></td>
34 </tr>
35
36 <tr>
37 <td class="tdEditLabel" >商品价格</td>
38 <td class="tdEditContent"><s:textfield name="product.price" label="价格" /></td>
39 </tr>
40
41 <tr>
42 <td class="tdEditLabel" >商品描述</td>
43 <td class="tdEditContent"><s:textarea name="product.description" label="描述" />
44 </td>
45 </tr>
46 <tr>
47 <td>
48 </td>
49 <td><s:submit/>
50 <br></td>
51 </tr>
52 </table>
53 </td>
54 </TR>
55 </TBODY>
56 </TABLE>
57 </s:form>
58 </center>
59 </body>
60 </html>
/compass1/WebRoot/product/insertOk.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"<
分享到:
相关推荐
spring+hibernate+jpa+struts1+struts2+springmvc+jquery+freemaker 学习笔记 Compass将lucene、Spring、Hibernate三者结合
标题 "spring + hibernate + struts2 + compass2.1" 暗示了这是一个关于构建基于Java的Web应用程序的经典组合。在这个框架中,Spring作为核心的依赖注入(DI)和面向切面编程(AOP)容器,Hibernate是用于对象关系...
Struts2、Spring、Hibernate和Compass是Java Web开发中常用的四大框架,它们各自负责不同的职责,协同工作可以构建高效、灵活的企业级应用。这里我们主要讨论如何将这些技术结合在一起,实现一个简单的搜索引擎功能...
struts2+spring3+hibernate3+compass实现全文检索功能,希望对初学者有所帮助!
整合这些技术时,开发者通常会创建一个综合的配置文件体系,例如Spring的配置文件可以管理所有组件的依赖和配置,通过引入Struts、Hibernate、JPA、Lucene、JBPM和Compass的相关配置。这需要对每个框架的原理有深入...
struts2 + spring2.5 + hibernate 3.2 + lucene 2.4 + compass 2.0 包含所有jar包,按readme.txt导入并运行即可 开始不用分了................
在本项目中,"JAVA 全文搜索 struts2+spring+hibernate+compass整合记录" 是一个关于如何在Java环境下集成四个关键组件来实现全文搜索引擎的实践教程。Struts2是一个流行的MVC框架,Spring是核心的依赖注入框架,...
标题 "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、Hibernate、Spring和Compass,构建一个具备全文检索功能的Java Web应用。这样的架构不仅实现了业务逻辑和数据访问的解耦,还提高了数据查询的性能,为用户提供了一种...
通过以上步骤和技巧,我们可以将Compass有效地集成到Struts2、Spring和Hibernate的环境中,实现高效、强大的全文搜索功能。这将极大地提升Web应用的用户体验,特别是对于内容丰富的网站或信息管理系统,全文搜索功能...
本资源是struts2 + spring2.5 + hibernate 3.2 + lucene 2.4 + compass 2.0整合实例,可以为初学入门者,提供个参考,本人也是网上找的,感觉很不错(因其中所用的jar文件太大,无法上传,大家可以自己添加各框架...
在这个实例中,"TestCompass"可能是包含具体代码和配置文件的测试项目,用于演示如何将STRUTS、SPRING、HIBERNATE和COMPASS集成。开发者可能需要创建Action类来处理业务逻辑,配置Spring的bean来管理依赖,设置...
SSH(Struts2 + Spring + Hibernate)是一种经典的Java Web开发框架,用于构建高效、可扩展的企业级应用程序。Compass是一个基于Lucene的全文搜索引擎库,它简化了在Java应用中集成全文检索的功能。本教程将重点讲解...
标题中的"S2SH+compass"指的是使用Struts2(S),Spring(S)和Hibernate(H)这三种开源框架的组合,再加上Compass搜索引擎库来实现一个网站内部的全文检索功能。这种组合常见于Java Web开发中,用于构建复杂、高...
Struts2-Spring-Compass 是一个典型的Java企业级应用框架整合示例,它结合了Struts2、Spring3和Hibernate三大主流框架,并利用Compass实现了全文检索功能。这个项目基于Lucene 2.4.1,一个强大的全文搜索引擎库,...
标题中的“compass+ssh2集成 hibernategps问题”指的是在Java开发中,开发者尝试将Compass搜索引擎库与SSH2(Spring、Struts和Hibernate)框架整合时遇到的问题,特别是与Hibernate的GPS(Global Persistent ...
SSH2+Compass2.2搜索实例是一个基于Java Web开发的综合项目,它结合了Struts2.2.3、Spring 3.0.6、Hibernate3.6以及Compass2.2这四个核心技术,旨在提供一个高效且灵活的搜索功能。这个完整的工程是一个很好的学习...
使用compass+lucene实现简单的全文...里面整合了spring2.5、hibernate3.2、struts2.0,是对数据库进行全文检索的一个非常好的demo的所有jar包组合! 对研究基于数据库检索的java开源搜索引擎的朋友有很大的参考价值