gallery/image/113449.html">http://www.cnblogs.com/hongten/gallery/image/113449.html
=======================================================
jar包:
Highlighter.jar
=======================================================
`product` ( `id` () AUTO_INCREMENT, `name` () COLLATE utf8_unicode_ci , `price` (,) , `brand` () COLLATE utf8_unicode_ci , `description` () COLLATE utf8_unicode_ci , (`id`) ) ENGINEInnoDB CHARSETutf8 COLLATEutf8_unicode_ci;
MysqL中建立的product表,与本项目的表有一个地方有区别:id的类型
=======================================================
com.v512.example.action; java.util.List; com.opensymphony.xwork2.ActionSupport; com.v512.example.model.Product; com.v512.example.service.ProductManager; org.apache.struts2.ServletActionContext; ProductAction ActionSupport { serialVersionUID = 3795048906805728732L; ProductManager productManager; Product product; String queryString; setQueryString(String queryString) { .queryString = queryString; } Product getProduct() { product; } setProduct(Product product) { .product = product; } setProductManager(ProductManager productManager) { .productManager = productManager; } String insert() { productManager.insertProduct(product); SUCCESS; } String search() { List results = productManager.searchProducts(queryString); System.out.println(results.size()); ServletActionContext.getRequest() .setAttribute("searchresults",results); SUCCESS; } }
com.v512.example.dao; java.util.List; com.v512.example.model.Product; ProductDao { create(Product p); Product getProduct(String id); List getProducts(); update(Product product); remove(String id); }
com.v512.example.dao.hibernate; java.util.List; com.v512.example.dao.ProductDao; com.v512.example.model.Product; org.springframework.orm.hibernate3.support.HibernateDaoSupport; ProductDaoHibernate HibernateDaoSupport ProductDao { create(Product p) { getHibernateTemplate().save(p); } Product getProduct(String id) { (Product) getHibernateTemplate().get(Product.,id); } List getProducts() { getHibernateTemplate().find("from Product order by id desc"); } remove(String id) { getHibernateTemplate().delete(getProduct(id)); } update(Product product) { getHibernateTemplate().saveOrUpdate(product); } }
实体类中注入compass
com.v512.example.model; org.compass.annotations.*; @Searchable Product java.io.Serializable { 实体类中注入compass @SearchableId String id; @SearchableProperty(name = "name") String name; @SearchableProperty(name = "price") Double price; @SearchableProperty(name = "brand") String brand; @SearchableProperty(name = "description") String description; Product() { } Product(String name,Double price,String brand,String description) { .name = name; .price = price; .brand = brand; .description = description; } String getId() { .id; } setId(String id) { .id = id; } String getName() { .name; } setName(String name) { .name = name; } Double getPrice() { .price; } setPrice(Double price) { .price = price; } String getBrand() { .brand; } setBrand(String brand) { .brand = brand; } String getDescription() { .description; } setDescription(String description) { .description = description; } }
.sourceforge.net/hibernate-mapping-3.0.dtd" ott" java.lang.String" java.lang.String" java.lang.Double" java.lang.String" java.lang.String"
com.v512.example.service; java.util.List; com.v512.example.model.Product; ProductManager { insertProduct(Product p); Product findProdcut(String id); List searchProducts(String queryString); }
com.v512.example.service.impl; org.compass.gps.CompassGps; org.springframework.beans.factory.InitializingBean; 自动随Spring ApplicationContext启动而重建索引的Builder. 调用compassGps.index()函数. 默认会在Web应用每次启动时重建索引,可以设置buildindex属性为false来禁止此功能. 也可以不用本Builder, 调用compassGps.index()的代码. CompassIndexBuilder InitializingBean { buildindex = ; lazyTime = 10; CompassGps compassGps; Thread indexThread = Thread() { @Override run() { { Thread.sleep(lazyTime * 1000); System.out.println("begin compass index..."); beginTime = System.currentTimeMillis(); 文件已存在,索引过程中会建立临时索引, compassGps.index(); costTime = System.currentTimeMillis() - beginTime; System.out.println("compss index finished."); System.out.println("costed " + costTime + " milliseconds"); } (InterruptedException e) { e.printstacktrace(); } } }; InitializingBean接口,在完成注入后调用启动索引线程. afterPropertiesSet() Exception { (buildindex) { indexThread.setDaemon(); indexThread.setName("Compass Indexer"); indexThread.start(); } } setBuildindex( buildindex) { .buildindex = buildindex; } setLazyTime( lazyTime) { .lazyTime = lazyTime; } setCompassGps(CompassGps compassGps) { .compassGps = compassGps; } }
com.v512.example.service.impl; java.util.ArrayList; java.util.List; org.compass.core.CompassHits; org.compass.core.CompassSession; org.compass.core.Compasstemplate; org.compass.core.Compasstransaction; com.v512.example.dao.ProductDao; com.v512.example.model.Product; com.v512.example.service.ProductManager; org.compass.core.Compass; ProductManagerImpl ProductManager { ProductDao productDao; Compasstemplate compasstemplate; setCompasstemplate(Compasstemplate compasstemplate) { .compasstemplate = compasstemplate; } setProductDao(ProductDao productDao) { .productDao = productDao; } Product findProdcut(String id) { productDao.getProduct(id); } insertProduct(Product p) { productDao.create(p); } List searchProducts(String queryString) { Compass compass = compasstemplate.getCompass(); CompassSession session = compass.openSession(); List list = ArrayList(); CompassHits hits = session.queryBuilder().queryString( "name:" + queryString).toQuery().hits(); System.out.println("queryString:" + queryString); System.out.println("hits:" + hits.getLength()); ( i = 0; i < hits.length(); i++) { Product hit = (Product) hits.data(i); list.add(hit); } list; } Compasstemplate getCompasstemplate() { compasstemplate; } }
paoding.dic.home=C:/paoding/dicpaoding.dic.detector.interval=60
figuration 2.0//EN" insertOk.jsp searchResults.jsp
broot/product/input.jsp