当前位置:   article > 正文

Solr案列:从数据库中获取数据添加到Solr索引库中,在Solr索引库中搜索获取数据在页面展示_solr file upload使用

solr file upload使用

一:案列需求:

1)使用技术 springMVC+Spring+Mybatis+solrJ

2) 将 mysql 中的 tb_item 表中的部分业务数据导入到 solr 的索引库中

3) 提供一个搜索页面,在搜索页面中完成数据搜索

二:使用软件:IDEA。使用MAVEN管理项目。

先搭建环境

1.SolrParent:pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <packaging>pom</packaging>
  7. <groupId>com.bjsxt</groupId>
  8. <artifactId>SolrParent</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <!-- 对依赖的jar包的版本统一进行定义 -->
  11. <properties>
  12. <solrj.version>4.10.3</solrj.version>
  13. <jedis.version>2.9.0</jedis.version>
  14. <junit.version>4.12</junit.version>
  15. <spring.version>4.1.3.RELEASE</spring.version>
  16. <mybatis.version>3.2.8</mybatis.version>
  17. <mybatis.spring.version>1.2.2</mybatis.spring.version>
  18. <mysql.version>5.1.32</mysql.version>
  19. <slf4j.version>1.6.4</slf4j.version>
  20. <druid.version>1.0.9</druid.version>
  21. <jstl.version>1.2</jstl.version>
  22. <servlet-api.version>2.5</servlet-api.version>
  23. <tomcat.version>2.2</tomcat.version>
  24. <jsp-api.version>2.0</jsp-api.version>
  25. <zkClient-version>0.10</zkClient-version>
  26. <dubbo-version>2.5.4</dubbo-version>
  27. <jackson.version>2.4.2</jackson.version>
  28. <commons-net.version>3.3</commons-net.version>
  29. <commons-fileupload.version>1.3.1</commons-fileupload.version>
  30. </properties>
  31. <!-- jar包的依赖注入 ,由于该工程是一个父工程,所以jar包在该pom文件中只是声明 -->
  32. <dependencyManagement>
  33. <dependencies>
  34. <dependency>
  35. <groupId>org.apache.solr</groupId>
  36. <artifactId>solr-solrj</artifactId>
  37. <version>${solrj.version}</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>redis.clients</groupId>
  41. <artifactId>jedis</artifactId>
  42. <version>${jedis.version}</version>
  43. </dependency>
  44. <!-- 单元测试 -->
  45. <dependency>
  46. <groupId>junit</groupId>
  47. <artifactId>junit</artifactId>
  48. <version>${junit.version}</version>
  49. </dependency>
  50. <!-- 日志处理 -->
  51. <dependency>
  52. <groupId>org.slf4j</groupId>
  53. <artifactId>slf4j-log4j12</artifactId>
  54. <version>${slf4j.version}</version>
  55. </dependency>
  56. <!-- Mybatis -->
  57. <dependency>
  58. <groupId>org.mybatis</groupId>
  59. <artifactId>mybatis</artifactId>
  60. <version>${mybatis.version}</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.mybatis</groupId>
  64. <artifactId>mybatis-spring</artifactId>
  65. <version>${mybatis.spring.version}</version>
  66. </dependency>
  67. <!-- MySql -->
  68. <dependency>
  69. <groupId>mysql</groupId>
  70. <artifactId>mysql-connector-java</artifactId>
  71. <version>${mysql.version}</version>
  72. </dependency>
  73. <!-- 连接池 -->
  74. <dependency>
  75. <groupId>com.alibaba</groupId>
  76. <artifactId>druid</artifactId>
  77. <version>${druid.version}</version>
  78. </dependency>
  79. <!-- Spring -->
  80. <dependency>
  81. <groupId>org.springframework</groupId>
  82. <artifactId>spring-context</artifactId>
  83. <version>${spring.version}</version>
  84. </dependency>
  85. <dependency>
  86. <groupId>org.springframework</groupId>
  87. <artifactId>spring-beans</artifactId>
  88. <version>${spring.version}</version>
  89. </dependency>
  90. <dependency>
  91. <groupId>org.springframework</groupId>
  92. <artifactId>spring-webmvc</artifactId>
  93. <version>${spring.version}</version>
  94. </dependency>
  95. <dependency>
  96. <groupId>org.springframework</groupId>
  97. <artifactId>spring-jdbc</artifactId>
  98. <version>${spring.version}</version>
  99. </dependency>
  100. <dependency>
  101. <groupId>org.springframework</groupId>
  102. <artifactId>spring-aspects</artifactId>
  103. <version>${spring.version}</version>
  104. </dependency>
  105. <!-- JSP相关 -->
  106. <dependency>
  107. <groupId>jstl</groupId>
  108. <artifactId>jstl</artifactId>
  109. <version>${jstl.version}</version>
  110. </dependency>
  111. <dependency>
  112. <groupId>javax.servlet</groupId>
  113. <artifactId>servlet-api</artifactId>
  114. <version>${servlet-api.version}</version>
  115. <scope>provided</scope>
  116. </dependency>
  117. <dependency>
  118. <groupId>javax.servlet</groupId>
  119. <artifactId>jsp-api</artifactId>
  120. <version>${jsp-api.version}</version>
  121. <scope>provided</scope>
  122. </dependency>
  123. <!-- 文件上传组件 -->
  124. <dependency>
  125. <groupId>commons-fileupload</groupId>
  126. <artifactId>commons-fileupload</artifactId>
  127. <version>${commons-fileupload.version}</version>
  128. </dependency>
  129. <dependency>
  130. <groupId>commons-net</groupId>
  131. <artifactId>commons-net</artifactId>
  132. <version>${commons-net.version}</version>
  133. </dependency>
  134. <!-- Jackson Json处理工具包 -->
  135. <dependency>
  136. <groupId>com.fasterxml.jackson.core</groupId>
  137. <artifactId>jackson-databind</artifactId>
  138. <version>${jackson.version}</version>
  139. </dependency>
  140. </dependencies>
  141. </dependencyManagement>
  142. <build>
  143. <resources>
  144. <resource>
  145. <directory>src/main/java</directory>
  146. <includes>
  147. <include>**/*.xml</include>
  148. </includes>
  149. </resource>
  150. <resource>
  151. <directory>src/main/resources</directory>
  152. <includes>
  153. <include>**/*.xml</include>
  154. <include>**/*.properties</include>
  155. </includes>
  156. </resource>
  157. </resources>
  158. <!-- tomcat插件,由于子项目不一定每个都是web项目,所以该插件只是声明,并未开启 -->
  159. <pluginManagement>
  160. <plugins>
  161. <!-- 配置Tomcat插件 -->
  162. <plugin>
  163. <groupId>org.apache.tomcat.maven</groupId>
  164. <artifactId>tomcat7-maven-plugin</artifactId>
  165. <version>${tomcat.version}</version>
  166. </plugin>
  167. </plugins>
  168. </pluginManagement>
  169. </build>
  170. </project>

2.SolrZH:pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>SolrParent</artifactId>
  7. <groupId>com.bjsxt</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. <relativePath>../SolrParent/pom.xml</relativePath>
  10. </parent>
  11. <modelVersion>4.0.0</modelVersion>
  12. <groupId>com.bjsxt</groupId>
  13. <version>1.0-SNAPSHOT</version>
  14. <artifactId>SolrZH</artifactId>
  15. <packaging>war</packaging>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.apache.solr</groupId>
  19. <artifactId>solr-solrj</artifactId>
  20. </dependency>
  21. <!-- 单元测试 -->
  22. <dependency>
  23. <groupId>junit</groupId>
  24. <artifactId>junit</artifactId>
  25. </dependency>
  26. <!-- 日志处理 -->
  27. <dependency>
  28. <groupId>org.slf4j</groupId>
  29. <artifactId>slf4j-log4j12</artifactId>
  30. </dependency>
  31. <!-- Mybatis -->
  32. <dependency>
  33. <groupId>org.mybatis</groupId>
  34. <artifactId>mybatis</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.mybatis</groupId>
  38. <artifactId>mybatis-spring</artifactId>
  39. </dependency>
  40. <!-- MySql -->
  41. <dependency>
  42. <groupId>mysql</groupId>
  43. <artifactId>mysql-connector-java</artifactId>
  44. </dependency>
  45. <!-- 连接池 -->
  46. <dependency>
  47. <groupId>com.alibaba</groupId>
  48. <artifactId>druid</artifactId>
  49. </dependency>
  50. <!-- Spring -->
  51. <dependency>
  52. <groupId>org.springframework</groupId>
  53. <artifactId>spring-context</artifactId>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.springframework</groupId>
  57. <artifactId>spring-beans</artifactId>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework</groupId>
  61. <artifactId>spring-webmvc</artifactId>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.springframework</groupId>
  65. <artifactId>spring-jdbc</artifactId>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.springframework</groupId>
  69. <artifactId>spring-aspects</artifactId>
  70. </dependency>
  71. <!-- JSP相关 -->
  72. <dependency>
  73. <groupId>jstl</groupId>
  74. <artifactId>jstl</artifactId>
  75. </dependency>
  76. <dependency>
  77. <groupId>javax.servlet</groupId>
  78. <artifactId>servlet-api</artifactId>
  79. <scope>provided</scope>
  80. </dependency>
  81. <dependency>
  82. <groupId>javax.servlet</groupId>
  83. <artifactId>jsp-api</artifactId>
  84. <scope>provided</scope>
  85. </dependency>
  86. </dependencies>
  87. <build>
  88. <resources>
  89. <resource>
  90. <directory>src/main/java</directory>
  91. <includes>
  92. <include>**/*.xml</include>
  93. </includes>
  94. </resource>
  95. <resource>
  96. <directory>src/main/resources</directory>
  97. <includes>
  98. <include>**/*.xml</include>
  99. <include>**/*.properties</include>
  100. </includes>
  101. </resource>
  102. </resources>
  103. <!-- tomcat插件,由于子项目不一定每个都是web项目,所以该插件只是声明,并未开启 -->
  104. <plugins>
  105. <!-- 配置Tomcat插件 -->
  106. <plugin>
  107. <groupId>org.apache.tomcat.maven</groupId>
  108. <artifactId>tomcat7-maven-plugin</artifactId>
  109. <configuration>
  110. <path>/</path>
  111. <port>8080</port>
  112. </configuration>
  113. </plugin>
  114. </plugins>
  115. </build>
  116. </project>

3.resources:mybatis-->SqlMapperClient.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!-- 配置分页插件 -->
  7. </configuration>

resources:resource-->db.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=root

resources:resource-->resource.properties

  1. SOLR_SERVICE_URL=http://192.168.92.135:8080/solr
  2. SOLR_CLOUD_SERVICE_URL=192.168.92.135:2181,192.168.92.135:2182,192.168.92.135:2183
  3. DEFAULT_COLLECTION=collection2

4.resources:spring:以下代码为图片中顺序

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- 配置解析properties文件的工具类 -->
  10. <context:property-placeholder location="classpath:resource/*.properties"/>
  11. <!-- 配置数据源 dataSource -->
  12. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
  13. <property name="url" value="${jdbc.url}" />
  14. <property name="username" value="${jdbc.username}" />
  15. <property name="password" value="${jdbc.password}" />
  16. <property name="driverClassName" value="${jdbc.driver}" />
  17. <property name="maxActive" value="10" />
  18. <property name="minIdle" value="5" />
  19. </bean>
  20. <!-- 创建mybatis的上下文对象 -->
  21. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
  22. <property name="dataSource">
  23. <ref bean="dataSource"/>
  24. </property>
  25. <property name="configLocation">
  26. <value>classpath:mybatis/SqlMapperClient.xml</value>
  27. </property>
  28. </bean>
  29. <!-- 扫描mybatis的接口与映射配置文件 -->
  30. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  31. <property name="basePackage" value="com.bjsxt.mapper"/>
  32. </bean>
  33. </beans>

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- 扫描bean对象 -->
  10. <context:component-scan base-package="com.bjsxt.service,com.bjsxt.dao"/>
  11. </beans>

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- 整个单机版solr-->
  10. <!--<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
  11. <constructor-arg name="baseURL">
  12. <value>${SOLR_SERVICE_URL}</value>
  13. </constructor-arg>
  14. </bean>-->
  15. <!--整合集群版solr-->
  16. <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
  17. <constructor-arg name="zkHost">
  18. <value>${SOLR_CLOUD_SERVICE_URL}</value>
  19. </constructor-arg>
  20. <property name="defaultCollection">
  21. <value>${DEFAULT_COLLECTION}</value>
  22. </property>
  23. </bean>
  24. </beans>

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. 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
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10. <!-- 配置事物管理器的切面 -->
  11. <bean id="transactionMananger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  12. <property name="dataSource" ref="dataSource"/>
  13. </bean>
  14. <!-- 配置事物传播行为 :其实就是那些方法应该受什么样的事物控制-->
  15. <tx:advice id="advice" transaction-manager="transactionMananger">
  16. <tx:attributes>
  17. <tx:method name="add*" propagation="REQUIRED"/>
  18. <tx:method name="modify*" propagation="REQUIRED"/>
  19. <tx:method name="update*" propagation="REQUIRED"/>
  20. <tx:method name="dorp*" propagation="REQUIRED"/>
  21. <tx:method name="del*" propagation="REQUIRED"/>
  22. <tx:method name="find*" read-only="true"/>
  23. </tx:attributes>
  24. </tx:advice>
  25. <!-- 那些类下的方法需要参与到当前的事物管理中 。配置切点 -->
  26. <aop:config>
  27. <aop:advisor advice-ref="advice" pointcut="execution(* com.bjsxt.service.*.*(..))"/>
  28. </aop:config>
  29. </beans>

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- 包的扫描器主要是扫描@controller -->
  10. <context:component-scan base-package="com.bjsxt.web.controller"/>
  11. <!-- 注册两个新对象 主要是为了来处理springmvc中的其他anntation 如:@requestmapping -->
  12. <mvc:annotation-driven/>
  13. <!-- 视图解析器 -->
  14. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <property name="prefix" value="/WEB-INF/jsp/" /><!-- jsp所在的前缀 -->
  16. <property name="suffix" value=".jsp" />
  17. </bean>
  18. <!-- 配置静态资源映射 -->
  19. <!--<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
  20. <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>-->
  21. </beans>

5.webapp:WEB-INF-->web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. id="WebApp_ID" version="3.0">
  6. <!-- 上下文参数,告诉Spring配置文件路径 -->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:spring/applicationContext-*.xml</param-value>
  10. </context-param>
  11. <listener>
  12. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  13. </listener>
  14. <!-- 配置springmvc -->
  15. <servlet>
  16. <servlet-name>springmvc</servlet-name>
  17. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  18. <init-param>
  19. <param-name>contextConfigLocation</param-name>
  20. <param-value>classpath:spring/springmvc.xml</param-value>
  21. </init-param>
  22. <load-on-startup>1</load-on-startup>
  23. </servlet>
  24. <servlet-mapping>
  25. <servlet-name>springmvc</servlet-name>
  26. <url-pattern>/</url-pattern>
  27. </servlet-mapping>
  28. <filter>
  29. <filter-name>encoding</filter-name>
  30. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  31. <init-param>
  32. <param-name>encoding</param-name>
  33. <param-value>utf-8</param-value>
  34. </init-param>
  35. </filter>
  36. <filter-mapping>
  37. <filter-name>encoding</filter-name>
  38. <url-pattern>/*</url-pattern>
  39. </filter-mapping>
  40. <welcome-file-list>
  41. <welcome-file>index.html</welcome-file>
  42. <welcome-file>index.htm</welcome-file>
  43. <welcome-file>index.jsp</welcome-file>
  44. <welcome-file>default.html</welcome-file>
  45. <welcome-file>default.htm</welcome-file>
  46. <welcome-file>default.jsp</welcome-file>
  47. </welcome-file-list>
  48. </web-app>

 

此图为整个案列的环境

三:实现功能1:从数据库中获取数据添加到Solr索引库中:数据为商品数据

1.pojo-->TbItem

  1. package com.bjsxt.pojo;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. public class TbItem implements Serializable {
  5. private Long id;
  6. private String title;
  7. private String sell_Point;
  8. private Long price;
  9. private Integer num;
  10. private String barcode;
  11. private String image;
  12. private Long cid;
  13. private Byte status;
  14. private Date created;
  15. private Date updated;
  16. public Long getId() {
  17. return id;
  18. }
  19. public void setId(Long id) {
  20. this.id = id;
  21. }
  22. public String getTitle() {
  23. return title;
  24. }
  25. public void setTitle(String title) {
  26. this.title = title == null ? null : title.trim();
  27. }
  28. public String getSell_Point() {
  29. return sell_Point;
  30. }
  31. public void setSell_Point(String sell_Point) {
  32. this.sell_Point = sell_Point;
  33. }
  34. public Long getPrice() {
  35. return price;
  36. }
  37. public void setPrice(Long price) {
  38. this.price = price;
  39. }
  40. public Integer getNum() {
  41. return num;
  42. }
  43. public void setNum(Integer num) {
  44. this.num = num;
  45. }
  46. public String getBarcode() {
  47. return barcode;
  48. }
  49. public void setBarcode(String barcode) {
  50. this.barcode = barcode == null ? null : barcode.trim();
  51. }
  52. public String getImage() {
  53. return image;
  54. }
  55. public void setImage(String image) {
  56. this.image = image == null ? null : image.trim();
  57. }
  58. public Long getCid() {
  59. return cid;
  60. }
  61. public void setCid(Long cid) {
  62. this.cid = cid;
  63. }
  64. public Byte getStatus() {
  65. return status;
  66. }
  67. public void setStatus(Byte status) {
  68. this.status = status;
  69. }
  70. public Date getCreated() {
  71. return created;
  72. }
  73. public void setCreated(Date created) {
  74. this.created = created;
  75. }
  76. public Date getUpdated() {
  77. return updated;
  78. }
  79. public void setUpdated(Date updated) {
  80. this.updated = updated;
  81. }
  82. }

2.mapper-->ItemMapper

  1. package com.bjsxt.mapper;
  2. import com.bjsxt.pojo.TbItem;
  3. import java.util.List;
  4. public interface ItemMapper {
  5. List<TbItem> findAll();
  6. }

mapper-->ItemMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.bjsxt.mapper.ItemMapper">
  4. <select id="findAll" resultType="com.bjsxt.pojo.TbItem">
  5. select * from tb_item
  6. </select>
  7. </mapper>

 

3.service-->ImportItemService----ImportItemServiceImpl

  1. package com.bjsxt.service;
  2. import com.bjsxt.pojo.TbItem;
  3. import java.util.List;
  4. public interface ImportItemService {
  5. void importItem();
  6. }

 

  1. package com.bjsxt.service.impl;
  2. import com.bjsxt.mapper.ItemMapper;
  3. import com.bjsxt.pojo.TbItem;
  4. import com.bjsxt.service.ImportItemService;
  5. import org.apache.solr.client.solrj.impl.CloudSolrServer;
  6. import org.apache.solr.common.SolrInputDocument;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. /**
  12. * 从数据库中获取数据导入索引库
  13. */
  14. @Service
  15. public class ImportItemServiceImpl implements ImportItemService {
  16. @Autowired
  17. private ItemMapper itemMapper;
  18. @Autowired
  19. private CloudSolrServer cloudSolrServer;
  20. @Override
  21. public void importItem() {
  22. try {
  23. //查询数据库所以数据
  24. List<TbItem> list = this.itemMapper.findAll();
  25. //创建文档集合模型
  26. List<SolrInputDocument> result=new ArrayList<>();
  27. //遍历数据并创建索引库文档导入文档中
  28. for(TbItem item:list){
  29. //创建文档
  30. SolrInputDocument document=new SolrInputDocument();
  31. //讲数据库中部分数据放入文档中
  32. document.addField("id",item.getId()+"");
  33. document.addField("item_title",item.getTitle());
  34. document.addField("item_sell_point",item.getSell_Point());
  35. document.addField("item_price",item.getPrice());
  36. document.addField("item_image",item.getImage());
  37. //添加文档数据到文档模型中
  38. result.add(document);
  39. }
  40. this.cloudSolrServer.add(result);
  41. this.cloudSolrServer.commit();
  42. }catch (Exception e){
  43. e.printStackTrace();
  44. }
  45. }
  46. }

3.Controller-->ImportDataController

  1. package com.bjsxt.web.controller;
  2. import com.bjsxt.service.ImportItemService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. /**
  8. * 导入商品数据Controller
  9. */
  10. @Controller
  11. @RequestMapping("/import")
  12. public class ImportDataController {
  13. @Autowired
  14. private ImportItemService importItemService;
  15. @RequestMapping("/importData")
  16. public String importData(){
  17. this.importItemService.importItem();
  18. return "ok";
  19. }
  20. }

4.结果如图:

 

四:实现功能2:在Solr索引库中搜索获取数据在页面展示

1.pojo-->PageResult

  1. package com.bjsxt.pojo;
  2. import java.io.Serializable;
  3. import java.util.List;
  4. public class PageResult implements Serializable {
  5. private List<TbItem> result;//结果集
  6. private Integer indexPage;//当前页
  7. private Long totalPage;//总页数
  8. private Long totalNum;//总条数
  9. public List<TbItem> getResult() {
  10. return result;
  11. }
  12. public void setResult(List<TbItem> result) {
  13. this.result = result;
  14. }
  15. public Integer getIndexPage() {
  16. return indexPage;
  17. }
  18. public void setIndexPage(Integer indexPage) {
  19. this.indexPage = indexPage;
  20. }
  21. public Long getTotalPage() {
  22. return totalPage;
  23. }
  24. public void setTotalPage(Long totalPage) {
  25. this.totalPage = totalPage;
  26. }
  27. public Long getTotalNum() {
  28. return totalNum;
  29. }
  30. public void setTotalNum(Long totalNum) {
  31. this.totalNum = totalNum;
  32. }
  33. }

2.dao-->SolrSearchItemDao----SolrSearchItemDaoImpl

  1. package com.bjsxt.dao;
  2. import com.bjsxt.pojo.PageResult;
  3. import org.apache.solr.client.solrj.SolrQuery;
  4. public interface SolrSearchItemDao {
  5. PageResult searchItem(SolrQuery solrQuery) throws Exception;
  6. }

 

  1. package com.bjsxt.dao.impl;
  2. import com.bjsxt.dao.SolrSearchItemDao;
  3. import com.bjsxt.pojo.PageResult;
  4. import com.bjsxt.pojo.TbItem;
  5. import org.apache.solr.client.solrj.SolrQuery;
  6. import org.apache.solr.client.solrj.impl.CloudSolrServer;
  7. import org.apache.solr.client.solrj.response.QueryResponse;
  8. import org.apache.solr.common.SolrDocument;
  9. import org.apache.solr.common.SolrDocumentList;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Repository;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Map;
  15. /**
  16. * 查询solr
  17. */
  18. @Repository
  19. public class SolrSearchItemDaoImpl implements SolrSearchItemDao {
  20. @Autowired
  21. private CloudSolrServer cloudSolrServer;
  22. @Override
  23. public PageResult searchItem(SolrQuery solrQuery) throws Exception {
  24. //1.执行查询
  25. QueryResponse res = this.cloudSolrServer.query(solrQuery);
  26. //2.得到查询结果集
  27. SolrDocumentList list = res.getResults();
  28. //3.处理结果集
  29. //创建接收页面对象
  30. PageResult page = new PageResult();
  31. //3.1.页面获得总条数
  32. page.setTotalNum(list.getNumFound());
  33. //3.2.获取数据结果集
  34. //创建集合模型
  35. List<TbItem> items=new ArrayList<>();
  36. //获取高亮信息
  37. Map<String ,Map<String,List<String>>> hl=res.getHighlighting();
  38. //模型转换————》把索引库数据(文档)转换成页面接收数据(结果数据)
  39. for(SolrDocument var :list){
  40. //创建商品对象
  41. TbItem item=new TbItem();
  42. //设置每个商品信息的属性
  43. item.setId(Long.parseLong((String)var.get("id")));
  44. //item.setTitle((String)var.get("item_title"));
  45. item.setSell_Point((String)var.get("item_sell_point"));
  46. item.setPrice((Long) var.get("item_price"));
  47. item.setImage((String)var.get("item_image"));
  48. //由于对搜索关键字item_title设为高亮:所以需要对item_title判断是否为关键字,
  49. List<String> h = hl.get(var.get("id")).get("item_title");
  50. String title="";//设置变量title用于代替item_title
  51. if(h!=null && h.size()>0){//是关键字
  52. title = h.get(0);
  53. }else {//不是关键字,无高亮标识
  54. title=(String)var.get("item_title");
  55. }
  56. item.setTitle(title);
  57. //将商品对象放在商品集合中
  58. items.add(item);
  59. }
  60. //将结果放在页面对象中
  61. page.setResult(items);
  62. return page;
  63. }
  64. }

 

3.service-->SearchItemService----SearchItemServiceImpl

  1. package com.bjsxt.service;
  2. import com.bjsxt.pojo.PageResult;
  3. public interface SearchItemService {
  4. PageResult searchItem(String query, Integer page, Integer rows) throws Exception;
  5. }
  1. package com.bjsxt.service.impl;
  2. import com.bjsxt.dao.SolrSearchItemDao;
  3. import com.bjsxt.pojo.PageResult;
  4. import com.bjsxt.service.SearchItemService;
  5. import org.apache.solr.client.solrj.SolrQuery;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. /**
  9. * 完成solr搜索业务
  10. *
  11. *
  12. */
  13. @Service
  14. public class SearchItemServiceImpl implements SearchItemService {
  15. @Autowired
  16. private SolrSearchItemDao solrSearchItemDao;
  17. @Override
  18. public PageResult searchItem(String query, Integer page, Integer rows) throws Exception {
  19. //1.创建查询条件
  20. SolrQuery solrQuery=new SolrQuery();
  21. //2.设置查询条件
  22. //查询关键字
  23. solrQuery.setQuery(query);
  24. //查询域
  25. solrQuery.set("df","item_keywords");
  26. //分页查询
  27. solrQuery.setStart((page-1)*rows);
  28. solrQuery.setRows(rows);
  29. //设置高亮
  30. solrQuery.setHighlight(true);//开启高亮
  31. solrQuery.addHighlightField("item_title");//设置高亮位置
  32. solrQuery.setHighlightSimplePre("<em style='color:red'>");//设置高亮样式
  33. solrQuery.setHighlightSimplePost("</em>");//设置高亮样式
  34. //3.调用solrSearchItemDao
  35. PageResult result = solrSearchItemDao.searchItem(solrQuery);
  36. //补齐dao层未获取数据
  37. //3.3获取当前页
  38. result.setIndexPage(page);
  39. //3.4获取总页数
  40. Long total=result.getTotalNum()/rows;
  41. if(result.getTotalNum()% rows>0){
  42. total++;//总条数除以行数 余数大于0 ,则新加一页
  43. }
  44. result.setTotalPage(total);
  45. return result;
  46. }
  47. }

 

4.Controller-->SearchItemController

  1. package com.bjsxt.web.controller;
  2. import com.bjsxt.pojo.PageResult;
  3. import com.bjsxt.service.SearchItemService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. /**
  10. * 查询商品Controller
  11. */
  12. @Controller
  13. @RequestMapping("/search")
  14. public class SearchItemController {
  15. @Autowired
  16. private SearchItemService searchItemService;
  17. @RequestMapping("/searchItem")
  18. public String SearchItem(String query, @RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "rows",defaultValue = "2") Integer rows, Model model){
  19. try {
  20. PageResult result = this.searchItemService.searchItem(query, page, rows);
  21. model.addAttribute("result",result);
  22. }catch (Exception e){
  23. e.printStackTrace();
  24. }
  25. return "showItem";
  26. }
  27. }

5.jsp-->index.jsp-----showItem.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: Administrator
  4. Date: 2019/10/9
  5. Time: 11:10
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>Title</title>
  12. </head>
  13. <body>
  14. <form action="search/searchItem" method="post">
  15. <input type="text" name="query">
  16. <input type="submit" value="搜索">
  17. </form>
  18. </body>
  19. </html>

 

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: Administrator
  4. Date: 2019/10/9
  5. Time: 17:44
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  10. <html>
  11. <head>
  12. <title>Title</title>
  13. </head>
  14. <body>
  15. <span>当前页:${result.indexPage}</span>
  16. <span>总页数:${result.totalPage}</span>
  17. <span>总条数:${result.totalNum}</span>
  18. <table align="center" border="1">
  19. <c:forEach items="${result.result}" var="item">
  20. <tr>
  21. <td>${item.id}</td>
  22. <td>${item.title }</td>
  23. <td>${item.sell_Point }</td>
  24. <td>${item.price }</td>
  25. <td>${item.image }</td>
  26. </tr>
  27. </c:forEach>
  28. </table>
  29. </body>
  30. </html>

结果如图:

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号