e3mall项目:索引管理模块之一键导入数据到索引库(后台)
e3mall项目:索引管理模块
一、新建子模块(e3-search、e3-search-interface以及e3-search-service),目录结构如下:
二、相关配置文件
(1)pom.xml
e3-search中的pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>e3-parent</artifactId> <groupId>cn.e3mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>e3-search</artifactId> <packaging>pom</packaging> <modules> <module>e3-search-interface</module> <module>e3-search-service</module> </modules> <dependencies> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8084</port> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
e3-search-interface中的pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>e3-search</artifactId> <groupId>cn.e3mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>e3-search-interface</artifactId> <name>e3-search-interface</name> <dependencies> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-manager-entity</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- dubbo相关 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> </dependencies> </project>
e3-search-service中的pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>e3-search</artifactId> <groupId>cn.e3mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>e3-search-service</artifactId> <packaging>war</packaging> <name>e3-search-service</name> <dependencies> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-search-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>cn.e3mall</groupId> <artifactId>e3-manager-dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <!-- solr客户端 --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>${solrj.version}</version> </dependency> </dependencies> <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 --> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
(2)其他配置文件
resources.properties
## solr服务器URL solr.baseURL=http://192.168.25.129:8080/solr/
spring/applicationContext-dao.xml(修改部分)
spring/applicationContext-service.xml(修改部分)
spring/applicationContext-solr.xml(新建配置文件)
web.xml
三、一键导入数据到索引库(代码)
(1)在e3-manager-web中新建 IndexController(注意:在springmvc.xml中引用SearchService的服务)
package cn.e3mall.controller; import cn.e3mall.common.entity.E3Result; import cn.e3mall.search.service.SearchService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * 索引管理 web层 * Author: xushuai * Date: 2018/5/23 * Time: 12:23 * Description: */ @Controller public class IndexController { @Autowired private SearchService searchService; /* * 将数据库数据导入索引库 */ @RequestMapping("/index/item/import") @ResponseBody public E3Result importIndex(){ return searchService.importIndex(); } }
(2)e3-search-interface、e3-search-service
package cn.e3mall.search.service; import cn.e3mall.common.entity.E3Result; /** * 索引管理 业务逻辑层 * @Auther: Administrator * @Date: 2018/5/23 12:26 * @Description: */ public interface SearchService { /** * 将数据库数据导入索引库 * @auther: xushuai * @date: 2018/5/23 12:27 */ E3Result importIndex(); }
package cn.e3mall.search.service.impl; import cn.e3mall.common.entity.E3Result; import cn.e3mall.common.entity.SearchResult; import cn.e3mall.search.mapper.SearchMapper; import cn.e3mall.search.service.SearchService; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.common.SolrInputDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.List; /** * 索引管理 业务逻辑层实现 * Author: xushuai * Date: 2018/5/23 * Time: 12:27 * Description: */ @Service public class SearchServiceImpl implements SearchService { @Autowired private SearchMapper searchMapper; @Autowired private SolrServer solrServer; @Override public E3Result importIndex() { try { //查询商品结果集 List<SearchResult> itemList = searchMapper.getItemList(); //遍历结果集 for(SearchResult result : itemList){ //将结果集转换为document对象,并写入索引库 write(result); } //写入完成,返回成功 return E3Result.ok(); }catch (Exception e){ e.printStackTrace(); //写入失败,返回失败 return E3Result.build(500,null); } } /** * 将商品索引结果集封装为document对象,写入索引库 * @auther: xushuai * @date: 2018/5/23 12:50 * @param: SearchResult * @throws: IOException, SolrServerException */ private void write(SearchResult result) throws IOException, SolrServerException { //创建文档对象 SolrInputDocument document = new SolrInputDocument(); //封装数据到文档对象 document.addField("id", result.getId()); document.addField("item_title", result.getTitle()); document.addField("item_sell_point", result.getSell_point()); document.addField("item_price", result.getPrice()); document.addField("item_image", result.getImage()); document.addField("item_category_name", result.getCategory_name()); //执行写入操作 solrServer.add(document); //提交 solrServer.commit(); } }
(3)SearchMapper、SearchMapper.xml
package cn.e3mall.search.mapper; import cn.e3mall.common.entity.SearchResult; import java.util.List; /** * 索引管理持久层 * Author: xushuai * Date: 2018/5/23 * Time: 12:35 * Description: */ public interface SearchMapper { /** * 查询商品列表 * @auther: xushuai * @date: 2018/5/23 12:42 * @return: 索引库结果对象集合 */ List<SearchResult> getItemList(); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="cn.e3mall.search.mapper.SearchMapper" > <!--查询商品列表,返回结果封装为索引结果对象--> <select id="getItemList" resultType="cn.e3mall.common.entity.SearchResult"> SELECT a.id, a.title, a.sell_point, a.price, a.image, b.`name` category_name FROM tb_item a LEFT JOIN tb_item_cat b ON a.cid = b.id WHERE a. `status` = 1 </select> </mapper>
(4)SearchResult