这篇文章主要为大家展示了“SpringBoot如何结合Neo4j自定义cypherSql”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“SpringBoot如何结合Neo4j自定义cypherSql”这篇文章吧。
前言
SpringBoot引入neo4j
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency>
<spring.boot.version>2.2.11.RELEASE</spring.boot.version>
大多数时候Neo4j结合springboot都是按照实体类的映射,进行对象形式的创建节点,导致了节点属性的不可配性。结合这种问题,结合目前开发经验,可以使用neo4j 框架的session 进行原生cypherSql的执行。所以结合使用session进行动态可配等思路,建立一个Neo4jUtil..
Neo4jUtil作用
SpringBoot结合neo4j,自定义封装cypherSql进行操作。实际就是使用neo4j的Session.执行一些cypherSql操作,然后封装了一些方法供大家在既可以使用springboot的对象化操作方式的前提创建节点或者关系,也可以自定义继续封装一些特殊需求的方法,避免大家造轮子。
相关代码
application.yml
spring: data: neo4j: uri: bolt://127.0.0.1:7688 username: neo4j password: neo4j
config
package com.troy.keeper.modules.desk.config;import org.neo4j.ogm.session.SessionFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;@Configuration@EnableNeo4jRepositories("com.troy.keeper.desc.repository") // 声明neo4j repository存放地址public class Neo4jConfig { @Value("${spring.data.neo4j.uri}") private String uri; @Value("${spring.data.neo4j.username}") private String userName; @Value("${spring.data.neo4j.password}") private String password; @Bean public org.neo4j.ogm.config.Configuration getConfiguration() { org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder().uri(uri).connectionPoolSize(100).credentials(userName, password).withBasePackages("com.troy.keeper.desc.repository").build(); return configuration; } @Bean public SessionFactory sessionFactory() { return new SessionFactory(getConfiguration()); } @Bean("neo4jTransaction") public Neo4jTransactionManager neo4jTransactionManager(SessionFactory sessionFactory) { return new Neo4jTransactionManager(sessionFactory); }}
Entity
Neo4jBasicNode
//节点实体类package com.troy.keeper.modules.desk.entity.neo4j;import lombok.Data;import java.io.Serializable;import java.util.List;import java.util.Map;@Datapublic class Neo4jBasicNode implements Serializable { private static final long serialVersionUID = 1L; private Long id; private List<String> labels; private Map<String, Object> property;}
Neo4jBaiscRelation
//关系实体类package com.troy.keeper.modules.desk.entity.neo4j;import lombok.Data;import java.io.Serializable;import java.util.Map;@Datapublic class Neo4jBaiscRelation implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String type; private Map<String, Object> property;}
Neo4jQueryRelation
//查询关系的时候返回的对象封装的实体类package com.troy.keeper.modules.desk.entity.neo4j;import lombok.Data;import java.io.Serializable;import java.util.Map;@Datapublic class Neo4jQueryRelation implements Serializable { private static final long serialVersionUID = 1L; private Long start; private Long end; private String type; private Long id; private Map<String, Object> property;}
VO
Neo4jBasicRelationReturnVO
package com.troy.keeper.modules.desk.vo.neo4j;import com.troy.keeper.modules.desk.entity.neo4j.Neo4jBasicNode;import com.troy.keeper.modules.desk.entity.neo4j.Neo4jQueryRelation;import lombok.Data;import java.io.Serializable;@Datapublic class Neo4jBasicRelationReturnVO implements Serializable { private static final long serialVersionUID = 1L; private Neo4jBasicNode start; private Neo4jQueryRelation relationship; private Neo4jBasicNode end;}
RelationVO
package com.troy.keeper.modules.desk.vo.neo4j;import lombok.Data;@Datapublic class RelationVO { private String relationLabelName; private String startLabelName; private String startNodeProperties; private String relationProperties; private String endNodeProperties; private String endLabelName; private String level;}
Util
Neo4jUtil
package com.troy.keeper.modules.desk.util;import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.databind.ObjectMapper;import com.troy.keeper.core.tool.utils.BeanUtil;import com.troy.keeper.core.tool.utils.Func;import com.troy.keeper.modules.desk.dto.neo4j.Neo4jSaveRelationDTO;import com.troy.keeper.modules.desk.dto.neo4j.RelationDTO;import com.troy.keeper.modules.desk.entity.neo4j.Neo4jBaiscRelation;import com.troy.keeper.modules.desk.entity.neo4j.Neo4jBasicNode;import com.troy.keeper.modules.desk.entity.neo4j.Neo4jQueryRelation;import com.troy.keeper.modules.desk.vo.neo4j.Neo4jBasicRelationReturnVO;import com.troy.keeper.modules.desk.vo.neo4j.RelationVO;import lombok.SneakyThrows;import org.apache.commons.collections.IteratorUtils;import org.apache.commons.lang3.StringUtils;import org.neo4j.driver.internal.InternalPath;import org.neo4j.driver.types.Node;import org.neo4j.driver.types.Relationship;import org.neo4j.ogm.model.Property;import org.neo4j.ogm.model.Result;import org.neo4j.ogm.response.model.NodeModel;import org.neo4j.ogm.session.Session;import org.springframework.stereotype.Component;import javax.annotation.Resource;import java.util.*;@Componentpublic class Neo4jUtil { private static final ObjectMapper mapper = new ObjectMapper(); static { mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false); } @SneakyThrows public static String propertiesMapToPropertiesStr(Map<String,Object> map) { map.entrySet().removeIf(entry -> Func.isEmpty(entry.getValue())); return mapper.writeValueAsString(map); } @Resource private Session session; public Session getSession() { return this.session; } public List<String> getAllLabelName() { String cypherSql = "match (n) return distinct labels(n) as name"; Result query = session.query(cypherSql, new HashMap<>()); ArrayList<String> labelNames = new ArrayList<>(); for (Map<String, Object> map : query.queryResults()) { String[] names = (String[]) map.get("name"); for (String name : names) { labelNames.add(name); } } return labelNames; } public List<String> getAllRelationName() { String cypherSql = "MATCH ()-[r]-() RETURN distinct type(r) as name"; Result query = session.query(cypherSql, new HashMap<>()); ArrayList<String> relationNames = new ArrayList<>(); for (Map<String, Object> map : query.queryResults()) { relationNames.add(map.get("name").toString()); } return relationNames; } public List<Neo4jBasicNode> queryNode(Neo4jBasicNode node) { String cypherSql = ""; if (Func.isNotEmpty(node.getId())) { cypherSql = String.format("MATCH (n) where id(n)=%s return n", node.getId()); } else { String labels = ""; if (Func.isNotEmpty(node.getLabels())) { labels = ":`" + String.join("`:`", node.getLabels()) + "`"; } String property = ""; if (Func.isNotEmpty(node.getProperty())) { property = Neo4jUtil.propertiesMapToPropertiesStr(node.getProperty()); } cypherSql = String.format("match(n%s%s) return n", labels, property); } Result query = session.query(cypherSql, new HashMap<>()); ArrayList<Neo4jBasicNode> nodeList = new ArrayList<>(); Iterable<Map<String, Object>> maps = query.queryResults(); for (Map<String, Object> map : maps) { NodeModel queryNode = (NodeModel) map.get("n"); Neo4jBasicNode startNodeVo = new Neo4jBasicNode(); startNodeVo.setId(queryNode.getId()); startNodeVo.setLabels(Arrays.asList(queryNode.getLabels())); List<Property<String, Object>> propertyList = queryNode.getPropertyList(); HashMap<String, Object> proMap = new HashMap<>(); for (Property<String, Object> stringObjectProperty : propertyList) { if (proMap.containsKey(stringObjectProperty.getKey())) { throw new RuntimeException("数据重复"); } proMap.put(stringObjectProperty.getKey(), stringObjectProperty.getValue()); } startNodeVo.setProperty(proMap); nodeList.add(startNodeVo); } session.clear(); return nodeList; } public boolean createNode(Neo4jBasicNode node, Boolean nodup) { String labels = ""; if (Func.isNotEmpty(node.getLabels())) { labels = ":`" + String.join("`:`", node.getLabels()) + "`"; } String property = ""; if (Func.isNotEmpty(node.getProperty())) { property = Neo4jUtil.propertiesMapToPropertiesStr(node.getProperty()); } String cypherSql = String.format("%s(%s%s)", nodup ? "MERGE" : "create", labels, property); Result query = session.query(cypherSql, new HashMap<>()); session.clear(); return query.queryStatistics().getNodesCreated() > 0; } public boolean createNode(Neo4jBasicNode node) { return this.createNode(node, false); } public boolean recreateNode(Neo4jBasicNode node){ List<String> saveLabels = node.getLabels(); Map<String, Object> saveProperty = node.getProperty(); Set<String> savePropertyKeySet = saveProperty.keySet(); //查询用属性查询节点是不是存在。 //存在比较标签的lable1是不是一样。不一样就这个查询到的节点(少了就新增标签,多了就删除标签) Neo4jBasicNode queryNode= BeanUtil.copy(node,Neo4jBasicNode.class); queryNode.setLabels(null); List<Neo4jBasicNode> queryNodeList = this.queryNode(queryNode); if (queryNodeList.isEmpty()){ return createNode(node,true); } for (Neo4jBasicNode neo4jBasicNode : queryNodeList) { //处理标签 List<String> queryLabels = neo4jBasicNode.getLabels(); ArrayList<String> addLabels = new ArrayList<>(); for (String saveLabel : saveLabels) { if (!queryLabels.contains(saveLabel)){ //新增标签 addLabels.add(saveLabel); } } String addLabelStr=addLabels.isEmpty()?"":("e:"+String.join(":",addLabels)); //处理属性 Map<String, Object> queryProperty = neo4jBasicNode.getProperty(); Set<String> queryPropertyKeySet = queryProperty.keySet(); HashMap<String, Object> addPropertyMap = new HashMap<>(); for (String savePropertyKey: savePropertyKeySet) { if (!queryPropertyKeySet.contains(savePropertyKey)){ addPropertyMap.put(savePropertyKey,saveProperty.get(savePropertyKey)); } } String addPropertyStr=addPropertyMap.isEmpty()?"":(",e+="+ Neo4jUtil.propertiesMapToPropertiesStr(addPropertyMap)); if(StringUtils.isAllEmpty(addPropertyStr,addPropertyStr)){ return true; } String addLabelCypherSql =String.format("MERGE (e) with e where id(e)=%s set %s %s return count(e) as count",neo4jBasicNode.getId(),addLabelStr,addPropertyStr); Result query = session.query(addLabelCypherSql, new HashMap<>()); System.out.println("跟新了:"+neo4jBasicNode.getId()); session.clear(); } //创建不重复节点 return true; }; public Long batchCreateNode(List<Neo4jBasicNode> nodeList) { return this.batchCreateNode(nodeList, false); } public Long batchCreateNode(List<Neo4jBasicNode> nodeList, Boolean nodup) { ArrayList<Neo4jBasicNode> addNode = new ArrayList<>(); //验证 for (Neo4jBasicNode neo4jBasicNode : nodeList) { if ((!nodup) || this.queryNode(neo4jBasicNode).size() == 0) { addNode.add(neo4jBasicNode); } } String cypherSql = "create"; ArrayList<String> content = new ArrayList<>(); for (Neo4jBasicNode node : addNode) { String labels = ""; if (Func.isNotEmpty(node.getLabels())) { labels = ":`" + String.join("`:`", node.getLabels()) + "`"; } String property = ""; if (Func.isNotEmpty(node.getProperty())) { property = Neo4jUtil.propertiesMapToPropertiesStr(node.getProperty()); } content.add(String.format("(%s%s)", labels, property)); } cypherSql += String.join(",", content); if (content.size() == 0) { return 0L; } Result query = session.query(cypherSql, new HashMap<>()); session.clear(); return Long.valueOf(query.queryStatistics().getNodesCreated()); } public Integer delNode(Neo4jBasicNode node, boolean delRelation) { String cypherSql = ""; if (Func.isNotEmpty(node.getId())) { cypherSql = String.format("MATCH (n) where id(n)=%s ", node.getId()); } else { String labels = ""; if (Func.isNotEmpty(node.getLabels())) { labels = ":`" + String.join("`:`", node.getLabels()) + "`"; } String property = ""; if (Func.isNotEmpty(node.getProperty())) { property = Neo4jUtil.propertiesMapToPropertiesStr(node.getProperty()); } cypherSql = String.format("match(n%s%s) ", labels, property); } if (delRelation) { cypherSql += "DETACH DELETE n"; } else { //删除不存在关系的节点 cypherSql += " where not exists((n)-[]-()) DELETE n"; } Result query = session.query(cypherSql, new HashMap<>()); session.clear(); return query.queryStatistics().getNodesDeleted(); } public Integer delNode(Neo4jBasicNode node) { return this.delNode(node, false); } public int queryNodeCreateRelation(Neo4jBasicNode start, Neo4jBasicNode end, Neo4jBaiscRelation relation) { Neo4jSaveRelationDTO dto = new Neo4jSaveRelationDTO(); dto.setStart(start); dto.setEnd(end); dto.setRelationship(relation); return queryNodeCreateRelation(dto); } public int queryNodeCreateRelation(Neo4jSaveRelationDTO saveRelation) { //开始节点和结束节点验证 String cypherSql = ""; String startLable = ""; if (Func.isNotEmpty(saveRelation.getStart().getLabels())) { startLable = ":`" + String.join("`:`", saveRelation.getStart().getLabels()) + "`"; } String startProperty = ""; if (Func.isNotEmpty(saveRelation.getStart().getProperty())) { startProperty = Neo4jUtil.propertiesMapToPropertiesStr(saveRelation.getStart().getProperty()); } String endLable = ""; if (Func.isNotEmpty(saveRelation.getEnd().getLabels())) { endLable = ":`" + String.join("`:`", saveRelation.getEnd().getLabels()) + "`"; } String endProperty = ""; if (Func.isNotEmpty(saveRelation.getEnd().getProperty())) { endProperty = Neo4jUtil.propertiesMapToPropertiesStr(saveRelation.getEnd().getProperty()); } String startWhere = ""; if (Func.isNotEmpty(saveRelation.getStart().getId())) { startWhere += " where id(start)=" + saveRelation.getStart().getId(); startLable = ""; startProperty = ""; } String endWhere = ""; if (Func.isNotEmpty(saveRelation.getEnd().getId())) { endWhere += " where id(end)=" + saveRelation.getEnd().getId(); endLable = ""; endProperty = ""; } String relationType = ""; if (Func.isNotEmpty(saveRelation.getRelationship().getType())) { relationType = ":`" + saveRelation.getRelationship().getType() + "`"; } if (Func.isEmpty(relationType)) { throw new RuntimeException("关系名称不能为空!"); } String relationProperty = ""; if (Func.isNotEmpty(saveRelation.getRelationship().getProperty())) { relationProperty = Neo4jUtil.propertiesMapToPropertiesStr(saveRelation.getRelationship().getProperty()); } cypherSql = String.format("MATCH (start%s%s) %s with start MATCH (end%s%s) %s MERGE (start)-[rep%s%s]->(end)", startLable, startProperty, startWhere, endLable, endProperty, endWhere, relationType, relationProperty); Result query = session.query(cypherSql, new HashMap<>()); session.clear(); return query.queryStatistics().getRelationshipsCreated(); } public boolean creteNodeAndRelation(Neo4jSaveRelationDTO saveRelation) { String cypherSql = ""; String startLable = ""; if (Func.isNotEmpty(saveRelation.getStart().getLabels())) { startLable = ":`" + String.join("`:`", saveRelation.getStart().getLabels()) + "`"; } String startProperty = ""; if (Func.isNotEmpty(saveRelation.getStart().getProperty())) { startProperty = Neo4jUtil.propertiesMapToPropertiesStr(saveRelation.getStart().getProperty()); } String endLable = ""; if (Func.isNotEmpty(saveRelation.getEnd().getLabels())) { endLable = ":`" + String.join("`:`", saveRelation.getEnd().getLabels()) + "`"; } String endProperty = ""; if (Func.isNotEmpty(saveRelation.getEnd().getProperty())) { endProperty = Neo4jUtil.propertiesMapToPropertiesStr(saveRelation.getEnd().getProperty()); } String relationType = ""; if (Func.isNotEmpty(saveRelation.getRelationship().getType())) { relationType = ":`" + saveRelation.getRelationship().getType() + "`"; } if (Func.isEmpty(relationType)) { throw new RuntimeException("关系名称不能为空!"); } String relationProperty = ""; if (Func.isNotEmpty(saveRelation.getRelationship().getProperty())) { relationProperty = Neo4jUtil.propertiesMapToPropertiesStr(saveRelation.getRelationship().getProperty()); } cypherSql = String.format("MERGE (start%s%s)-[rep%s%s]->(end%s%s)", startLable, startProperty, relationType, relationProperty, endLable, endProperty); Result query = session.query(cypherSql, new HashMap<>()); session.clear(); return query.queryStatistics().getRelationshipsCreated() > 0; } public List<Neo4jBasicRelationReturnVO> queryRelation(RelationDTO relationDTO) { RelationVO relationVO = formatRelation(relationDTO); //拼接sql String cypherSql = String.format("MATCH p=(a%s%s)-[r%s%s]->(b%s%s)-[*0..%s]->() RETURN p", relationVO.getStartLabelName(), relationVO.getStartNodeProperties(), relationVO.getRelationLabelName(), relationVO.getRelationProperties(), relationVO.getEndLabelName(), relationVO.getEndNodeProperties(), relationVO.getLevel()); System.out.println(cypherSql); long startTime = System.currentTimeMillis(); Result query = session.query(cypherSql, new HashMap<>()); System.out.println(String.format("耗时%d秒", System.currentTimeMillis() - startTime)); Iterable<Map<String, Object>> maps = query.queryResults(); ArrayList<Neo4jBasicRelationReturnVO> returnList = new ArrayList<>(); for (Map<String, Object> map : maps) { InternalPath.SelfContainedSegment[] ps = (InternalPath.SelfContainedSegment[]) map.get("p"); for (InternalPath.SelfContainedSegment p : ps) { returnList.add(changeToNeo4jBasicRelationReturnVO(p)); } } session.clear(); return returnList; } public RelationVO formatRelation(RelationDTO relationDTO) { RelationVO relationVO = new RelationVO(); //验证 if (Func.isNotEmpty(relationDTO.getRelationLabelName())) { relationVO.setRelationLabelName(":`" + relationDTO.getRelationLabelName()+"`"); } else { relationVO.setRelationLabelName(""); } if (Func.isNotEmpty(relationDTO.getStartLabelName())) { relationVO.setStartLabelName(":`" + relationDTO.getStartLabelName()+"`"); } else { relationVO.setStartLabelName(""); } if (Func.isNotEmpty(relationDTO.getEndLabelName())) { relationVO.setEndLabelName(":`" + relationDTO.getEndLabelName()+"`"); } else { relationVO.setEndLabelName(""); } if (Func.isNotEmpty(relationDTO.getStartNodeProperties())) { relationVO.setStartNodeProperties(Neo4jUtil.propertiesMapToPropertiesStr(relationDTO.getStartNodeProperties())); } else { relationVO.setStartNodeProperties(""); } if (Func.isNotEmpty(relationDTO.getRelationProperties())) { relationVO.setRelationProperties(Neo4jUtil.propertiesMapToPropertiesStr(relationDTO.getRelationProperties())); } else { relationVO.setRelationProperties(""); } if (Func.isNotEmpty(relationDTO.getEndNodeProperties())) { relationVO.setEndNodeProperties(Neo4jUtil.propertiesMapToPropertiesStr(relationDTO.getEndNodeProperties())); } else { relationVO.setEndNodeProperties(""); } if (Func.isNotEmpty(relationDTO.getLevel())) { relationVO.setLevel(relationDTO.getLevel().toString()); } else { relationVO.setLevel(""); } return relationVO; } public Neo4jBasicRelationReturnVO changeToNeo4jBasicRelationReturnVO(InternalPath.SelfContainedSegment selfContainedSegment) { Neo4jBasicRelationReturnVO neo4JBasicRelationReturnVO = new Neo4jBasicRelationReturnVO(); //start Node start = selfContainedSegment.start(); Neo4jBasicNode startNodeVo = new Neo4jBasicNode(); startNodeVo.setId(start.id()); startNodeVo.setLabels(IteratorUtils.toList(start.labels().iterator())); startNodeVo.setProperty(start.asMap()); neo4JBasicRelationReturnVO.setStart(startNodeVo); //end Node end = selfContainedSegment.end(); Neo4jBasicNode endNodeVo = new Neo4jBasicNode(); endNodeVo.setId(end.id()); endNodeVo.setLabels(IteratorUtils.toList(end.labels().iterator())); endNodeVo.setProperty(end.asMap()); neo4JBasicRelationReturnVO.setEnd(endNodeVo); //relationship Neo4jQueryRelation neo4JQueryRelation = new Neo4jQueryRelation(); Relationship relationship = selfContainedSegment.relationship(); neo4JQueryRelation.setStart(relationship.startNodeId()); neo4JQueryRelation.setEnd(relationship.endNodeId()); neo4JQueryRelation.setId(relationship.id()); neo4JQueryRelation.setType(relationship.type()); neo4JQueryRelation.setProperty(relationship.asMap()); neo4JBasicRelationReturnVO.setRelationship(neo4JQueryRelation); return neo4JBasicRelationReturnVO; }}
以上是“SpringBoot如何结合Neo4j自定义cypherSql”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!