前面幾篇文章簡單的描述了Neo4j的安裝、配置及導數的內容,這篇就來寫下實際項目中如何整合應用Neo4j。下面要整合的是SpringBoot+Mybatis+Druid+Neo4j這樣的一個框架。廢話不多說,直接開始吧(這里我用的是2018.1.8版本的IntelliJ IDEA、jdk1.8)。
第一步:創建一個SpringBoot的WebService項目。
打開IDEA點擊Create New Project。
選擇Spring Initializr,再點擊Next下一步。
填寫好項目信息后點Next
選擇Web,勾選SpringWebServices,之后點擊Next
最后點擊Finish,初始的SpringBoot webservice項目就完成了
此后需要等待一小段時間讓它構建完成。
完成之后其目錄結構如上圖所示,java包下有SpringBoot的啟動類,resources下有個SpringBoot的application.propertis配置文件,而我喜歡用yml就把它改成了yml,這個視個人喜好而定。
下面可以先完善一下項目的包結構,我的包結構如下所示:
第二步:添加依賴包
需要手動添加的只有三個包,分別是Neo4j 驅動、mybatis還有druid連接池,完整POM如下:
-
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>4.0.0
</modelVersion>
-
<parent>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-parent
</artifactId>
-
<version>2.2.7.RELEASE
</version>
-
<relativePath/>
<!-- lookup parent from repository -->
-
</parent>
-
<groupId>com.minant
</groupId>
-
<artifactId>mant
</artifactId>
-
<version>0.0.1-SNAPSHOT
</version>
-
<name>minant-service-neo4j
</name>
-
<description>springBoot+mybatis+druid+neo4j
</description>
-
-
<properties>
-
<java.version>1.8
</java.version>
-
</properties>
-
-
<dependencies>
-
<!--構建項目時添加的包-->
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-web-services
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-test
</artifactId>
-
<scope>test
</scope>
-
<exclusions>
-
<exclusion>
-
<groupId>org.junit.vintage
</groupId>
-
<artifactId>junit-vintage-engine
</artifactId>
-
</exclusion>
-
</exclusions>
-
</dependency>
-
<!--手動添加-->
-
<!--neo4j-jdbc-driver-->
-
<dependency>
-
<groupId>org.neo4j
</groupId>
-
<artifactId>neo4j-jdbc-driver
</artifactId>
-
<version>3.4.0
</version>
</dependency>
-
<!--mybatis-spring-boot-->
-
<dependency>
-
<groupId>org.mybatis.spring.boot
</groupId>
-
<artifactId>mybatis-spring-boot-starter
</artifactId>
-
<version>1.3.2
</version>
-
</dependency>
-
<!-- 連接池-->
-
<dependency>
-
<groupId>com.alibaba
</groupId>
-
<artifactId>druid-spring-boot-starter
</artifactId>
-
<version>1.1.10
</version>
-
</dependency>
-
</dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-maven-plugin
</artifactId>
-
</plugin>
-
</plugins>
-
</build>
-
-
</project>
第三步:yml配置
有服務端口、名稱、數據源連接池等,賦上yml文件:
-
server:
-
port:
8081
-
servlet:
-
context-path:
/
-
spring:
-
application:
-
name:
service-neo4j
-
http:
-
encoding:
-
charset:
UTF-8
-
enabled:
true
-
force:
true
-
datasource:
-
type:
com.alibaba.druid.pool.DruidDataSource
-
driverClassName:
org.neo4j.jdbc.bolt.BoltDriver
-
url:
jdbc:neo4j:bolt://localhost:7687
-
username:
neo4j
-
password:
root
-
# 數據源其他配置
-
initialSize:
5
-
minIdle:
5
-
maxActive:
20
-
maxWait:
60000
-
timeBetweenEvictionRunsMillis:
60000
-
minEvictableIdleTimeMillis:
300000
-
# validation-query: match (n) return id(n) limit 2
-
testWhileIdle:
false
-
testOnBorrow:
false
-
testOnReturn:
false
-
poolPreparedStatements:
true
-
filters:
stat,wall,log4j
-
maxPoolPreparedStatementPerConnectionSize:
20
-
useGlobalDataSourceStat:
true
-
connectionProperties:
druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
-
mybatis:
-
mapper-locations:
classpath*:/mapper/*.xml
-
config-location:
classpath:mybatis-config.xml
第四步:編寫代碼
這里編寫代碼就與一般SMM項目的編寫大同小異了,只不過XML中的SQL要寫成查詢Neo4j用的Cypher語句就行了。如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
<mapper namespace="com.minant.mant.mapper.MyNeo4jMapper">
-
<select id="getAll" resultType="com.minant.mant.model.MovieInfoModel">
-
MATCH (people:Movie)
-
RETURN
-
id(people) as id,
-
people.title as title,
-
people.tagline as tagline,
-
people.released as released LIMIT 10
-
</select>
-
-
<insert id="addMovie" parameterType="com.minant.mant.form.MovieInfoForm">
-
create(people:Movie{title:#{title},tagline:#{tagline},released:#{released}})
-
</insert>
-
-
<update id="updateMovie" parameterType="com.minant.mant.form.MovieInfoForm">
-
match (people:Movie)
-
where people.title = #{title}
-
set people.tagline=#{tagline},people.released=#{released}
-
</update>
-
-
<delete id="delMovie" parameterType="com.minant.mant.form.MovieInfoForm">
-
match (people:Movie)
-
where people.title = #{title} delete people
-
</delete>
-
</mapper>
第五步:接口驗證
本DEMO中的測試數據為Neo4j Cypher學習中的 Movie Graph數據:
接口定義如下:
-
package com.minant.mant.controller;
-
-
import com.minant.mant.form.MovieInfoForm;
-
import com.minant.mant.model.MovieInfoModel;
-
import com.minant.mant.service.neo4j.Neo4jService;
-
import org.springframework.beans.factory.
annotation.Autowired;
-
import org.springframework.web.bind.
annotation.PostMapping;
-
import org.springframework.web.bind.
annotation.RequestBody;
-
import org.springframework.web.bind.
annotation.RequestMapping;
-
import org.springframework.web.bind.
annotation.RestController;
-
-
import java.util.List;
-
/**
-
* @author : MinAnt
-
* @ClassName : MyNeo4jController
-
* @Description :
-
* @date : 2020/5/14
-
**/
-
-
@RestController
-
public
class MyNeo4jController {
-
-
@Autowired
-
private Neo4jService neo4jService;
-
-
/**
-
* 查詢所有電影節點信息
-
* */
-
@RequestMapping("/getAll")
-
public List<MovieInfoModel> getAll() {
-
List<MovieInfoModel> models = neo4jService.getAll();
-
return models;
-
}
-
-
/**
-
* 新增電影節點
-
* */
-
@RequestMapping("/add")
-
public int add(MovieInfoForm form) {
-
form = new MovieInfoForm();
-
form.setTitle(
"MinAntMovie");
-
form.setReleased(
"2020");
-
form.setTagline(
"my test add");
-
int i = neo4jService.addMovie(form);
-
return i;
-
}
-
-
/**
-
* 修改電影節點
-
* */
-
@RequestMapping("/update")
-
public int update(MovieInfoForm form) {
-
form = new MovieInfoForm();
-
form.setTitle(
"MinAntMovie");
-
form.setReleased(
"2050");
-
form.setTagline(
"my test update");
-
int i = neo4jService.updateMovie(form);
-
return i;
-
}
-
/**
-
* 刪除電影節點
-
* */
-
@RequestMapping("/delMovie")
-
public int delMovie(MovieInfoForm form) {
-
form = new MovieInfoForm();
-
form.setTitle(
"MinAntMovie");
-
int i = neo4jService.delMovie(form);
-
return i;
-
}
-
-
}
-
調用接口結果如下:
至此項目整合完成!