Springboot整合Neo4j--- 通过neo4j-java-driver

我们是前后端分离的项目,前端使用的zoomchat 博主不写前端

第一步 添加依赖

    
      org.neo4j.driver
      neo4j-java-driver
      1.5.0
    

第二步 将数据源注入到Spring

	@Bean("neo4jDriver")
	public Driver session() {
		//neo4j的服务器的配置
		Neo4jSetting neo4jSetting = settingService.getNeo4jSetting();
		String url = neo4jSetting.getHost();
		String username = neo4jSetting.getUsername();
		String password = neo4jSetting.getPassword();

		//配置连接池大小
		Config config = Config.build().withEncryption()
				.withTrustStrategy(Config.TrustStrategy.trustAllCertificates())
				.withMaxConnectionPoolSize(32).toConfig();
		Driver driver= GraphDatabase.driver(url, AuthTokens.basic(username, password), config);
		return driver;
	}

 

第三步使用  

   @Autowired
    private Driver neo4jDriver;

 

        Session session = neo4jDriver.session();
        try{
            String stateMent = "match p=(c:Company)-[*]->(:Company) where c.id = \""+id+"\" return p";
            StatementResult result = session.run(stateMent);
            List recordList =  result.list();
            recordList.forEach(record -> {
                Iterator iterator = record.get("p").asPath().iterator();
                while (iterator.hasNext()) {
                    Path.Segment s = iterator.next();
                    Map snodeMap= new HashMap<>();
                    Map enodeMap= new HashMap<>();
                    Map relationshipMap = new HashMap<>();
                    Map sMap = s.start().asMap();
                    snodeMap.put(Neo4jUtil.NAME,sMap.get("NAME"));
                    Long sId = Long.parseLong(String.valueOf(sMap.get("id"))) ;
                    snodeMap.put(Neo4jUtil.ID,sId);
               }
            });
        }finally {
            session.close();
        }

如有疑问请在下方留言

你可能感兴趣的:(Springboot整合Neo4j--- 通过neo4j-java-driver)