- 新建web工程
- 导入jar包
- 编写springmvc配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 配置组件扫描 --> <context:component-scan base-package="com.jd.controller" /> <!-- 配置处理器映射器 --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> --> <!-- 配置处理器适配器 --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> --> <!-- 配置处理器映射器 --> <!-- 配置处理器适配器 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
- 编写web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>solrJD</display-name> <!-- 配置springmvc前端控制器 --> <servlet> <servlet-name>spmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 服务器启动时创建servlet实例 --> <load-on-startup>5</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
- 导入静态资源文件
- 建立包结构
- 创建pojo
商品实体类
package com.jd.pojo; public class Product { // 商品编号 private String pid; // 商品名称 private String name; // 商品分类名称 private String catalog_name; // 价格 private float price; // 商品描述 private String description; // 图片名称 private String picture; public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCatalog_name() { return catalog_name; } public void setCatalog_name(String catalog_name) { this.catalog_name = catalog_name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getPicture() { return picture; } public void setPicture(String picture) { this.picture = picture; } }
封装分页实体类
package com.jd.pojo; import java.util.List; public class ResultModel { // 商品列表 private List<Product> list; // 商品总数 private Long recordCount; // 总页数 private Long pageCount; // 当前页 private long curPage; public List<Product> getList() { return list; } public void setList(List<Product> list) { this.list = list; } public Long getRecordCount() { return recordCount; } public void setRecordCount(Long recordCount) { this.recordCount = recordCount; } public Long getPageCount() { return pageCount; } public void setPageCount(Long pageCount) { this.pageCount = pageCount; } public long getCurPage() { return curPage; } public void setCurPage(long curPage) { this.curPage = curPage; } }
- ProductMapper
package com.jd.mapper; import org.apache.solr.client.solrj.SolrQuery; import com.jd.pojo.Product; import com.jd.pojo.ResultModel; public interface ProductMapper { /** * 分页条件查询所有商品 * @param solrQuery * @return * @throws Exception */ public ResultModel<Product> findAll(SolrQuery solrQuery) throws Exception; }
- ProductMapperImpl
package com.jd.mapper; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.springframework.stereotype.Repository; import com.jd.pojo.Product; import com.jd.pojo.ResultModel; @Repository public class ProductMapperImpl implements ProductMapper { @Override public ResultModel<Product> findAll(SolrQuery solrQuery) throws Exception { // 创建连接solr服务器对象 SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1"); // 创建ResultModel对象 ResultModel<Product> results = new ResultModel<Product>(); // 创建集合,存储Product List<Product> productList = new ArrayList<Product>(); // 执行查询 QueryResponse queryResponse = solrServer.query(solrQuery); // 获取Document结果集 SolrDocumentList solrDocumentList = queryResponse.getResults(); // 设置总记录数 results.setRecordCount(solrDocumentList.getNumFound()); // 高亮显示 Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting(); // 遍历结果集 for (SolrDocument doc : solrDocumentList) { // 创建Product对象 Product product = new Product(); // 设置商品的编号 product.setPid((String) doc.get("id")); List<String> list = highlighting.get(doc.get("id")).get("product_name"); if (list == null) { // 设置商品的名称 product.setName((String) doc.get("product_name")); } else { // 设置高亮显示名称 product.setName(list.get(0)); } // 设置商品分类名称 product.setCatalog_name((String) doc.get("product_catalog_name")); // 设置商品价格 product.setPrice((Float) doc.get("product_price")); // 设置商品图片名称 product.setPicture((String) doc.get("product_picture")); // 将商品添加到集合中 productList.add(product); } results.setList(productList); return results; } }
- ProductService
package com.jd.service; import com.jd.pojo.Product; import com.jd.pojo.QueryVo; import com.jd.pojo.ResultModel; public interface ProductService { /** * 分页条件查询所有商品 * * @param solrQuery * @return * @throws Exception */ public ResultModel<Product> findAll(QueryVo queryVo) throws Exception; }
- ProductServiceImpl
package com.jd.service; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrQuery.ORDER; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.jd.mapper.ProductMapper; import com.jd.pojo.Product; import com.jd.pojo.QueryVo; import com.jd.pojo.ResultModel; /** * 实现类 * @author Easong * */ @Service public class ProductServiceImpl implements ProductService { // 每页显示的条数 private final static Integer PAGE_SIZE = 32; @Autowired private ProductMapper productMapper; @Override public ResultModel<Product> findAll(QueryVo queryVo) throws Exception { // 创建solr查询对象 SolrQuery solrQuery = new SolrQuery(); // 设置查询关键字 if (queryVo.getQueryString() != null && !"".equals(queryVo.getQueryString())) { solrQuery.setQuery(queryVo.getQueryString()); } else { solrQuery.setQuery("*:*"); } // 设置过滤查询--商品类别 if (queryVo.getCatalog_name() != null && !"".equals(queryVo.getCatalog_name())) { solrQuery.setFilterQueries("product_catalog_name:" + queryVo.getCatalog_name()); } // 设置价格排序 if ("1".equals(queryVo.getSort())) { // 升序 solrQuery.setSort("product_price", ORDER.asc); } else if("0".equals(queryVo.getSort())){ // 降序 solrQuery.setSort("product_price", ORDER.desc); } // 设置价格查询区间 if (queryVo.getPrice() != null && !"".equals(queryVo.getPrice())) { String[] split = queryVo.getPrice().split("-"); if (split != null && split.length > 1) { solrQuery.setFilterQueries("product_price:[" + split[0] + " TO " + split[1] + "]"); } } // 分页查询 if (queryVo.getPage() == null) { queryVo.setPage(1); } Integer currPage = queryVo.getPage(); // 开始索引 solrQuery.setStart((currPage - 1) * PAGE_SIZE); // 每页显示条数 solrQuery.setRows(PAGE_SIZE); // 设置默认搜索域 solrQuery.set("df", "product_keywords"); // 开启高亮显示 solrQuery.setHighlight(true); // 设置显示域名 solrQuery.addHighlightField("product_name"); // 设置前缀 solrQuery.setHighlightSimplePre("<span style="color:red">"); // 设置后缀 solrQuery.setHighlightSimplePost("</span>"); // 调用Mapper层 ResultModel<Product> resultModel = productMapper.findAll(solrQuery); // 设置当前页 resultModel.setCurPage(currPage.longValue()); // 设置总页数 Double pageSize = Math.ceil(resultModel.getRecordCount().doubleValue() / PAGE_SIZE); resultModel.setPageCount(pageSize.longValue()); return resultModel; } }
- ProductController
package com.jd.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.jd.pojo.Product; import com.jd.pojo.QueryVo; import com.jd.pojo.ResultModel; import com.jd.service.ProductService; @Controller public class ProductController { @Autowired private ProductService productService; @RequestMapping("/list") public String list(QueryVo queryVo, Model model) throws Exception { ResultModel<Product> resultModel = productService.findAll(queryVo); // 将结果存入到model域中 model.addAttribute("list", resultModel); // 查询条件回显 model.addAttribute("queryString", queryVo.getQueryString()); model.addAttribute("catalog_name", queryVo.getCatalog_name()); model.addAttribute("price", queryVo.getPrice()); model.addAttribute("curPage", queryVo.getPage()); model.addAttribute("sort", queryVo.getSort()); return "product_list"; } }
-
- 源码地址以及静态页面 链接: https://pan.baidu.com/s/1miLjfkc 密码: ysks