在上几篇的文章中,基于Springboot+Neo4j搭建了框架,做了一些节点与关系的常用操作。实际开发中还是会遇到一些问题,比如从Neo4j查询的数据如何转化成我们需要的结果?定义的Dto怎么接收到查询的结果呢?今天就来讲讲如何返回自定义的结果。
还是以公司生产产品为例子。第一种返回方式:关系对象,节点对象。这种返回方式就是把定义的关系类和节点类直接作为接收对象返回。
首先定义model里两个类:公司节点类(CompanyEntryNode),生产关系类(ProductionRelationship)
@NodeEntity(label = "CompanyEntry")
@Data
public class CompanyEntryNode {
@Id
private String companyEntryId;
/\*\*
\* 模板id
\*/
private String templateId;
/\*\*
\* 名称
\*/
private String name;
/\*\*
\* 类型
\*/
private String type;
/\*\*
\* 别名
\*/
private String aliasName;
}
生产关系类(ProductionRelationship)
@Data
@RelationshipEntity(type = "Production")
public class ProductionRelationship {
@Id
private String uuid;
@StartNode
private CompanyEntryNode startNode;
@EndNode
private ProductEntryNode endNode;
/\*\*
\* 创建关系词条的关系词条名称,例如:阿博茨公司 生产 产业链图谱
\*/
private String relationEntryName;
/\*\*
\* 名称
\*/
private String name;
/\*\*
\* 别名
\*/
private String aliasName;
/\*\*
\* 简介
\*/
private String introduction;
}
在公司的dao层就直接返回CompanyEntryNode就行了
@Repository
public interface CompanyEntryRepository extends Neo4jRepository<CompanyEntryNode, String> {
/\*\*
\* 通过名称查询公司
\* @param name
\* @return
\*/
@Query("match (c:CompanyEntry) where c.name={name} return c")
CompanyEntryNode getEntryByName(String name);
}
同时在生产关系的dao层直接返回ProductionRelationship,这里需要注意的是我在match查询时定义了一个productionRelationship 返回所有的查询结果,这样做的目的是把ProductionRelationshipg关系中的开始节点@StartNode和结束节点@EndNode也都返回了出来。
@Repository
public interface ProductionRelationshipRepository extends Neo4jRepository<ProductionRelationship, String> {
\*\*
\* 通过公司词条id和产品词条id 查询 ProductionRelationship
\*
\* @param companyEntryId 公司词条id
\* @param productEntryId 产品词条id
\* @param relationName 关系类型名称
\* @return ProductionRelationship
\*/
@Query("MATCH productionRelationship = (c:CompanyEntry)-\[r\]->(p:ProductEntry) " +
"where type(r) = {relationName} " +
"and c.companyEntryId={companyEntryId} " +
"and p.productEntryId = {productEntryId}" +
"return productionRelationship ")
ProductionRelationship findProductionRelationship(String companyEntryId, String productEntryId, String relationName);
}
查询生产关系的controller结果如下

这种结果包含了开始节点与结束节点的信息,如果不想要这些信息,只想定义一个Dto返回生产关系的属性信息。那该怎么做呢?这就用到了第二种接收方式-自定义类。这种定义方式就是只关心定义的信息。先在Dto定义一个类RelationshipDto ,这里要注意,我们用到了注解@QueryResult,他代表了Neo4j查询结果返回的类,这个注解不加的话会接收不到返回数据。
@Data
@QueryResult
public class RelationshipDto {
/\*\*
\* 关系uuid
\*/
private String uuid;
/\*\*
\* 产品名称
\*/
private String productName;
/\*\*
\* 产品uuid
\*/
private String productEntryId;
/\*\*
\* 收入占比
\*/
private String incomeProportion;
}
接着我们到dao层定义返回一个集合结果List ,并且对应的值都要像 r.uuid as uuid这样单独的重命名下。这样自定义返回结果就OK了。
@Repository
public interface ProductionRelationshipRepository extends Neo4jRepository<ProductionRelationship, String> {
\*\*
\* 通过公司词条id和产品词条id 查询 ProductionRelationship
\*
\* @param companyEntryId 公司词条id
\* @param productEntryId 产品词条id
\* @param relationName 关系类型名称
\* @return ProductionRelationship
\*/
@Query("MATCH (c:CompanyEntry)-\[r\]->(p:ProductEntry) " +
"where type(r) = {relationName} " +
"and c.companyEntryId={companyEntryId} " +
"and p.productEntryId = {productEntryId}" +
"return r.uuid as uuid,p.name as productName,p.productEntryId as productEntryId")
List<RelationshipEntryDto> findRelationship(String companyEntryId, String productEntryId, String relationName);
}
我们controller层请求返回结果

两种自定义返回结果就讲完了,当然我们也可以返回单独的boolean类型,String类型等结果就不说了。
彩蛋

下期文章开始我们谈谈Neo4j基于Lucene的搜索,以及用Neo4j做语义搜索等内容。
- 本期完 -
有疑问请点赞哈
,我会及时回复。由于微信限制了公众号留言功能,有问题你可以直接发公众号聊天,我会在下期末尾解答问题。
为方便看最新内容,记得关注哦! 
216

被折叠的 条评论
为什么被折叠?



