1. 程式人生 > 其它 >Spring boot 整合Neo4j 實現動態Cypher

Spring boot 整合Neo4j 實現動態Cypher

技術標籤:spring bootneo4j

提到spring boot整合Neo4j,一般都會提到spring-data-neo4j,使用類似於jpa的方式,使用entity去maintain,但是如果想要新增動態關係或者動態的node,就算是@Query也是不夠用了

使用OGM

其實這個包也被spring-data-neo4j引入了,所以不必要單獨引入
在這裡插入圖片描述


import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.slf4j.Logger;
import
org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.neo4j.ogm.model.Result; @Service public class NodeService { private final Logger log = LoggerFactory.getLogger(NodeService.class); @Autowired private
SessionFactory sessionFactory; public Result searchPath(SearchPathDTO searchPathDTO) { String cypher = "MATCH (n:" + searchPathDTO.getFromNode() + "{" + searchPathDTO.getProperity() + ":'" + searchPathDTO.getValue() + "'}) " +
"CALL apoc.path.subgraphAll(n,{relationshipFilter: '<|>',minLevel: 0,maxLevel: -1}) " + "YIELD nodes,relationships RETURN nodes, relationships"; Session session = sessionFactory.openSession(); return session.query(cypher, new HashMap<>(), false); } }

這樣以來,就可以動態拼接cypher,更加靈活,可以實現更加複雜的查詢了.