前言
SpringBoot引入neo4j
org.springframework.boot
spring-boot-starter-data-neo4j
大多数时候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;
/**
* neo4j 节点实体类
* @author YangBM
*/
@Data
public class Neo4jBasicNode implements Serializable {
private static final long serialVersionUID = 1L;
/**
* id
*/
private Long id;
/**
* 标签
*/
private List labels;
/**
* 标签属性
*/
private Map property;
}
Neo4jBaiscRelation
//关系实体类
package com.troy.keeper.modules.desk.entity.neo4j;
import lombok.Data;
import java.io.Serializable;
import java.util.Map;
/**
* 关系
*/
@Data
public class Neo4jBaiscRelation implements Serializable {
private static final long serialVersionUID = 1L;
/**
* id
*/
private Long id;
/**
* 标签
*/
private String type;
/**
* 标签属性
*/
private Map property;
}
Neo4jQueryRelation
//查询关系的时候返回的对象封装的实体类
package com.troy.keeper.modules.desk.entity.neo4j;
import lombok.Data;
import java.io.Serializable;
import java.util.Map;
/**
* 关系
* @author YangBM
*/
@Data
public class Neo4jQueryRelation implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 开始节点id
*/
private Long start;
/**
* 结束节点id
*/
private Long end;
/**
* 关系类型
*/
private String type;
/**
* id
*/
private Long id;
/**
* 标签属性
*/
private Map 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;
/**
* 基础返回关系VO
* 关系
* @author YangBM
*/
@Data
public 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;
/**
* 将关系的实体类,转换换成cypherSql需要字符串类型的vo
* Util里面会用到
* @author YangBM
*/
@Data
public 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.*;
/**
* @author YangBM
* Neo4J工具类
*/
@Component
public class Neo4jUtil {
/**
* init map序列号
*/
private static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
}
@SneakyThrows
public static String propertiesMapToPropertiesStr(Map map) {
map.entrySet().removeIf(entry -> Func.isEmpty(entry.getValue()));
return mapper.writeValueAsString(map);
}
@Resource
private Session session;
public Session getSession() {
return this.session;
}
/**
* 获取所有的标签名称
*
* @return
*/
public List getAllLabelName() {
String cypherSql = "match (n) return distinct labels(n) as name";
Result query = session.query(cypherSql, new HashMap<>());
ArrayList labelNames = new ArrayList<>();
for (Map map : query.queryResults()) {
String[] names = (String[]) map.get("name");
for (String name : names) {
labelNames.add(name);
}
}
return labelNames;
}
/**
* 获取所有的关系名称
*
* @return
*/
public List getAllRelationName() {
String cypherSql = "MATCH ()-[r]-() RETURN distinct type(r) as name";
Result query = session.query(cypherSql, new HashMap<>());
ArrayList relationNames = new ArrayList<>();
for (Map map : query.queryResults()) {
relationNames.add(map.get("name").toString());
}
return relationNames;
}
/**
* 按条件查询节点
*
* @param node
* @return 返回节点集合
*/
public List 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 nodeList = new ArrayList<>();
Iterable
到此这篇关于SpringBoot结合Neo4j自定义cypherSql的文章就介绍到这了,更多相关SpringBoot自定义cypherSql内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!