springboot集成neo4j

        1.在neo4j官网下载windows客户端用来测试。下载社区版community

springboot集成neo4j_第1张图片2.配置neo4j环境变量。将你下载的地址填写到环境变量里

springboot集成neo4j_第2张图片

3.将neo4j环境变量配置到path里。直接粘贴就可以%NEO4J_HOME%\bin;

springboot集成neo4j_第3张图片

4.输入cmd 输入文件路径。然后neo4j.bat console.  等待启动成功。

springboot集成neo4j_第4张图片

注:(1)这里需要将下载的neo4j 中bin目录下所有.ps1文件里的目录内容改为安装后的真实目录不然会启动失败

        (2)如果下载的为4.0之后的版本需要安装jdk11或以上版本不然会启动失败

springboot集成neo4j_第5张图片

5 输入http://localhost:7474 即可登录成功 初始账号密码为 neo4j:neo4j

6.在项目中引入jar包


    org.springframework.boot
    spring-boot-starter-data-neo4j

7.在application.yml中配置neo4j路径以及账号密码。这里如果使用的是http开头路径需要引入jar包。如果不引入默认的是bolt://localhost:7687.


    org.neo4j
    neo4j-ogm-http-driver

springboot集成neo4j_第6张图片

8.项目整体路径

springboot集成neo4j_第7张图片

9.controller层方法testcontroller,这里我没有写serveice方法 而是直接调用数据访问层repository里

package com.wugui.datax.admin.controller;



import com.wugui.datax.admin.entity.Dept;
import com.wugui.datax.admin.entity.RelationShip;

import com.wugui.datax.admin.repository.DeptRepository;
import com.wugui.datax.admin.repository.RelationShipRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author [email protected]
 * @date 2020-11-15
 */
@RestController
@Api(tags = "neo4j测试")
public class TestController {

    @Resource
    private DeptRepository deptRepository;
    @Resource
    private RelationShipRepository relationShipRepository;

    /**
     */
    @GetMapping("create")
    @ApiOperation("创建")

    public void create(){
        String name ="sftp3"; //原表名
        String newname ="ceshi";  //新表名
        String datasource ="mysql";  //新库类型
        List ships = deptRepository.findByName(name);
        //查询是否存在
       if(ships.size()>0){
          System.out.println("====存在");

           //建立节点与节点属性
           Dept  CEO = Dept.builder().deptName(newname).dataSource(datasource).sourceName(datasource).build();
           List depts = new ArrayList<>(Arrays.asList(CEO));
           deptRepository.saveAll(depts);

           //没有判断重复关系
           //创建关系
           relationShipRepository.saveTbale(name,newname);
       }
       else {
          System.out.println("====不存在");
          //建立节点
          Dept CEO = Dept.builder().deptName(name).dataSource(datasource).sourceName(datasource).build();
          Dept dept1 = Dept.builder().deptName(newname).dataSource(datasource).sourceName(datasource).build();
          List depts = new ArrayList<>(Arrays.asList(CEO, dept1));
          deptRepository.saveAll(depts);


          //创建关系
          RelationShip relationShip1 = RelationShip.builder().parent(CEO).child(dept1).field(datasource).build();
          List relationShips = new ArrayList<>(Arrays.asList(relationShip1));
          relationShipRepository.saveAll(relationShips);
      }




    }

    @ApiOperation("获取")
    @GetMapping("get")
    public List> get(Long id){
      String  name ="hive";

        return deptRepository.findByRelationShip(name);
    }

    @GetMapping("deleteRelationShip")
    public void deleteRelationShip(Long id){
        relationShipRepository.deleteById(id);
    }

    @GetMapping("deleteDept")
    public void deleteDept(Long id){
        deptRepository.deleteById(id);
    }

    @GetMapping("deleteAll")
    public void deleteAll(){
        deptRepository.deleteAll();
        relationShipRepository.deleteAll();
    }
}

10.节点实体Dept

package com.wugui.datax.admin.entity;

import jdk.nashorn.internal.objects.annotations.Property;
import lombok.Builder;
import lombok.Data;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;

@NodeEntity(label = "DATASOURCE")  //节点实体的
@Data
@Builder
public class Dept {

    @Id
    @GeneratedValue
    private Long id;

    @Property(name = "deptName")  
    private String deptName;


    @Property(name = "dataSource")
    private String  dataSource;

    @Property(name = "sourceName")
    private String  sourceName;


}

11.关系实体 RelationShip,用来展示两个节点之间的关系

package com.wugui.datax.admin.entity;

import jdk.nashorn.internal.objects.annotations.Property;
import lombok.Builder;
import lombok.Data;
import org.neo4j.ogm.annotation.*;

/**
 * 关系类
 */
@RelationshipEntity(type = "数据流向")
@Data
@Builder
public class RelationShip {

    @Id
    @GeneratedValue
    private Long id;

    @StartNode
    private Dept parent;

    @EndNode
    private Dept child;

    @Property(name = "field")
    private String  field;
}

12 DeptRepository,这里可以用封装好的方法,也可以自己写neo4j的增删改查语句

package com.wugui.datax.admin.repository;

import com.wugui.datax.admin.entity.Dept;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface DeptRepository extends Neo4jRepository {





    @Query("MATCH (n:DATASOURCE) WHERE n.deptName={name} RETURN n ")
    List findByName(@Param("name")String name);


    @Query("match (na:DATASOURCE)-[re]->(nb:DATASOURCE) where nb.deptName ={name} WITH na,re,nb match (nb:DATASOURCE)-[re2]->(nc:DATASOURCE)  return na,re,nb,re2,nc  order  by  STARTNODE(re)desc")
    List> findByRelationShip(@Param("name")String name);
//
//    @Query("CREATE (n:Ceshi { name: 'Vic', title: 'Developer' })  return n")
//    void save1(Dept dept11);
}

总结:引入完neo4j的包与配置好neo4j的账号密码与路径就算是完成了neo4j的集成。这里用到springboot版本为2.3.4。如果是2.4.0或以上实体的注释会有变化 把@NodeEntity换成@Node这样就好使了

你可能感兴趣的:(neo4j,java)