当前位置:   article > 正文

Spring-Boot+Neo4j+节点之间关系网的搭建和查询_springboot 用cypher获取neo4j网页code中的response结果

springboot 用cypher获取neo4j网页code中的response结果

接着上一篇讲


       上一篇我们学习了如何创建节点,以及查询节点,但未涉及二者之间的关系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三者共有的属性 -- 抽象类


  1. package com.appleyk.data.nodeentity;
  2. import org.neo4j.ogm.annotation.GraphId;
  3. /**
  4. * 抽取共同的属性字段
  5. * @author yukun24@126.com
  6. * @blob http://blog.csdn.net/appleyk
  7. * @date 2018年1月19日-下午4:35:25
  8. */
  9. public abstract class BaseEntity {
  10. /**
  11. * Neo4j会分配的ID(节点唯一标识 当前类中有效)
  12. */
  13. @GraphId
  14. private Long id;
  15. private String name;
  16. public Long getId() {
  17. return id;
  18. }
  19. public void setId(Long id) {
  20. this.id = id;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. }



(2)Coder


A. Coder实体Java类映射(包含单边关系---注意关系指向的对象又是一个节点)


@JsonProperty("别名") :Json注解---> 给关系起别名

  1. package com.appleyk.data.nodeentity;
  2. import java.util.List;
  3. import org.neo4j.ogm.annotation.NodeEntity;
  4. import org.neo4j.ogm.annotation.Relationship;
  5. import com.fasterxml.jackson.annotation.JsonProperty;
  6. /**
  7. * Neo4j的节点实体类:Coder
  8. *
  9. * @author yukun24@126.com
  10. * @blob http://blog.csdn.net/appleyk
  11. * @date 2018年1月18日-下午12:06:17
  12. */
  13. @NodeEntity
  14. public class Coder extends BaseEntity {
  15. private String sex;
  16. private String hobby;
  17. @Relationship(type = "Like")
  18. @JsonProperty("喜欢")
  19. private List<Player> players;
  20. @Relationship(type = "Have")
  21. @JsonProperty("拥有")
  22. private List<Cat> cats;
  23. public Coder() {
  24. }
  25. public String getSex() {
  26. return sex;
  27. }
  28. public void setSex(String sex) {
  29. this.sex = sex;
  30. }
  31. public String getHobby() {
  32. return hobby;
  33. }
  34. public void setHobby(String hobby) {
  35. this.hobby = hobby;
  36. }
  37. public List<Player> getPlayers() {
  38. return players;
  39. }
  40. public void setPlayers(List<Player> players) {
  41. this.players = players;
  42. }
  43. public List<Cat> getCats() {
  44. return cats;
  45. }
  46. public void setCats(List<Cat> cats) {
  47. this.cats = cats;
  48. }
  49. }


B. Coder实体对应的图形存储器的接口实现(CRUD 增删改查)


  1. package com.appleyk.data.Repository;
  2. import org.apache.ibatis.annotations.Param;
  3. import org.springframework.data.neo4j.repository.GraphRepository;
  4. import org.springframework.stereotype.Repository;
  5. import com.appleyk.data.nodeentity.Coder;
  6. /**
  7. * Coder 节点增删改
  8. * @author yukun24@126.com
  9. * @blob http://blog.csdn.net/appleyk
  10. * @date 2018年1月18日-下午12:10:53
  11. */
  12. @Repository
  13. public interface CoderRepository extends GraphRepository<Coder>{
  14. /*
  15. CoderRepositiory 继承 GraphRepository类,实现增删查改
  16. 实现自己的接口:通过名字查询Coder(可以是单个Coder,也可以是一个List集合),
  17. spring-data-neo4j 支持方法命名约定查询 findBy{Coder的属性名},
  18. findBy后面的属性名一定要Coder类里存在,否则会报错
  19. */
  20. Coder findByName(@Param("name") String name);


(3)Player


A.实体

  1. package com.appleyk.data.nodeentity;
  2. import org.neo4j.ogm.annotation.NodeEntity;
  3. @NodeEntity
  4. public class Player extends BaseEntity{
  5. private double height;
  6. private String location;
  7. public Player(){
  8. }
  9. public double getHeight() {
  10. return height;
  11. }
  12. public void setHeight(double height) {
  13. this.height = height;
  14. }
  15. public String getLocation() {
  16. return location;
  17. }
  18. public void setLocation(String location) {
  19. this.location = location;
  20. }
  21. }

B.接口


  1. package com.appleyk.data.Repository;
  2. import org.springframework.data.neo4j.repository.GraphRepository;
  3. import com.appleyk.data.nodeentity.Player;
  4. public interface PlayerRepository extends GraphRepository<Player>{
  5. }



(4)Cat


A.实体


  1. package com.appleyk.data.nodeentity;
  2. import org.neo4j.ogm.annotation.NodeEntity;
  3. @NodeEntity
  4. public class Cat extends BaseEntity {
  5. private String color;
  6. public Cat() {
  7. }
  8. public String getColor() {
  9. return color;
  10. }
  11. public void setColor(String color) {
  12. this.color = color;
  13. }
  14. }


B.接口


  1. package com.appleyk.data.Repository;
  2. import org.springframework.data.neo4j.repository.GraphRepository;
  3. import com.appleyk.data.nodeentity.Cat;
  4. public interface CatReponsitory extends GraphRepository<Cat>{
  5. }



三、创建节点Coder(含关系连接Cat节点和Player节点)



(1)CoderController


  1. package com.appleyk.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.transaction.annotation.Transactional;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import com.appleyk.data.Repository.CoderRepository;
  10. import com.appleyk.data.nodeentity.Coder;
  11. import com.appleyk.result.ResponseResult;
  12. @RestController
  13. @RequestMapping("/rest/v1.0.1/database/coder") //restful风格的api接口
  14. public class CoderController {
  15. @Autowired
  16. CoderRepository coderRepositiory;
  17. @RequestMapping("/get")
  18. public Coder GetCoderByName(@RequestParam(value="name") String name){
  19. return coderRepositiory.findByName(name);
  20. }
  21. @PostMapping("/save")
  22. @Transactional
  23. public ResponseResult Create(@RequestBody Coder coder) throws Exception{
  24. Coder result = coderRepositiory.save(coder);
  25. if(result!=null){
  26. return new ResponseResult(200,result.getName()+"节点创建成功");
  27. }
  28. return new ResponseResult(500,coder.getName()+"节点创建失败!");
  29. }
  30. }


(2)Coder:Json数据



  1. {
  2. "name": "appleyk",
  3. "sex": "男",
  4. "hobby": "体育、游戏",
  5. "喜欢": [
  6. {
  7. "name": "科比",
  8. "height": 1.98,
  9. "location": "得分后卫"
  10. },
  11. {
  12. "name": "孙杨",
  13. "height": 1.98,
  14. "location": "游泳运动员"
  15. }
  16. ],
  17. "拥有": [
  18. {
  19. "name": "苏格兰折耳猫",
  20. "color": "灰白色"
  21. }
  22. ]
  23. }



(3)启动项目






(4)请求数据 -- 创建(save)Coder节点



restful api:http://localhost:8088/rest/v1.0.1/database/coder/save



A.




B.






C.






四、创建节点Coder(不包含关系,仅仅是单节点)



(1)请求数据:JSON


  1. {
  2. "name": "李大锤",
  3. "sex": "男",
  4. "hobby": "吃饭、睡觉"
  5. }


(2)Save







(3)








五、查询节点Coder(带关系,name='appleyk')



(1)请求地址(Get):http://localhost:8088/rest/v1.0.1/database/coder/get




(2)Get









六、查询节点Coder(不带关系,name='李大锤')







声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/727715
推荐阅读
相关标签
  

闽ICP备14008679号