Java 操作 Neo4J 就是這么簡(jiǎn)單!
本文轉(zhuǎn)載自微信公眾號(hào)「Java極客技術(shù)」,作者鴨血粉絲Tang。轉(zhuǎn)載本文請(qǐng)聯(lián)系Java極客技術(shù)公眾號(hào)。
前幾天阿粉給大家擴(kuò)展了關(guān)于 Neo4J 圖譜數(shù)據(jù)庫(kù)的內(nèi)容,今天阿粉教給大家如何使用 Java 來(lái)操作 Neo4j 數(shù)據(jù)庫(kù)。
使用 Java 操作 Neo4J
首先我們先使用原生的這種方式,導(dǎo)入 jar 包,然后:
- public class TestController {
 - public static void main(String[] args) {
 - Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "Yinlidong1995."));
 - Session session = driver.session();
 - session.run("CREATE (n:Part {name: {name},title: {title}})",
 - parameters( "name", "Arthur001", "title", "King001" ));
 - StatementResult result = session.run( "MATCH (a:Part) WHERE a.name = {name} " +
 - "RETURN a.name AS name, a.title AS title",
 - parameters( "name", "Arthur001"));
 - while (result.hasNext()) {
 - Record record = result.next();
 - System.out.println( record.get( "title" ).asString() + "" + record.get( "name" ).asString() );
 - }
 - session.close();
 - driver.close();
 - }
 - }
 
這是一種比較古老的方式,來(lái)實(shí)現(xiàn)的,而且還是需要些 CQL 語(yǔ)句來(lái)進(jìn)行實(shí)現(xiàn)。但是勝在非常好理解,這個(gè)時(shí)候,我們需要再來(lái)看看圖,看看在 Neo4J 中他是怎么展現(xiàn)的。
通過這個(gè),我們至少證明我們成功連上了,并且創(chuàng)建也成功了。
這時(shí)候有細(xì)心的讀者就會(huì)問,為啥我之前在 GraphDatabase.driver 的地方,連接的是
bolt://localhost:7687.
這是因?yàn)?,你啟?dòng)的Neo4J 是7474,也就是說(shuō),Neo4J 服務(wù)里面可不是這個(gè)來(lái)連接,
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
- <dependency>
 - <groupId>org.springframework.boot</groupId>
 - <artifactId>spring-boot-starter-data-neo4j</artifactId>
 - </dependency>
 - <dependency>
 - <groupId>org.projectlombok</groupId>
 - <artifactId>lombok</artifactId>
 - <version>1.16.10</version>
 - </dependency>
 
2.增加配置
- spring:
 - data:
 - neo4j:
 - url: bolt://localhost:7687
 - username: neo4j
 - password: Yinlidong1995.
 - main:
 - allow-bean-definition-overriding: true
 
3.Neo4JConfig
- package com.example.neo4j.config;
 - import org.neo4j.driver.v1.AuthTokens;
 - import org.neo4j.driver.v1.Driver;
 - import org.neo4j.driver.v1.GraphDatabase;
 - import org.neo4j.driver.v1.Session;
 - import org.springframework.beans.factory.annotation.Value;
 - import org.springframework.context.annotation.Bean;
 - import org.springframework.context.annotation.Configuration;
 - import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
 - import org.springframework.transaction.annotation.EnableTransactionManagement;
 - @Configuration
 - @EnableNeo4jRepositories("com.example.neo4j.repository")
 - @EnableTransactionManagement
 - public class Neo4jConfig {
 - @Value("${spring.data.neo4j.url}")
 - private String url;
 - @Value("${spring.data.neo4j.username}")
 - private String userName;
 - @Value("${spring.data.neo4j.password}")
 - private String password;
 - @Bean(name = "session")
 - public Session neo4jSession() {
 - Driver driver = GraphDatabase.driver(url, AuthTokens.basic(userName, password));
 - return driver.session();
 - }
 - }
 
4.編寫實(shí)體類
- package com.example.neo4j.entry;
 - import org.neo4j.ogm.annotation.*;
 - import java.util.HashSet;
 - import java.util.Set;
 - @NodeEntity("group")
 - @Data
 - public class GroupNode {
 - @Id
 - @GeneratedValue
 - private Long id;
 - /**
 - * 班級(jí)名稱
 - */
 - @Property(name = "name")
 - private String name;
 - /**
 - * 編號(hào)
 - */
 - private String num;
 - @Relationship(type = "RelationEdge")
 - private Set<RelationEdge> sets = new HashSet<>();
 - public void addRelation(StudentNode sonNode, String name) {
 - RelationEdge relationNode = new RelationEdge(this, sonNode, name);
 - sets.add(relationNode);
 - sonNode.getSets().add(relationNode);
 - }
 - }
 
學(xué)生實(shí)體類:
- package com.example.neo4j.entry;
 - import org.neo4j.ogm.annotation.GeneratedValue;
 - import org.neo4j.ogm.annotation.Id;
 - import org.neo4j.ogm.annotation.NodeEntity;
 - import org.neo4j.ogm.annotation.Relationship;
 - import java.util.HashSet;
 - import java.util.Set;
 - /**
 - * 有點(diǎn)類似于Mysql中的table 映射的對(duì)象類,mysql中叫做ORM,neo4j中叫做OGM [object graph mapping]
 - */
 - @NodeEntity("student")
 - @Data
 - public class StudentNode {
 - @Id
 - @GeneratedValue
 - private Long id;
 - /**
 - * 學(xué)生名稱
 - */
 - private String name;
 - /**
 - * 性別
 - */
 - private String sex;
 - @Relationship(type = "RelationEdge", direction = "INCOMING")
 - private Set<RelationEdge> sets = new HashSet<>();
 - }
 
- package com.example.neo4j.entry;
 - import lombok.Data;
 - import org.neo4j.ogm.annotation.*;
 - @RelationshipEntity(type = "RelationEdge")
 - @Data
 - public class RelationEdge {
 - @Id
 - @GeneratedValue
 - private Long id;
 - // 關(guān)系名
 - private String name;
 - @StartNode
 - private GroupNode groupNode;
 - @EndNode
 - private StudentNode studentNode;
 - public RelationEdge(GroupNode parentNode, StudentNode sonNode, String name) {
 - this.groupNode = parentNode;
 - this.studentNode = sonNode;
 - this.name = name;
 - }
 - }
 
5.Repository接口
對(duì)應(yīng)的學(xué)生接口:
- package com.example.neo4j.repository;
 - import com.example.neo4j.entry.StudentNode;
 - import org.springframework.data.neo4j.repository.Neo4jRepository;
 - public interface StudentRepository extends Neo4jRepository<StudentNode,Long> {
 - }
 
對(duì)應(yīng)的班級(jí)接口
- package com.example.neo4j.repository;
 - import com.example.neo4j.entry.GroupNode;
 - import org.springframework.data.neo4j.repository.Neo4jRepository;
 - public interface GroupRepository extends Neo4jRepository<GroupNode,Long> {
 - }
 
最后完成編寫我們的 Controller
- package com.example.neo4j.controller;
 - import com.example.neo4j.entry.*;
 - import com.example.neo4j.repository.GroupRepository;
 - import com.example.neo4j.repository.StudentRepository;
 - import lombok.extern.slf4j.Slf4j;
 - import org.springframework.beans.factory.annotation.Autowired;
 - import org.springframework.web.bind.annotation.GetMapping;
 - import org.springframework.web.bind.annotation.RequestMapping;
 - import org.springframework.web.bind.annotation.RestController;
 - @RestController
 - @RequestMapping("/node")
 - @Slf4j
 - public class GroupController {
 - @Autowired
 - private StudentRepository studentRepository;
 - @Autowired
 - private GroupRepository groupRepository;
 - @GetMapping(value = "/create")
 - public void createNodeRelation() {
 - StudentNode studentNode1 = new StudentNode();
 - studentNode1.setName("Alen");
 - studentNode1.setSex("男");
 - StudentNode studentNode2 = new StudentNode();
 - studentNode2.setName("Kai");
 - studentNode2.setSex("女");
 - studentRepository.save(studentNode1);
 - studentRepository.save(studentNode2);
 - GroupNode groupNode = new GroupNode();
 - groupNode.setName("火箭班");
 - groupNode.setNum("298");
 - // 增加關(guān)系
 - groupNode.addRelation(studentNode1, "includes");
 - groupNode.addRelation(studentNode2, "includes");
 - groupRepository.save(groupNode);
 - }
 - }
 
啟動(dòng)之后,訪問http://localhost:8080/node/create
我們?cè)偃D譜數(shù)據(jù)庫(kù)看看。
怎么樣,使用Java 操作是不是也是非常簡(jiǎn)單的呢?這樣的圖譜數(shù)據(jù)庫(kù)你會(huì)選擇么?




















 
 
 















 
 
 
 