自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java 操作 Neo4J 就是這么簡(jiǎn)單!

開發(fā) 后端
前幾天阿粉給大家擴(kuò)展了關(guān)于 Neo4J 圖譜數(shù)據(jù)庫的內(nèi)容,今天阿粉教給大家如何使用 Java 來操作 Neo4j 數(shù)據(jù)庫。

[[442141]]

本文轉(zhuǎn)載自微信公眾號(hào)「Java極客技術(shù)」,作者鴨血粉絲Tang。轉(zhuǎn)載本文請(qǐng)聯(lián)系Java極客技術(shù)公眾號(hào)。

前幾天阿粉給大家擴(kuò)展了關(guān)于 Neo4J 圖譜數(shù)據(jù)庫的內(nèi)容,今天阿粉教給大家如何使用 Java 來操作 Neo4j 數(shù)據(jù)庫。

使用 Java 操作 Neo4J

首先我們先使用原生的這種方式,導(dǎo)入 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.     } 

這是一種比較古老的方式,來實(shí)現(xiàn)的,而且還是需要些 CQL 語句來進(jìn)行實(shí)現(xiàn)。但是勝在非常好理解,這個(gè)時(shí)候,我們需要再來看看圖,看看在 Neo4J 中他是怎么展現(xiàn)的。

通過這個(gè),我們至少證明我們成功連上了,并且創(chuàng)建也成功了。

這時(shí)候有細(xì)心的讀者就會(huì)問,為啥我之前在 GraphDatabase.driver 的地方,連接的是

bolt://localhost:7687.

這是因?yàn)椋銌?dòng)的Neo4J 是7474,也就是說,Neo4J 服務(wù)里面可不是這個(gè)來連接,

SpringBoot 整合 Neo4j

1.創(chuàng)建SpringBoot項(xiàng)目

常規(guī)的創(chuàng)建SpringBoot項(xiàng)目,

創(chuàng)建完成之后,習(xí)慣性的要改一下 SpringBoot 的版本號(hào),最好別用最新的,因?yàn)榘⒎塾H身經(jīng)歷,使用最新版的,出現(xiàn)了錯(cuò)誤你都不知道怎么出現(xiàn)的,就是這么神奇,你永遠(yuǎn)都發(fā)現(xiàn)不了的bug。

我們把版本號(hào)改成2.1.0,這樣的話,我們?cè)?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.編寫實(shí)體類

  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.      * 班級(jí)名稱 
  16.      */ 
  17.     @Property(name = "name"
  18.     private String name
  19.  
  20.     /** 
  21.      * 編號(hào) 
  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.     } 

學(xué)生實(shí)體類:

  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.  * 有點(diǎn)類似于Mysql中的table 映射的對(duì)象類,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.      * 學(xué)生名稱 
  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.     // 關(guān)系名 
  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接口

對(duì)應(yīng)的學(xué)生接口:

  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> { 

對(duì)應(yīng)的班級(jí)接口

  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.         // 增加關(guān)系 
  38.         groupNode.addRelation(studentNode1, "includes"); 
  39.         groupNode.addRelation(studentNode2, "includes"); 
  40.         groupRepository.save(groupNode); 
  41.     } 

啟動(dòng)之后,訪問http://localhost:8080/node/create

我們?cè)偃D譜數(shù)據(jù)庫看看。

怎么樣,使用Java 操作是不是也是非常簡(jiǎn)單的呢?這樣的圖譜數(shù)據(jù)庫你會(huì)選擇么?

 

責(zé)任編輯:武曉燕 來源: Java極客技術(shù)
相關(guān)推薦

2022-11-18 17:53:03

Neo4j

2011-07-26 12:48:52

neo4j圖數(shù)據(jù)庫

2017-07-28 15:12:28

Neo4j圖數(shù)據(jù)庫

2022-04-13 11:32:45

Neo4j圖數(shù)據(jù)庫

2018-05-16 08:26:39

知識(shí)圖譜Neo4j

2021-12-03 20:33:08

計(jì)算

2024-08-08 08:31:32

SpringNeo4j優(yōu)化

2024-06-03 10:53:18

LLMRAGGraphRAG

2011-09-22 16:46:02

Neo4j圖形數(shù)據(jù)庫數(shù)據(jù)庫

2022-01-17 17:10:18

Neo4j 圖數(shù)據(jù)庫

2022-01-17 14:34:59

數(shù)據(jù)平臺(tái)數(shù)據(jù)數(shù)字化

2017-11-28 15:29:04

iPhone X網(wǎng)頁適配

2021-05-24 10:50:10

Git命令Linux

2018-05-03 15:40:33

2015-09-28 08:57:06

Ruby APPNeo4j

2017-04-24 20:30:47

數(shù)據(jù)庫工具導(dǎo)入數(shù)據(jù)

2021-10-27 17:20:23

圖數(shù)據(jù)數(shù)據(jù)庫

2022-02-15 08:22:28

Neo4jSpring數(shù)據(jù)庫

2020-06-16 10:57:20

搭建

2024-08-28 08:42:21

API接口限流
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)