spring+hibernate+struts2+compass整合

摘要: 项目结构;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>&nbsp;
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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值