锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / java开源 / 基于Hibernate把全文搜索功能集成到Spring MVC
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

基于Hibernate把全文搜索功能集成到Spring MVC


Introduction

When I was working on my first ever blog engine (sorry about this shameful self-promotion) recently, at the very late stage, I discovered a problem -- there is no search capability in this blog engine. I released it out on Digital Ocean as my personal photography site. But I regretted the decision of not having the search function in the application.

最近,当我第一次写博客引擎 (抱歉这可耻的自我推销) 时,在很晚的阶段,发现了一个问题 — — 这个博客引擎有没有搜索功能。我把博客引擎公开到Digital Ocean作为我的个人图片网站。但我感到遗憾的是决定不加入搜索功能。

For my own benefit, I decided to research for a solution to add full text search to the web app. It turned out that there are several approaches available. The obvious two for me are:

为了自己的利益,我决定研究一种添加到 web 应用程序的全文搜索解决方案。原来有几种方法可用。两个对我来说是显而易见的事实:

  • Add a customized SQL dialect for the Hibernate and take advantage of MySQL's (or any other SQL DB) full text search functionality.为Hibernate添加一个自定义的 SQL 方言,充分利用MySQL (或任何其他 SQL DB) 全文搜索功能。
  • Use Hibernate Search and integrate with existing Spring MVC app.使用Hibernate搜索,并与现有的Spring MVC 应用程序集成。

I highly recommended not trying the first approach because it is not portable. Here is the thing, if you follow this approach, you are making a tight couple of your application to a specific relational database. In the event of changing to a different database, you must implement a new dialect.

我强烈建议不用第一种方法,因为它不可移植。还有件事,如果你遵循这种方法,你就和一个特定的关系数据库紧密绑定。一旦更改到不同的数据库,您必须实现一种新方言。

Based on this reasoning, I chose the second approach which was quite a popular approach based on the search results I have received through Google. Regardless of the approaches, I still find it hard to get some good documentations on the type of integration I worked on. This article is intended to track all the information and give the audience a good working copy of the work I have done.

基于这种推理,我选择第二种方法,这是相当流行的方法,基于我收到通过谷歌的搜索结果。无论用何方法,我还是发现很难找得到一些好的文献。这篇文章旨在跟踪所有信息,并给观众提供我的工作副本。

Background on Technologies Used使用的技术背景

Before I get into the details of how the implementation goes, I would like to discuss in brief the technologies I have used for this. The project is a Spring MVC based web application. I used Spring 3 components. The build is done using maven 2. The persistence layer is using Hibernate 4. The back end data store is MySQL. Everything is integrated through Spring.

在进入如何实现的细节之前,我想简要讨论用到的技术。该项目是基于web 应用程序的Spring MVC 。我用的Spring 3 组件。生成使用 maven 2。持久层使用Hibernate 4。后端数据存储是 MySQL。一切都通过Spring集成。

The attached zip file will have all you need to run the web app. What you need to do are: 附带的 zip 文件将有所有你需要运行的 web 应用程序。你需要做的:

  • Install Java 1.6 安装 Java 1.6
  • Install Apache Maven 2 or 3 安装 Apache Maven 2 或 3
  • Install MySQL and install the DB and tables. 安装 MySQL 和安装的数据库和表。
  • Set the environment variables for JAVA_HOME and M2_HOME, and the PATH variable for both. 设置 JAVA_HOME 和 M2_HOME,环境变量和两个路径变量。
  • Run mvn clean install. Once the war file has been created, deploy to your machine and run. 运行mvn clean install。一旦创建了 war 文件,此文件将部署到您的计算机并运行。

Alternatively, you can use Maven to generate the Eclipse project files and import the project into Eclipse for editing. I even run the server in Eclipse for trouble shooting. The main focus of this article is to describe how the implementation of full text search on SQL based DB works, so I will not spend time explaining how to setup the dev environment for this. There are plenty of resources to help you with this problem.

或者,你可以使用 Maven 生成 Eclipse 项目文件并将该项目导入到 Eclipse 进行编辑。我甚至在 Eclipse 中运行服务器,用于故障排除。这篇文章的主要焦点是描述如何基于的 SQL 全文搜索实现 DB 的作品,所以我不会在此花时间来解释如何设置开发环境。有很多资源来帮你解决这个问题。

Understanding the Code 了解代码

I will discuss the project architecture and code structure in detail. First, let me describe what this application does. It is a simple web based application. It allows the user to enter the details of a book: title, description, and author. And it allows the user to search the entered information by 1 simple keyword. As you can see, it is so simple and it does not do much. This is intended. All I want to show you is how to setup the whole thing. How you like to implement full text search functionality using Hibernate Search is up to you to do. Let's go through the steps on setting up this Java project.

我将讨论中详细的项目架构和代码结构。首先,让我描述此应用程序的执行。它是一个基于 web的简单应用程序。它允许用户输入的一本书的详细信息: 标题、 说明和作者。它允许用户通过 1 简单的关键字搜索输入的信息。正如你所看到的就这么简单,它并没有多大。这被为了。如何设置整件事是所有我想要给你看。你怎样想实现全文搜索功能使用Hibernate搜索是由你来做。让我们去通过设置此 Java 项目的步骤。

Create DB User and Tables 创建数据库用户和表

In the zip file, there is a folder called DB, which contains 2 scripts. The first script creates a DB user. The content looks like this:

在 zip 文件中,还有一个名为 DB,包含 2 脚本文件夹。第一个脚本创建一个数据库用户。内容看起来像这样:

CREATE DATABASE fulltextsearch;    
CREATE USER 'ftuser1'@'localhost' IDENTIFIED BY '123test321';
GRANT ALL PRIVILEGES ON fulltextsearch.* TO 'ftuser1'@'localhost';
FLUSH PRIVILEGES;

Run the above script on MySQL with root user, you will create a new user and new database for this project.

使用根用户对 MySQL 运行上面的脚本,您将创建一个新用户和新数据库为这个项目。

Another SQL script is included which will create a new table in the new DB. The content looks like the following:

另一个 包括在内SQL 脚本,将会在新数据库中创建一个新表。内容如下所示:

use fulltextsearch;    
DROP TABLE IF EXISTS book;    
CREATE TABLE book (
id VARCHAR(37) NOT NULL PRIMARY KEY,
title VARCHAR(128) NOT NULL,     
description VARCHAR(256) NOT NULL,
author VARCHAR(64) NOT NULL,     
createdate DATETIME NOT NULL,     
updatedate DATETIME NOT NULL  
);

Again, I won't explain how you will run these two scripts. There should be plenty of resources online to help you out.

再一次,我不会解释你将如何运行这两个脚本。应该有大量的在线资源,为您排忧解难。

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内