Spring-Boot+Neo4j+节点之间关系网的搭建和查询
2018-01-19 17:20
483 查看
接着上一篇讲
上一篇我们学习了如何创建节点,以及查询节点,但未涉及二者之间的关系relationship,本篇,我们将通过简单的案列来完整的走一遍整个节点--关系--节点的流程,为了降低复杂度,关系是(1...n)单向的,也就是默认关系是Out出去的。
一、场景
节点Nodes:
1.创建一个Coder类型的节点,代表程序员,属性有id,name,sex,hobby(姓名,性别,爱好)
2.创建一个Cat类型的节点,代表猫,属性有id,name,color(名称,颜色)
3.创建一个Player类型的节点,代表体育运动员,属性有id,name,height,location(姓名。身高,场上位置)
关系Relationship:
Coder(程序员) -- Have(拥有)Cat(猫)
--Like(喜欢)Player(运动员)
在neo4j图库中表示为下图:
![](http://img.blog.csdn.net/20180119170306981?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
为了后台演示关系数据的创建,这里我们把图库中的数据清空:
![](http://img.blog.csdn.net/20180119170458084?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
执行
![](http://img.blog.csdn.net/20180119170549502?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
二、构建实体(数据操作部分)+接口(增删改查CRUD)
![](http://img.blog.csdn.net/20180119171414328?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
(1)抽离Coder、Cat、Player三者共有的属性 -- 抽象类
package com.appleyk.data.nodeentity;
import org.neo4j.ogm.annotation.GraphId;
/**
* 抽取共同的属性字段
* @author yukun24@126.com
* @blob http://blog.csdn.net/appleyk * @date 2018年1月19日-下午4:35:25
*/
public abstract class BaseEntity {
/**
* Neo4j会分配的ID(节点唯一标识 当前类中有效)
*/
@GraphId
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(2)Coder
A. Coder实体Java类映射(包含单边关系---注意关系指向的对象又是一个节点)
@JsonProperty("别名") :Json注解---> 给关系起别名
package com.appleyk.data.nodeentity;
import java.util.List;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Neo4j的节点实体类:Coder
*
* @author yukun24@126.com
* @blob http://blog.csdn.net/appleyk * @date 2018年1月18日-下午12:06:17
*/
@NodeEntity
public class Coder extends BaseEntity {
private String sex;
private String hobby;
@Relationship(type = "Like")
@JsonProperty("喜欢")
private List<Player> players;
@Relationship(type = "Have")
@JsonProperty("拥有")
private List<Cat> cats;
public Coder() {
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public List<Player> getPlayers() {
return players;
}
public void setPlayers(List<Player> players) {
this.players = players;
}
public List<Cat> getCats() {
return cats;
}
public void setCats(List<Cat> cats) {
this.cats = cats;
}
}
B. Coder实体对应的图形存储器的接口实现(CRUD 增删改查)
package com.appleyk.data.Repository;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.stereotype.Repository;
import com.appleyk.data.nodeentity.Coder;
/**
* Coder 节点增删改
* @author yukun24@126.com
* @blob http://blog.csdn.net/appleyk * @date 2018年1月18日-下午12:10:53
*/
@Repository
public interface CoderRepository extends GraphRepository<Coder>{
/*
CoderRepositiory 继承 GraphRepository类,实现增删查改
实现自己的接口:通过名字查询Coder(可以是单个Coder,也可以是一个List集合),
spring-data-neo4j 支持方法命名约定查询 findBy{Coder的属性名},
findBy后面的属性名一定要Coder类里存在,否则会报错
*/
Coder findByName(@Param("name") String name);
(3)Player
A.实体
package com.appleyk.data.nodeentity;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
public class Player extends BaseEntity{
private double height;
private String location;
public Player(){
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
B.接口
package com.appleyk.data.Repository;
import org.springframework.data.neo4j.repository.GraphRepository;
import com.appleyk.data.nodeentity.Player;
public interface PlayerRepository extends GraphRepository<Player>{
}
(4)Cat
A.实体
package com.appleyk.data.nodeentity;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
public class Cat extends BaseEntity {
private String color;
public Cat() {
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
B.接口
package com.appleyk.data.Repository;
import org.springframework.data.neo4j.repository.GraphRepository;
import com.appleyk.data.nodeentity.Cat;
public interface CatReponsitory extends GraphRepository<Cat>{
}
三、创建节点Coder(含关系连接Cat节点和Player节点)
(1)CoderController
package com.appleyk.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.appleyk.data.Repository.CoderRepository;
import com.appleyk.data.nodeentity.Coder;
import com.appleyk.result.ResponseResult;
@RestController
@RequestMapping("/rest/v1.0.1/database/coder") //restful风格的api接口
public class CoderController {
@Autowired
CoderRepository coderRepositiory;
@RequestMapping("/get")
public Coder GetCoderByName(@RequestParam(value="name") String name){
return coderRepositiory.findByName(name);
}
@PostMapping("/save")
@Transactional
public ResponseResult Create(@RequestBody Coder coder) throws Exception{
Coder result = coderRepositiory.save(coder);
if(result!=null){
return new ResponseResult(200,result.getName()+"节点创建成功");
}
return new ResponseResult(500,coder.getName()+"节点创建失败!");
}
}
(2)Coder:Json数据
(3)启动项目
![](http://img.blog.csdn.net/20180119173245664?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
(4)请求数据 -- 创建(save)Coder节点
restful api:http://localhost:8088/rest/v1.0.1/database/coder/save
A.
![](http://img.blog.csdn.net/20180119173415893?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
B.
![](http://img.blog.csdn.net/20180119173451138?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
C.
![](http://img.blog.csdn.net/20180119173623890?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
四、创建节点Coder(不包含关系,仅仅是单节点)
(1)请求数据:JSON
{
"name": "李大锤",
"sex": "男",
"hobby": "吃饭、睡觉"
}
(2)Save
![](http://img.blog.csdn.net/20180119173843118?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
(3)
![](http://img.blog.csdn.net/20180119173943690?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
五、查询节点Coder(带关系,name='appleyk')
(1)请求地址(Get):http://localhost:8088/rest/v1.0.1/database/coder/get
(2)Get
![](http://img.blog.csdn.net/20180119174310024?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXBwbGV5aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
六、查询节点Coder(不带关系,name='李大锤')
上一篇我们学习了如何创建节点,以及查询节点,但未涉及二者之间的关系relationship,本篇,我们将通过简单的案列来完整的走一遍整个节点--关系--节点的流程,为了降低复杂度,关系是(1...n)单向的,也就是默认关系是Out出去的。
一、场景
节点Nodes:
1.创建一个Coder类型的节点,代表程序员,属性有id,name,sex,hobby(姓名,性别,爱好)
2.创建一个Cat类型的节点,代表猫,属性有id,name,color(名称,颜色)
3.创建一个Player类型的节点,代表体育运动员,属性有id,name,height,location(姓名。身高,场上位置)
关系Relationship:
Coder(程序员) -- Have(拥有)Cat(猫)
--Like(喜欢)Player(运动员)
在neo4j图库中表示为下图:
为了后台演示关系数据的创建,这里我们把图库中的数据清空:
执行
二、构建实体(数据操作部分)+接口(增删改查CRUD)
(1)抽离Coder、Cat、Player三者共有的属性 -- 抽象类
package com.appleyk.data.nodeentity;
import org.neo4j.ogm.annotation.GraphId;
/**
* 抽取共同的属性字段
* @author yukun24@126.com
* @blob http://blog.csdn.net/appleyk * @date 2018年1月19日-下午4:35:25
*/
public abstract class BaseEntity {
/**
* Neo4j会分配的ID(节点唯一标识 当前类中有效)
*/
@GraphId
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(2)Coder
A. Coder实体Java类映射(包含单边关系---注意关系指向的对象又是一个节点)
@JsonProperty("别名") :Json注解---> 给关系起别名
package com.appleyk.data.nodeentity;
import java.util.List;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Neo4j的节点实体类:Coder
*
* @author yukun24@126.com
* @blob http://blog.csdn.net/appleyk * @date 2018年1月18日-下午12:06:17
*/
@NodeEntity
public class Coder extends BaseEntity {
private String sex;
private String hobby;
@Relationship(type = "Like")
@JsonProperty("喜欢")
private List<Player> players;
@Relationship(type = "Have")
@JsonProperty("拥有")
private List<Cat> cats;
public Coder() {
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public List<Player> getPlayers() {
return players;
}
public void setPlayers(List<Player> players) {
this.players = players;
}
public List<Cat> getCats() {
return cats;
}
public void setCats(List<Cat> cats) {
this.cats = cats;
}
}
B. Coder实体对应的图形存储器的接口实现(CRUD 增删改查)
package com.appleyk.data.Repository;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.stereotype.Repository;
import com.appleyk.data.nodeentity.Coder;
/**
* Coder 节点增删改
* @author yukun24@126.com
* @blob http://blog.csdn.net/appleyk * @date 2018年1月18日-下午12:10:53
*/
@Repository
public interface CoderRepository extends GraphRepository<Coder>{
/*
CoderRepositiory 继承 GraphRepository类,实现增删查改
实现自己的接口:通过名字查询Coder(可以是单个Coder,也可以是一个List集合),
spring-data-neo4j 支持方法命名约定查询 findBy{Coder的属性名},
findBy后面的属性名一定要Coder类里存在,否则会报错
*/
Coder findByName(@Param("name") String name);
(3)Player
A.实体
package com.appleyk.data.nodeentity;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
public class Player extends BaseEntity{
private double height;
private String location;
public Player(){
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
B.接口
package com.appleyk.data.Repository;
import org.springframework.data.neo4j.repository.GraphRepository;
import com.appleyk.data.nodeentity.Player;
public interface PlayerRepository extends GraphRepository<Player>{
}
(4)Cat
A.实体
package com.appleyk.data.nodeentity;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
public class Cat extends BaseEntity {
private String color;
public Cat() {
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
B.接口
package com.appleyk.data.Repository;
import org.springframework.data.neo4j.repository.GraphRepository;
import com.appleyk.data.nodeentity.Cat;
public interface CatReponsitory extends GraphRepository<Cat>{
}
三、创建节点Coder(含关系连接Cat节点和Player节点)
(1)CoderController
package com.appleyk.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.appleyk.data.Repository.CoderRepository;
import com.appleyk.data.nodeentity.Coder;
import com.appleyk.result.ResponseResult;
@RestController
@RequestMapping("/rest/v1.0.1/database/coder") //restful风格的api接口
public class CoderController {
@Autowired
CoderRepository coderRepositiory;
@RequestMapping("/get")
public Coder GetCoderByName(@RequestParam(value="name") String name){
return coderRepositiory.findByName(name);
}
@PostMapping("/save")
@Transactional
public ResponseResult Create(@RequestBody Coder coder) throws Exception{
Coder result = coderRepositiory.save(coder);
if(result!=null){
return new ResponseResult(200,result.getName()+"节点创建成功");
}
return new ResponseResult(500,coder.getName()+"节点创建失败!");
}
}
(2)Coder:Json数据
{ "name": "appleyk", "sex": "男", "hobby": "体育、游戏", "喜欢": [ { "name": "科比", "height": 1.98, "location": "得分后卫" }, { "name": "孙杨", "height": 1.98, "location": "游泳运动员" } ], "拥有": [ { "name": "苏格兰折耳猫", "color": "灰白色" } ] }
(3)启动项目
(4)请求数据 -- 创建(save)Coder节点
restful api:http://localhost:8088/rest/v1.0.1/database/coder/save
A.
B.
C.
四、创建节点Coder(不包含关系,仅仅是单节点)
(1)请求数据:JSON
{
"name": "李大锤",
"sex": "男",
"hobby": "吃饭、睡觉"
}
(2)Save
(3)
五、查询节点Coder(带关系,name='appleyk')
(1)请求地址(Get):http://localhost:8088/rest/v1.0.1/database/coder/get
(2)Get
六、查询节点Coder(不带关系,name='李大锤')
相关文章推荐
- 数据库中表和表之间的连接(多关系连接查询)
- 市长信箱邮件查询服务: 使用SpringBoot搭建基础
- hibernate二级缓存与查询缓存之间的关系
- Spring内核研究-管理bean之间的关系三(自动装配)
- [bigdata-83] python3+图数据库neo4j 电话号码 身份证 黑名单 创建节点 标签 关系 增标记 增关系 查询pattern示例
- 基于【CentOS-7+ Ambari 2.7.0 + HDP 3.0】搭建HAWQ数据仓库——安装配置OPEN-SSH,设置主机节点之间免密互访
- WAS中单元(Cell)节点(Node),以及Node、Profile 与 Server 之间的关系是什么
- Spring、Struts、Hibernate框架之间的关系是什么以及怎么处理
- spring data jpa hibernate jpa 三者之间的关系
- Spring初学之bean之间的关系和bean的作用域
- Spring知识整理(三)Spring之Bean之间的关系,Bean的作用域,Bean的生命周期
- redis客户端jedis基于spring搭建单节点或者集群线程池连接
- 对于树形结构的关系表,查询一个节点下所有节点的方法设计
- Spring 习笔记之bean 之间的关系
- Spring 中获取servletContext及WebApplicationContext以及applicationContext三者之间的关系
- Spring 中 bean 之间的关系:继承;依赖
- spring中常见jar包的作用以及之间的关系
- oracle子接点与父节点上下级关系数据库查询语句,及联查询,
- Spring Bean之间的关系
- spring学习历程---bean之间的关系