暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

Springboot+Neo4j动态传参

Neo4j权威指南 2021-05-16
4663
在最近项目中,经常用到数据的动态传参,比如标签类型,关系类型,或者是属性的名称等等。接下来就一一介绍下。

关系类型传参:

以公司和产品为例,比如公司和产品有生产关系,也有售卖关系,那如果想根据传参来查询具体公司可以这样。其中type(r)就表示什么关系{relationship}就是动态参数。

     @Query("match(m:CompanyEntry)-[r]->(n) where type(r)={relationship} and n.productEntryId={productId} " +
                " return distinct m.name as companyName,m.companyEntryId as companyId ")
    List<SupplyResultDto> getCompanyList(String productId, String relationship);

    标签类型传参:

    标签主要差别在于是数组,所以我们传参的时候要以数组形式。返回的形式也是数组。

       @Query("match (n) where  labels(n) = {labelNames} " +
      " and exists(n.createTime) " +
                  " return n.name as name,labels(n)[0] as labelType, n.type as type " +
      " limit $1 ")
      List<TipEntryDto> getTipEntryList(List<String> labelNames, int limit);


      属性传参:

      比如我想查询公司的某一个属性值,可以是名称,可以是股票代码,也可以是市值,而且这个属性是动态传入的。其中m[$1]就是动态传入获取的值。

            @Query("match(m:CompanyEntry) where m.companyEntryId=$0" +
        " return m.companyEntryId as id,m[$1] as property,m.name as name,labels(m)[0] as labelType,m.type as type")
            List<TableCardDto> getCompanyPropertyByNum(String companyId, String property);

        其他传参:

        还有一些比如大于号,小于号这种情况,是没有办法传参的。但是可以通过编码来变相实现。

              @Query(" optional match(c:CompanyEntry) where c.companyEntryId=$0 " +
          " with c " +
          " optional match(m) where (m.companyEntryId=$0 or m.productEntryId=$0 or m.industryEntryId=$0) and " +
          " case {compare} when '>' then m[$1] > {propertyValue} " +
          " when '>=' then m[$1] >= {propertyValue} " +
          " when '=' then m[$1] = {propertyValue} " +
          " when '<=' then m[$1] <= {propertyValue} " +
          " when '<' then m[$1] < {propertyValue} " +
          " else 1=1 end " +
          " with m,c,case when m.name is null then c else m end as t," +
          " case when exists(c.companyEntryId) then c.companyEntryId when exists(m.companyEntryId) then m.companyEntryId " +
          " when exists(m.productEntryId) then m.productEntryId when exists(m.industryEntryId) then m.industryEntryId end as id" +
          " return id,t[$1] as property,t.name as name,labels(t)[0] as labelType,t.type as type,{propertyName} as propertyName" +
          " limit case {limitValue} when -1 then 1000 else {limitValue} end ")
          List<TableCardDto> getCompanyPropertyByNum(String companyId, String property, String propertyName, String propertyValue, String limit, int limitValue, String compare);


          这是一个传入带属性值,判断大小的查询语句。其中“>,<,=”是动态传参不了的,所以通过case when 来判断传参了。


          - 本期完 -


          有疑问请点赞哈,我会及时回复。由于微信限制了公众号留言功能,有问题你可以直接发公众号聊天,我会在下期文章末尾解答你的问题。


          为方便看最新内容,长按下图图片记得关注哦  




          文章转载自Neo4j权威指南,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

          评论