Java 操作 Neo4J 就是这么简单!

开发 后端
前几天阿粉给大家扩展了关于 Neo4J 图谱数据库的内容,今天阿粉教给大家如何使用 Java 来操作 Neo4j 数据库。

[[442141]]

本文转载自微信公众号「Java极客技术」,作者鸭血粉丝Tang。转载本文请联系Java极客技术公众号。

前几天阿粉给大家扩展了关于 Neo4J 图谱数据库的内容,今天阿粉教给大家如何使用 Java 来操作 Neo4j 数据库。

使用 Java 操作 Neo4J

首先我们先使用原生的这种方式,导入 jar 包,然后:

  1. public class TestController { 
  2.     public static void main(String[] args) { 
  3.         Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j""Yinlidong1995.")); 
  4.         Session session = driver.session(); 
  5.         session.run("CREATE (n:Part {name: {name},title: {title}})"
  6.                 parameters( "name""Arthur001""title""King001" )); 
  7.         StatementResult result = session.run( "MATCH (a:Part) WHERE a.name = {name} " + 
  8.                         "RETURN a.name AS name, a.title AS title"
  9.                 parameters( "name""Arthur001")); 
  10.         while (result.hasNext()) { 
  11.             Record record = result.next(); 
  12.             System.out.println( record.get( "title" ).asString() + "" + record.get( "name" ).asString() ); 
  13.         } 
  14.         session.close(); 
  15.         driver.close(); 
  16.     } 

这是一种比较古老的方式,来实现的,而且还是需要些 CQL 语句来进行实现。但是胜在非常好理解,这个时候,我们需要再来看看图,看看在 Neo4J 中他是怎么展现的。

通过这个,我们至少证明我们成功连上了,并且创建也成功了。

这时候有细心的读者就会问,为啥我之前在 GraphDatabase.driver 的地方,连接的是

bolt://localhost:7687.

这是因为,你启动的Neo4J 是7474,也就是说,Neo4J 服务里面可不是这个来连接,

SpringBoot 整合 Neo4j

1.创建SpringBoot项目

常规的创建SpringBoot项目,

创建完成之后,习惯性的要改一下 SpringBoot 的版本号,最好别用最新的,因为阿粉亲身经历,使用最新版的,出现了错误你都不知道怎么出现的,就是这么神奇,你永远都发现不了的bug。

我们把版本号改成2.1.0,这样的话,我们在 pom 文件中加入依赖 jar

  1. <dependency> 
  2.  <groupId>org.springframework.boot</groupId> 
  3.  <artifactId>spring-boot-starter-data-neo4j</artifactId> 
  4. </dependency> 
  5. <dependency> 
  6.  <groupId>org.projectlombok</groupId> 
  7.  <artifactId>lombok</artifactId> 
  8.  <version>1.16.10</version> 
  9. </dependency> 

 

2.增加配置

  1. spring: 
  2.   data: 
  3.     neo4j: 
  4.       url: bolt://localhost:7687 
  5.       username: neo4j 
  6.       password: Yinlidong1995. 
  7.   main: 
  8.     allow-bean-definition-overriding: true 

3.Neo4JConfig

  1. package com.example.neo4j.config; 
  2.  
  3. import org.neo4j.driver.v1.AuthTokens; 
  4. import org.neo4j.driver.v1.Driver; 
  5. import org.neo4j.driver.v1.GraphDatabase; 
  6. import org.neo4j.driver.v1.Session; 
  7. import org.springframework.beans.factory.annotation.Value; 
  8. import org.springframework.context.annotation.Bean; 
  9. import org.springframework.context.annotation.Configuration; 
  10. import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; 
  11. import org.springframework.transaction.annotation.EnableTransactionManagement; 
  12.  
  13. @Configuration 
  14. @EnableNeo4jRepositories("com.example.neo4j.repository"
  15. @EnableTransactionManagement 
  16. public class Neo4jConfig { 
  17.     @Value("${spring.data.neo4j.url}"
  18.     private String url; 
  19.     @Value("${spring.data.neo4j.username}"
  20.     private String userName; 
  21.     @Value("${spring.data.neo4j.password}"
  22.     private String password
  23.  
  24.     @Bean(name = "session"
  25.     public Session neo4jSession() { 
  26.         Driver driver = GraphDatabase.driver(url, AuthTokens.basic(userName, password)); 
  27.         return driver.session(); 
  28.     } 

4.编写实体类

  1. package com.example.neo4j.entry; 
  2.  
  3. import org.neo4j.ogm.annotation.*; 
  4.  
  5. import java.util.HashSet; 
  6. import java.util.Set
  7. @NodeEntity("group"
  8. @Data 
  9. public class GroupNode { 
  10.     @Id 
  11.     @GeneratedValue 
  12.     private Long id; 
  13.  
  14.     /** 
  15.      * 班级名称 
  16.      */ 
  17.     @Property(name = "name"
  18.     private String name
  19.  
  20.     /** 
  21.      * 编号 
  22.      */ 
  23.     private String num; 
  24.  
  25.     @Relationship(type = "RelationEdge"
  26.     private Set<RelationEdge> sets = new HashSet<>(); 
  27.  
  28.     public void addRelation(StudentNode sonNode, String name) { 
  29.         RelationEdge relationNode = new RelationEdge(this, sonNode, name); 
  30.         sets.add(relationNode); 
  31.         sonNode.getSets().add(relationNode); 
  32.     } 

学生实体类:

  1. package com.example.neo4j.entry; 
  2. import org.neo4j.ogm.annotation.GeneratedValue; 
  3. import org.neo4j.ogm.annotation.Id; 
  4. import org.neo4j.ogm.annotation.NodeEntity; 
  5. import org.neo4j.ogm.annotation.Relationship; 
  6.  
  7. import java.util.HashSet; 
  8. import java.util.Set
  9.  
  10. /** 
  11.  * 有点类似于Mysql中的table 映射的对象类,mysql中叫做ORM,neo4j中叫做OGM [object graph mapping] 
  12.  */ 
  13. @NodeEntity("student"
  14. @Data 
  15. public class StudentNode { 
  16.     @Id 
  17.     @GeneratedValue 
  18.     private Long id; 
  19.  
  20.     /** 
  21.      * 学生名称 
  22.      */ 
  23.     private String name
  24.  
  25.     /** 
  26.      * 性别 
  27.      */ 
  28.     private String sex; 
  29.  
  30.     @Relationship(type = "RelationEdge", direction = "INCOMING"
  31.     private Set<RelationEdge> sets = new HashSet<>(); 
  32.     
  1. package com.example.neo4j.entry; 
  2. import lombok.Data; 
  3. import org.neo4j.ogm.annotation.*; 
  4.  
  5. @RelationshipEntity(type = "RelationEdge"
  6. @Data 
  7. public class RelationEdge { 
  8.     @Id 
  9.     @GeneratedValue 
  10.     private Long id; 
  11.  
  12.     // 关系名 
  13.     private String name
  14.  
  15.     @StartNode 
  16.     private GroupNode groupNode; 
  17.  
  18.     @EndNode 
  19.     private StudentNode studentNode; 
  20.  
  21.     public RelationEdge(GroupNode parentNode, StudentNode sonNode, String name) { 
  22.         this.groupNode = parentNode; 
  23.         this.studentNode = sonNode; 
  24.         this.name = name
  25.     } 

5.Repository接口

对应的学生接口:

  1. package com.example.neo4j.repository; 
  2.  
  3. import com.example.neo4j.entry.StudentNode; 
  4. import org.springframework.data.neo4j.repository.Neo4jRepository; 
  5.  
  6. public interface StudentRepository extends Neo4jRepository<StudentNode,Long> { 

对应的班级接口

  1. package com.example.neo4j.repository; 
  2.  
  3. import com.example.neo4j.entry.GroupNode; 
  4. import org.springframework.data.neo4j.repository.Neo4jRepository; 
  5.  
  6. public interface GroupRepository extends Neo4jRepository<GroupNode,Long> { 

最后完成编写我们的 Controller

  1. package com.example.neo4j.controller; 
  2.  
  3. import com.example.neo4j.entry.*; 
  4. import com.example.neo4j.repository.GroupRepository; 
  5. import com.example.neo4j.repository.StudentRepository; 
  6. import lombok.extern.slf4j.Slf4j; 
  7. import org.springframework.beans.factory.annotation.Autowired; 
  8. import org.springframework.web.bind.annotation.GetMapping; 
  9. import org.springframework.web.bind.annotation.RequestMapping; 
  10. import org.springframework.web.bind.annotation.RestController; 
  11.  
  12.  
  13. @RestController 
  14. @RequestMapping("/node"
  15. @Slf4j 
  16. public class GroupController { 
  17.  
  18.     @Autowired 
  19.     private StudentRepository studentRepository; 
  20.     @Autowired 
  21.     private GroupRepository groupRepository; 
  22.  
  23.     @GetMapping(value = "/create"
  24.     public void createNodeRelation() { 
  25.         StudentNode studentNode1 = new StudentNode(); 
  26.         studentNode1.setName("Alen"); 
  27.         studentNode1.setSex("男"); 
  28.         StudentNode studentNode2 = new StudentNode(); 
  29.         studentNode2.setName("Kai"); 
  30.         studentNode2.setSex("女"); 
  31.         studentRepository.save(studentNode1); 
  32.         studentRepository.save(studentNode2); 
  33.  
  34.         GroupNode groupNode = new GroupNode(); 
  35.         groupNode.setName("火箭班"); 
  36.         groupNode.setNum("298"); 
  37.         // 增加关系 
  38.         groupNode.addRelation(studentNode1, "includes"); 
  39.         groupNode.addRelation(studentNode2, "includes"); 
  40.         groupRepository.save(groupNode); 
  41.     } 

启动之后,访问http://localhost:8080/node/create

我们再去图谱数据库看看。

怎么样,使用Java 操作是不是也是非常简单的呢?这样的图谱数据库你会选择么?

 

责任编辑:武晓燕 来源: Java极客技术
相关推荐

2022-11-18 17:53:03

Neo4j

2017-07-28 15:12:28

Neo4j图数据库

2011-07-26 12:48:52

neo4j图数据库

2022-04-13 11:32:45

Neo4j图数据库

2021-12-03 20:33:08

计算

2018-05-16 08:26:39

知识图谱Neo4j

2011-09-22 16:46:02

Neo4j图形数据库数据库

2022-01-17 14:34:59

数据平台数据数字化

2022-01-17 17:10:18

Neo4j 图数据库

2017-04-24 20:30:47

数据库工具导入数据

2015-09-28 08:57:06

Ruby APPNeo4j

2018-05-03 15:40:33

2021-05-24 10:50:10

Git命令Linux

2017-11-28 15:29:04

iPhone X网页适配

2021-10-27 17:20:23

图数据数据库

2022-02-15 08:22:28

Neo4jSpring数据库

2020-06-16 10:57:20

搭建

2022-09-08 13:57:00

SpringBootNeo4j

2022-03-23 09:37:54

Neo4j开源侵犯

2016-07-22 15:12:12

Win10技巧重装
点赞
收藏

51CTO技术栈公众号