当前位置:编程学习 > JAVA >>

spring+hibernate+struts2+compass整合

项目结构:

compass_project



Return to Gallery  | Original Image 


需要引入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;

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,