目录
第一部分 mac安装neo4j
第二部分 neo4j集成springboot实现基础的增删改查
一、图数据库相关配置
二、业务逻辑
实体类
持久层
业务层
表现层
启动类
三、测试
附录:
1、官网下载:Neo4j Download Center - Neo4j Graph Data Platform
注意:neo4j需要和java版本相对应,jdk1.8.x的应对应Neo4j3.x
2、cd到安装目录下:tar -zxvf neo4j-community-3.5.34-unix.tar.gz
3、cd到解压的文件夹的bin目录下,输入neo4j start,如下就是启动成功了
4、进入浏览器,输入http://localhost:7474/
5、如果想要退出,cd到解压的文件夹的bin目录下,输入neo4j stop即可
6、想要在其他目录下也可启动数据库,配置环境变量
vim ~/.bash_profile
source ~/.bash_profile
1、pom.xml
注意:这里使用的springboot2.3.2.RELEASE,jdk1.8,Neo4j Server version:3.5.28,不同版本可能存在冲突问题,导致程序起不来
org.springframework.boot
spring-boot-starter-parent
2.3.2.RELEASE
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-starter-web
junit
junit
4.12
org.projectlombok
lombok
1.18.22
provided
org.springframework.boot
spring-boot-starter-data-neo4j
org.neo4j
neo4j-ogm-core
3.2.16
2、application.yml(本地的neo4j数据库)
server:
port: 8888
servlet:
context-path: /
spring:
data:
neo4j:
uri: bolt://localhost:7687
username: neo4j
password: 123456
3、目录结构
1、neo4j中节点的实体类——UserNode节点
package com.study.neo.entity;
import lombok.Data;
import org.neo4j.ogm.annotation.*;
import java.util.HashSet;
import java.util.Set;
@NodeEntity(label = "User")
@Data
public class UserNode {
@Id
@GeneratedValue
private Long nodeId;
@Property(name = "name")
private String name;
@Property(name = "age")
private int age;
@Property(name = "sex")
private String sex;
/*注意这个地方:并不是说加了这个注解,然后下面实现了addKnows这个方法在以后调用addKnows
就可以实现关系的增加,这是不对的,我看了很多教程没有说明。这个addKnows方法,只有在节点未被
添加之前,调用该方法添加关系,然后再调用UserService中的create方法,这样关系才会同时被添加到图
数据库中。如果想实现两个已有节点之间增加关系,需要单独实现,即controller中的addKnows方法*/
@Relationship(type = "know" ,direction = Relationship.OUTGOING)
private Set knows = new HashSet<>();
public Boolean addKnows(UserNode to){
return this.knows.add(to);
}
@Override
public String toString() {
return "UserNode{" +
"nodeId=" + nodeId +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
public UserNode() {}
}
2、neo4j中关系的实体类——Know关系
package com.study.neo.entity;
import lombok.Data;
import org.neo4j.ogm.annotation.*;
@RelationshipEntity(type = "know")
@Data
public class Know {
@Id
@GeneratedValue
private Long id;
@StartNode
private UserNode from;
@EndNode
private UserNode to;
@Override
public String toString() {
return "Know{" +
"id=" + id +
", from=" + from +
", to=" + to +
'}';
}
public Know(UserNode from, UserNode to) {
this.from = from;
this.to = to;
}
}
持久层继承Neo4jRepository
UserRepository
package com.study.neo.dao;
import com.study.neo.entity.UserNode;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends Neo4jRepository {
// @Query("MATCH (n:User) RETURN n ")
// List getUserNodeList();
// @Query("create (n:User{name:{name},age:{age},sex:{sex}}) RETURN n ")
// UserNode addUserNode(@Param("name") String name, @Param("age")int age, @Param("sex") String sex);
@Query("MATCH (n) WHERE id(n) = :#{#userNode.nodeId} SET n.name = :#{#userNode.name},n.age = :#{#userNode.age},n.sex = :#{#userNode.sex} RETURN n")
UserNode updateByNode(@Param("userNode") UserNode userNode);
}
KnowRepository
package com.study.neo.dao;
import com.study.neo.entity.Know;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface KnowRepository extends Neo4jRepository {
@Query("MATCH (fromNode) WHERE id(fromNode) = {fromId} MATCH (toNode) WHERE id(toNode) = {toId} MATCH (fromNode)-[r]->(toNode) DELETE r")
void deleteByNodeId(@Param(value = "fromId") long fromId, @Param(value = "toId") long toId);
}
UserService
package com.study.neo.service;
import com.study.neo.entity.Know;
import com.study.neo.entity.UserNode;
import java.util.List;
import java.util.Optional;
public interface UserService {
/**
* @Description 添加user节点
**/
UserNode create(UserNode userNode);
/**
* @Description 根据id删除
**/
void deleteById(Long id);
/**
* @Description 根据id查询
**/
Optional findById(long id);
/**
* @Description 获取所有User节点
**/
List findAll();
/**
* @Description 增加“知道”关系
**/
Know addIKnows(UserNode fromNode, UserNode toNode);
/**
* @Description 修改单个节点
**/
UserNode updateByNode(UserNode userNode);
/**
* @Description 删除两个节点的关系
**/
void deleteKnowByNodeId(long fromId,long toId);
}
UserServiceImpl
package com.study.neo.service.impl;
import com.study.neo.service.UserService;
import com.study.neo.dao.KnowRepository;
import com.study.neo.dao.UserRepository;
import com.study.neo.entity.Know;
import com.study.neo.entity.UserNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private KnowRepository knowRepository;
@Override
public UserNode create(UserNode userNode) {
return userRepository.save(userNode);
}
@Override
public void deleteById(Long id) {
userRepository.deleteById(id);
}
@Override
public Optional findById(long id) {
return userRepository.findById(id);
}
@Override
public List findAll() {
return (List) userRepository.findAll();
}
@Override
public Know addIKnows(UserNode fromNode, UserNode toNode) {
Know newKnow = new Know(fromNode,toNode);
return knowRepository.save(newKnow);
}
@Override
public UserNode updateByNode(UserNode userNode) {
return userRepository.updateByNode(userNode);
}
@Override
public void deleteKnowByNodeId(long fromId, long toId) {
knowRepository.deleteByNodeId(fromId,toId);
}
}
UserController
package com.study.neo.controller;
import com.study.neo.entity.Know;
import com.study.neo.entity.UserNode;
import com.study.neo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
public class UserController {
@Autowired
private UserService userService;
/**
* @Description 添加节点
**/
@RequestMapping(path = "/add", method = RequestMethod.POST)
public UserNode addUserNode(@RequestBody UserNode userNode) {
return userService.create(userNode);
}
/**
* @Description 根据id删除
**/
@RequestMapping(path = "/delete", method = RequestMethod.POST)
public int delUserNodeById(@RequestParam(value = "id") long id) {
userService.deleteById(id);
System.out.println(id);
return 1;
}
/**
* @Description 根据id更新
**/
@RequestMapping(path = "/update", method = RequestMethod.POST)
public UserNode updateUserNodeByNode(@RequestBody UserNode userNode) {
return userService.updateByNode(userNode);
}
/**
* @Description 根据id查询单个节点
**/
@RequestMapping(path = "/get", method = RequestMethod.GET)
public UserNode getUserNodeById(@RequestParam(value = "id") long id) {
Optional optionalUserNode = userService.findById(id);
if (optionalUserNode.isPresent()) {
return optionalUserNode.get();
} else {
return null;
}
}
/**
* @Description 查找所有节点
**/
@RequestMapping(path = "/list", method = RequestMethod.GET)
public List getUserNodeList() {
return userService.findAll();
}
/**
* @Description 给已有的两个节点建立联系
**/
@RequestMapping(path = "/addKnows", method = RequestMethod.POST)
public Know addKnowsById(@RequestParam(value = "from") long fromId, @RequestParam(value = "to") long toId) {
Optional fromOpt = userService.findById(fromId);
Optional toOpt = userService.findById(toId);
if (fromOpt.isPresent()&&toOpt.isPresent()) {
return userService.addIKnows(fromOpt.get(),toOpt.get());
} else {
return null;
}
}
/**
* @Author keke
* @Description 删除两个节点之间的关系
* @Date 2021/6/7
* @Param [fromId, toId]
* @Return java.lang.String
**/
@RequestMapping(path = "/delKnows", method = RequestMethod.POST)
public String deleteKnowsByNodeId(@RequestParam(value = "from") long fromId, @RequestParam(value = "to") long toId) {
Optional fromOpt = userService.findById(fromId);
Optional toOpt = userService.findById(toId);
if (fromOpt.isPresent()&&toOpt.isPresent()) {
userService.deleteKnowByNodeId(fromId,toId);
return "ok";
} else {
return "false";
}
}
}
package com.study.neo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableNeo4jRepositories(basePackages = "com.study.neo.dao")
@EnableTransactionManagement // 激活SDN隐式事务
public class NeoDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NeoDemoApplication.class, args);
}
}
通过controller层接口对数据库进行增删改查
想在浏览器里打开本地图数据库,输入:http://localhost:7474/ 然后,输入数据库的账号密码