Springboot+Neo4j节点与关系的操作(三)

前两篇文章Springboot+Neo4j 初级框架搭建(一)Springboot+Neo4j 初级增删改查(二)我们介绍了Springboot集成Neo4j,以及Neo4j单节点的操作。

本篇文章我们就来写写节点与节点中关系的操作!!!话不多说直接开干。

在上篇文章中,我们以公司为例子做了演示,本篇文章我们还是以公司和产品为示例演示。

首先我们在model层创建两个类,一个产品类,一个生产关系类,公司类就不创建了,上篇文章已经有了。

@NodeEntity(label = "ProductEntry")

@Data

public class ProductEntryNode {

    @Id

    private String productEntryId;

    /**

    * 模板id

    */

    private String templateId;

    /**

    * 词条名称

    */

    private String name;

    /**

    * 词条类型  1:产品种类 2:产品类型 3:产品单元

    */

    private String type;

    /**

    * 别名

    */

    private String aliasName;

    /**

    * 简介

    */

    private String introduction;

}

/**

* @Author Created by YangMeng on 2021/3/4 14:09

* 公司->生产 产品关系

* 指定关系名称为Production

*/

@Data

@RelationshipEntity(type = "Production")

public class ProductionRelationship {

    @Id

    private String uuid;

    @StartNode

    private CompanyEntryNode startNode;

    @EndNode

    private ProductEntryNode endNode;

    /**

    * 收入占比

    */

    private String incomeProportion;

    /**

    * 毛利率

    */

    private String productGross;

    /**

    * 产品单价

    */

    private String productPrice;

    /**

    * 产能

    */

    private String capacity;

    /**

    * 产能利用率

    */

    private String capacityRatio;

    /**

    * 产能占比

    */

    private String capacityProportion;

}

分别定义产品和生产关系的dao层结构

@Repositorypublic interface ProductEntryRepository extends Neo4jRepository<ProductEntryNode, String> {}

@Repository

public interface ProductionRelationshipRepository extends Neo4jRepository<ProductionRelationship, String> {

  /**

    * 根据产品获取供应商

    * @param productEntryId

    * @return

    */

    @Query("match (c:CompanyEntry)-[:Production]->(p:ProductEntry) where p.productEntryId={productEntryId} return  c.companyEntryId as companyEntryId,c.name as companyName")

    List<DicDto> getCompanyByProductId(String productEntryId);

}

在service层我们定义增删改查操作,单节点产品的我们就不做演示了。我们现在只定义生成关系的操作,在关系里我们实现定义两个接口,第一个addProductionRelationship是把公司和产品创建关系连接起来,第二个getCompanyByProductId是根据产品id查询他的供应商,也就是谁生产了它

/**

* @Author Created by YangMeng on 2021/3/4 15:29

*/

public interface ProductionRelationshipService {

    /**

    * 添加公司产品 关系

    *

    * @param startNode

    * @param toNode

    * @return

    */

    ProductionRelationship addProductionRelationship(CompanyEntryNode startNode, ProductEntryNode toNode);

    /**

    * 添加公司产品 关系

    *

    * @param startNodeId

    * @param toNodeId

    * @return

    */

    ProductionRelationship addProductionRelationship(String startNodeId, String toNodeId);

    /**

    * 获取产品的供应商公司

    *

    * @param productEntryId

    * @return

    */

    List<DicDto> getCompanyByProductId(String productEntryId);

接着实现接口

@Service

public class ProductionRelationshipServiceImpl implements ProductionRelationshipService {

    @Autowired

    private ProductionRelationshipRepository productionRelationshipRepository;

    @Autowired

    private CompanyEntryRepository companyEntryRepository;

    @Autowired

    private ProductEntryRepository productEntryRepository;

    /**

    * 添加公司产品 关系

    *

    * @param startNode

    * @param toNode

    * @return

    */

    @Override

    public ProductionRelationship addProductionRelationship(CompanyEntryNode startNode, ProductEntryNode toNode) {

        ProductionRelationship productionRelationship = new ProductionRelationship();

        productionRelationship.setStartNode(startNode);

        productionRelationship.setEndNode(toNode);

        //添加属性

        productionRelationship.setUuid(UuidUtils.generate());

        ProductionRelationship save = productionRelationshipRepository.save(productionRelationship);

        return save;

    }

    /**

    * 添加公司产品 关系

    *

    * @param startNodeId

    * @param toNodeId

    * @return

    */

    @Override

    public ProductionRelationship addProductionRelationship(String startNodeId, String toNodeId) {

        Optional<CompanyEntryNode> byId = companyEntryRepository.findById(startNodeId);

        Optional<ProductEntryNode> byId1 = productEntryRepository.findById(toNodeId);

        if (byId.isPresent() && byId1.isPresent()) {

            return addProductionRelationship(byId.get(), byId1.get());

        }

        return new ProductionRelationship();

    }

    /**

    * 获取产品的供应商公司

    *

    * @param productEntryId

    * @return

    */

    @Override

    public List<DicDto> getCompanyByProductId(String productEntryId) {

        return productionRelationshipRepository.getCompanyByProductId(productEntryId);

    }

}

创建controller调用

@RestController

@RequestMapping(value = "productionRelationship")

@Slf4j

public class ProductionRelationshipController {

    @Autowired

    private ProductionRelationshipService productionRelationshipService;

    /**

    * 关联公司产品 关系

    *

    * @param startId

    * @param endId

    * @return

    */

    @GetMapping(value = "addRelationship")

    public WebResInfo addRelationship(String startId, String endId) {

        log.info("addRelationship->startId:{},endId:{}", startId, endId);

        WebResInfo webResInfo = new WebResInfo();

        try {

            webResInfo.setCode(WebResCode.Successful);

            ProductionRelationship productionRelationship = productionRelationshipService.addProductionRelationship(startId, endId);

            webResInfo.setData(productionRelationship);

        } catch (Exception e) {

            log.error("addRelationship error:{}", e);

            webResInfo.setCode(WebResCode.Server_Bug_Exception);

            webResInfo.setMessage(e.getMessage());

        }

        return webResInfo;

    }

    /**

    * 根据产品获取供应商信息

    *

    * @param productEntryId

    * @return

    */

    @GetMapping(value = "getCompanyByProductId")

    public WebResInfo getCompanyByProductId(String productEntryId) {

        log.info("getCompanyByProductId->productEntryId:{}", productEntryId);

        WebResInfo webResInfo = new WebResInfo();

        try {

            webResInfo.setCode(WebResCode.Successful);

            List<DicDto> companyByProductId = productionRelationshipService.getCompanyByProductId(productEntryId);

            webResInfo.setData(companyByProductId);

        } catch (Exception e) {

            log.error("getCompanyByProductId error:{}", e);

            webResInfo.setCode(WebResCode.Server_Bug_Exception);

            webResInfo.setMessage(e.getMessage());

        }

        return webResInfo;

    }

我们新增一个公司“阿里巴巴”companyEntryId为2,在新增一个产品“宝马X5”productEntryId为6,然后我们创建他们两个的关系用postman调用

基于以上关系的创建,现在我们查找生产iPhone的公司有哪些

@Query("match (c:CompanyEntry)-[:Production]->(p:ProductEntry) where p.productEntryId={productEntryId} return c.companyEntryId as companyEntryId,c.name as companyName")

    List<DicDto> getCompanyByProductId(String productEntryId);


这里就是查询产品id为{productEntryId}的对应的公司,并且指定了关系是Production,然后就可以查出来苹果公司了。具体请求大家试试就好了

本篇主要介绍关系类型,相信大家有了一定的了解,其实neo4j里最主要还是语义化查询,有了关系我们就可以进行这样一系列查询操作,比如我搜索“iPhone12的生产商是谁”可以查询产品节点是iPhone,对应关系是生产,然后就出来了苹果公司了。这只是其中一小部分,后续还有很多。

彩蛋

下篇文章我们来讲讲Springboot同时绑定neo4j和mysql两个数据源。

关注下方公众号,了解最新内容!!!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,607评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,239评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,960评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,750评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,764评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,604评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,347评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,253评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,702评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,893评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,015评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,734评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,352评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,934评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,052评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,216评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,969评论 2 355

推荐阅读更多精彩内容

  • 1. 什么是Neo4j? Neo4j是一个高性能的NOSQL图形数据库,它将结构化数据存储在网络上而不是表中。它是...
    Whoami令狐冲阅读 529评论 0 3
  • 这个问题,要追述到以前的一次爬虫。以前需要做一个需求,就是去爬天眼查的企业信息。当时有这么一个需求人物关系图,那时...
    茶艺瑶阅读 2,613评论 0 0
  • SpringBoot是Spring家族中的一个全新的框架,它用来简化Spring应用程序的创建和开发过程,提供了各...
    细肥尸丁阅读 4,330评论 0 0
  • 1.Lombok插件 对于开发人员来说,我要解释这个什么意思,你肯定也是一知半解,直接来代码解释吧 1.1 代码演...
    奥莉安娜的棒棒糖阅读 1,585评论 0 0
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,639评论 18 399