接着上一篇讲
上一篇我们学习了如何创建节点,以及查询节点,但未涉及二者之间的关系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 [email protected]
* @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 [email protected]
* @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 players;
@Relationship(type = "Have")
@JsonProperty("拥有")
private List 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 getPlayers() {
return players;
}
public void setPlayers(List players) {
this.players = players;
}
public List getCats() {
return cats;
}
public void setCats(List 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 [email protected]
* @blob http://blog.csdn.net/appleyk
* @date 2018年1月18日-下午12:10:53
*/
@Repository
public interface CoderRepository extends GraphRepository{
/*
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;
}
}
package com.appleyk.data.Repository;
import org.springframework.data.neo4j.repository.GraphRepository;
import com.appleyk.data.nodeentity.Player;
public interface PlayerRepository extends GraphRepository{
}
(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{
}
三、创建节点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": "灰白色"
}
]
}
B.
C.
四、创建节点Coder(不包含关系,仅仅是单节点)
(1)请求数据:JSON
{
"name": "李大锤",
"sex": "男",
"hobby": "吃饭、睡觉"
}
五、查询节点Coder(带关系,name='appleyk')
(1)请求地址(Get):http://localhost:8088/rest/v1.0.1/database/coder/get
六、查询节点Coder(不带关系,name='李大锤')