前面几篇文章简单的描述了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;
}
}
调用接口结果如下:
至此项目整合完成!