边学边写,直接做一个简单的实例吧,这个实例将包含以下内容:
1
.如何将数据库里的数据,建成搜索引擎里的索引(全量创建索引)。
2
.数据库表数据有变化时,如何实时更新索引(实时索引)。
3
.索引建完之后,如何进行搜索(搜索)。
|
本节的主要内容:构建项目
本章内容可忽略,不想自己搭建过程可直接从GitHub下载代码:
HTTPS:https://github.com/mcj2761358/marry.git
SSH : git@github.com:mcj2761358/marry.git
一、使用Intellij IDEA创建一个工程
1.创建一个Maven工程
2.填写groupId和artifactId
3.填写Project信息
工程创建完成,结构如下:
二、给marry工程创建模块
用于学习,我们创建两个模块,一个模块为业务模块(marry.biz),另一个模块包含对外提供接口和一些管理页面(marry.webapp)
2.1创建业务模块
下一步
下一步
当前整个项目的架构如下:
2.2 接下来建webapp模块,这个模块的创建和前面两个有一点点不一样,他在创建的时候需要指定一个webapp的模板。
下一步
下一步
下一步
此时,项目的结构如下:
最外层的src目录我们并不需要,直接删掉吧。在webapp模块里,还需要建一个java目录,用于我们写Controller层的代码。
最后,我们的webapp模块是依赖于biz模块的,所以需要在webapp的pom文件把biz引进来,打开marry.webapp的pom文件,引入biz
<project xmlns=
"<a href="http://maven.apache.org/POM/4.0.0" "="">http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"<a href="http://www.w3.org/2001/XMLSchema-instance" "="">http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 <a href="http://maven.apache.org/maven-v4_0_0.xsd" "="">http://maven.apache.org/maven-v4_0_0.xsd"
>
<parent>
<artifactId>marry</artifactId>
<groupId>com.tqmall.search</groupId>
<version>
1.0
-SNAPSHOT</version>
</parent>
<modelVersion>
4.0
.
0
</modelVersion>
<artifactId>marry.webapp</artifactId>
<packaging>war</packaging>
<name>marry.webapp Maven Webapp</name>
<url>http:
//maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>
3.8
.
1
</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tqmall.search</groupId>
<artifactId>marry.biz</artifactId>
<version>
1.0
-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>marry.webapp</finalName>
</build>
</project>
|
三、文件配置
3.1 在marry项目的pom文件里引用我们需要用到的第三方jar包
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
project
xmlns
=
"<a href="http://maven.apache.org/POM/4.0.0" "="">http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"<a href="http://www.w3.org/2001/XMLSchema-instance" "="">http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://maven.apache.org/POM/4.0.0 <a href="http://maven.apache.org/xsd/maven-4.0.0.xsd" "="">http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<
modelVersion
>4.0.0</
modelVersion
>
<
groupId
>com.tqmall.search</
groupId
>
<
artifactId
>marry</
artifactId
>
<
packaging
>pom</
packaging
>
<
version
>1.0-SNAPSHOT</
version
>
<
modules
>
<
module
>marry.biz</
module
>
<
module
>marry.webapp</
module
>
</
modules
>
<
properties
>
<
es.version
>1.4.1</
es.version
>
<
springframework-version
>4.0.1.RELEASE</
springframework-version
>
<
mysql-connector-version
>5.1.29</
mysql-connector-version
>
<
json-version
>2.3.2</
json-version
>
</
properties
>
<
dependencies
>
<!-- elasticsearch API -->
<
dependency
>
<
groupId
>org.elasticsearch</
groupId
>
<
artifactId
>elasticsearch</
artifactId
>
<
version
>${es.version}</
version
>
</
dependency
>
<!-- spring 相关 -->
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-web</
artifactId
>
<
version
>${springframework-version}</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-webmvc</
artifactId
>
<
version
>${springframework-version}</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-core</
artifactId
>
<
version
>${springframework-version}</
version
>
</
dependency
>
<!-- canal mvn 里有这个包 -->
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring</
artifactId
>
<
version
>2.5.6</
version
>
<
scope
>provided</
scope
>
</
dependency
>
<!-- Database -->
<
dependency
>
<
groupId
>mysql</
groupId
>
<
artifactId
>mysql-connector-java</
artifactId
>
<
version
>${mysql-connector-version}</
version
>
<
scope
>runtime</
scope
>
</
dependency
>
<!-- Spring自动处理JSON -->
<
dependency
>
<
groupId
>com.fasterxml.jackson.core</
groupId
>
<
artifactId
>jackson-core</
artifactId
>
<
version
>${json-version}</
version
>
</
dependency
>
<
dependency
>
<
groupId
>com.fasterxml.jackson.core</
groupId
>
<
artifactId
>jackson-annotations</
artifactId
>
<
version
>${json-version}</
version
>
</
dependency
>
<
dependency
>
<
groupId
>com.fasterxml.jackson.core</
groupId
>
<
artifactId
>jackson-databind</
artifactId
>
<
version
>${json-version}</
version
>
</
dependency
>
<!-- Servlet环境 -->
<
dependency
>
<
groupId
>javax.servlet</
groupId
>
<
artifactId
>javax.servlet-api</
artifactId
>
<
version
>3.0.1</
version
>
<
scope
>provided</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>javax.servlet.jsp</
groupId
>
<
artifactId
>javax.servlet.jsp-api</
artifactId
>
<
version
>2.2.1</
version
>
<
scope
>provided</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>javax.servlet.jsp.jstl</
groupId
>
<
artifactId
>javax.servlet.jsp.jstl-api</
artifactId
>
<
version
>1.2.1</
version
>
</
dependency
>
<!-- Commons -->
<
dependency
>
<
groupId
>commons-beanutils</
groupId
>
<
artifactId
>commons-beanutils</
artifactId
>
<
version
>1.9.1</
version
>
<
exclusions
>
<
exclusion
>
<
groupId
>commons-logging</
groupId
>
<
artifactId
>commons-logging</
artifactId
>
</
exclusion
>
</
exclusions
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.commons</
groupId
>
<
artifactId
>commons-lang3</
artifactId
>
<
version
>3.3</
version
>
</
dependency
>
<
dependency
>
<
groupId
>cglib</
groupId
>
<
artifactId
>cglib-nodep</
artifactId
>
<
version
>3.1</
version
>
</
dependency
>
<
dependency
>
<
groupId
>commons-httpclient</
groupId
>
<
artifactId
>commons-httpclient</
artifactId
>
<
version
>3.1</
version
>
</
dependency
>
<!-- canal -->
<
dependency
>
<
groupId
>com.alibaba.otter</
groupId
>
<
artifactId
>canal.client</
artifactId
>
<
version
>1.0.17</
version
>
<
exclusions
>
<
exclusion
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring</
artifactId
>
</
exclusion
>
</
exclusions
>
</
dependency
>
<
dependency
>
<
groupId
>org.projectlombok</
groupId
>
<
artifactId
>lombok</
artifactId
>
<
version
>1.14.4</
version
>
</
dependency
>
</
dependencies
>
</
project
>
|
3.2 配置webapp模块里的build工具,pom文件如下:
<
project
xmlns
=
"<a href="http://maven.apache.org/POM/4.0.0" "="">http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"<a href="http://www.w3.org/2001/XMLSchema-instance" "="">http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://maven.apache.org/POM/4.0.0 <a href="http://maven.apache.org/maven-v4_0_0.xsd" "="">http://maven.apache.org/maven-v4_0_0.xsd"
>
<
parent
>
<
artifactId
>marry</
artifactId
>
<
groupId
>com.tqmall.search</
groupId
>
<
version
>1.0-SNAPSHOT</
version
>
</
parent
>
<
modelVersion
>4.0.0</
modelVersion
>
<
artifactId
>marry.webapp</
artifactId
>
<
packaging
>war</
packaging
>
<
name
>marry.webapp Maven Webapp</
name
>
<
dependencies
>
<
dependency
>
<
groupId
>junit</
groupId
>
<
artifactId
>junit</
artifactId
>
<
version
>3.8.1</
version
>
<
scope
>test</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>com.tqmall.search</
groupId
>
<
artifactId
>marry.biz</
artifactId
>
<
version
>1.0-SNAPSHOT</
version
>
</
dependency
>
</
dependencies
>
<
build
>
<
finalName
>marry.webapp</
finalName
>
<
plugins
>
<
plugin
>
<
groupId
>org.apache.maven.plugins</
groupId
>
<
artifactId
>maven-compiler-plugin</
artifactId
>
<
version
>3.1</
version
>
<
configuration
>
<
source
>1.7</
source
>
<
target
>1.7</
target
>
<
encoding
>UTF-8</
encoding
>
</
configuration
>
</
plugin
>
<
plugin
>
<
groupId
>org.codehaus.mojo</
groupId
>
<
artifactId
>exec-maven-plugin</
artifactId
>
<
version
>1.2.1</
version
>
<
executions
>
<
execution
>
<
goals
>
<
goal
>java</
goal
>
</
goals
>
</
execution
>
</
executions
>
<
configuration
>
<
mainClass
>com.tqmall.core.CoreApplication</
mainClass
>
</
configuration
>
</
plugin
>
<
plugin
>
<
groupId
>org.apache.maven.plugins</
groupId
>
<
artifactId
>maven-surefire-plugin</
artifactId
>
<
version
>2.16</
version
>
<
configuration
>
<
junitArtifactName
>junit:junit</
junitArtifactName
>
<
includes
>
<
include
>**/*Test.java</
include
>
</
includes
>
</
configuration
>
</
plugin
>
<
plugin
>
<
groupId
>org.apache.maven.plugins</
groupId
>
<
artifactId
>maven-eclipse-plugin</
artifactId
>
<
version
>2.9</
version
>
<
configuration
>
<
downloadSources
>true</
downloadSources
>
<
downloadJavadocs
>false</
downloadJavadocs
>
<
wtpversion
>2.0</
wtpversion
>
<
sourceExcludes
>
<
sourceExclude
>**/.svn/**</
sourceExclude
>
</
sourceExcludes
>
</
configuration
>
</
plugin
>
<
plugin
>
<
groupId
>org.mortbay.jetty</
groupId
>
<
artifactId
>jetty-maven-plugin</
artifactId
>
<
version
>7.2.0.v20101020</
version
>
</
plugin
>
<
plugin
>
<
groupId
>org.apache.tomcat.maven</
groupId
>
<
artifactId
>tomcat7-maven-plugin</
artifactId
>
<
version
>2.2</
version
>
<
configuration
>
<
path
>/marry</
path
>
<
uriEncoding
>UTF-8</
uriEncoding
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
</
project
>
|
3.3 web.xml配置
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"2.4"
xmlns
=
"<a href="http://java.sun.com/xml/ns/j2ee" "="">http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
=
"<a href="http://www.w3.org/2001/XMLSchema-instance" "="">http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
<a href="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" "="">http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<
display-name
>Archetype Created Web Application</
display-name
>
<
context-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>/WEB-INF/spring.xml</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
</
listener
>
<!-- 字符编码过滤器 -->
<
filter
>
<
filter-name
>encodingFilter</
filter-name
>
<
filter-class
>org.springframework.web.filter.CharacterEncodingFilter</
filter-class
>
<
init-param
>
<
param-name
>encoding</
param-name
>
<
param-value
>UTF-8</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>forceEncoding</
param-name
>
<
param-value
>true</
param-value
>
</
init-param
>
</
filter
>
<
filter-mapping
>
<
filter-name
>encodingFilter</
filter-name
>
<
servlet-name
>/*</
servlet-name
>
</
filter-mapping
>
<!-- Enables use of HTTP methods PUT and DELETE,默认参数名: _method -->
<
filter
>
<
filter-name
>httpMethodFilter</
filter-name
>
<
filter-class
>org.springframework.web.filter.HiddenHttpMethodFilter</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>httpMethodFilter</
filter-name
>
<
servlet-name
>elasticsearch</
servlet-name
>
</
filter-mapping
>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<
servlet
>
<
servlet-name
>elasticsearch</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
init-param
>
<
param-name
>servletConfigLocation</
param-name
>
<
param-value
>classpath:elasticsearch-servlet.xml</
param-value
>
</
init-param
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<!-- Map all *.spring requests to the DispatcherServlet for handling -->
<
servlet-mapping
>
<
servlet-name
>elasticsearch</
servlet-name
>
<
url-pattern
>/*</
url-pattern
>
</
servlet-mapping
>
<!-- 欢迎页面 -->
<
welcome-file-list
>
<
welcome-file
>index.jsp</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
|
3.4 spring.xml配置
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"<a href="http://www.springframework.org/schema/beans" "="">http://www.springframework.org/schema/beans"
xmlns:xsi=
"<a href="http://www.w3.org/2001/XMLSchema-instance" "="">http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-4.0.xsd
">
<!-- 配置为空 -->
</beans>
|
3.5 elasticsearch-servlet.xml
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"<a href="http://www.springframework.org/schema/beans" "="">http://www.springframework.org/schema/beans"
xmlns:xsi=
"<a href="http://www.w3.org/2001/XMLSchema-instance" "="">http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc=
"<a href="http://www.springframework.org/schema/mvc" "="">http://www.springframework.org/schema/mvc"
xmlns:context=
"<a href="http://www.springframework.org/schema/context" "="">http://www.springframework.org/schema/context"
xmlns:task=
"<a href="http://www.springframework.org/schema/task" "="">http://www.springframework.org/schema/task"
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.xsd http://www.springframework.org/schema/task <a href="http://www.springframework.org/schema/task/spring-task.xsd" "="">http://www.springframework.org/schema/task/spring-task.xsd">
<!-- 启用 spring 注解 -->
<context:annotation-config />
<task:annotation-driven/>
<!-- 扫描控制器类 -->
<context:component-scan base-
package
=
"com.tqmall"
/>
<!-- 采用注解方式配置MVC -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults=
"true"
>
<!-- 将StringHttpMessageCOnverter的默认编码设为UTF-
8
-->
<bean
class
=
"org.springframework.http.converter.StringHttpMessageConverter"
>
<constructor-arg value=
"UTF-8"
/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
|
至此,项目构建完毕