加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring+hibernate+struts2+compass整合

发布时间:2020-12-15 01:52:48 所属栏目:大数据 来源:网络整理
导读:======================================================= ======================================================= `product` ( `id` ( ) AUTO_INCREMENT, `name` ( ) COLLATE utf8_unicode_ci , `price` ( , ) , `brand` ( ) COLLATE utf8_unicode_ci ,

=======================================================

=======================================================

`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;

=======================================================

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); } }

com.v512.example.model; org.compass.annotations.*; @Searchable Product java.io.Serializable { @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; } }

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; 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

insertOk.jsp searchResults.jsp

添加信息 添加商品名称 商品名称 < 商品品牌 < 商品价格 < 商品描述 < <

Insert title here 添加商品成功!

Insert title here

Insert title here no results found. <

classpath:com/v512 /lucene/indexes com.v512.example.model.Product org.compass.spring.transaction.SpringSyncTransactionFactory net.paoding.analysis.analyzer.PaodingAnalyzer hibernateDevice true

org.hibernate.dialect.Oracle9Dialect org.hibernate.cache.EhCacheProvider true true com/v512/example/model/Product.hbm.xml

contextConfigLocation /WEB-INF/applicationContext*.xml org.springframework.web.context.ContextLoaderListener lazyLoadingFilter org.springframework.orm.hibernate3.support.OpenSessionInViewFilter struts2 org.apache.struts2.dispatcher.FilterDispatcher lazyLoadingFilter *.action struts2 /* index.jsp

????????????????????????????????????????????????????????????????????????????? 感谢v512刘伟teacher

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读