springboot整合neo4j

springboot 整合 Neo4j

创建 SpringBoot 应用 springboot 版本为 2.3.0.RELEASE

添加依赖

<dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

添加配置文件

spring.data.neo4j.uri=bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456

创建节点类 UserNode

//表示节点类型
@NodeEntity(label="User")
@Data
public class UserNode {

    @Id
    private Long nodeId;

    @Property
    private String userId;

    @Property
    private String name;

    @Property
    private int age;
    
}

创建关系

//表示关系类型
@RelationshipEntity(type="UserRelation")
@Data
public class UserRelation {

    @Id
    private Long id;

    @StartNode
    private UserNode startNode;

    @EndNode
    private UserNode endNode;
}

创建UserRepository

@Component
public interface UserRepository extends Neo4jRepository<UserNode,Long> {

    /**
     * 查询所有节点
     * @return
     */
    @Query("MATCH (n:User) RETURN n ")
    List<UserNode> getUserNodeList();

    /**
     * 创建节点
     * @param userId
     * @param name
     * @param age
     * @return
     */
    @Query("create (n:User{userId:$userId,age:$age,name:$name}) RETURN n ")
    List<UserNode> addUserNodeList(@Param("userId") String userId,@Param("name") String name, @Param("age")int age);
}

创建 UserRelationRepository


@Component
public interface UserRelationRepository extends Neo4jRepository<UserRelation,Long> {

    /**
     * 通过id 查询关系
     * @param firstUserId
     * @param secondUserId
     * @return
     */
    @Query("match p=(n:User)<-[r:FRIEND]->(n1:User) where n.userId=$firstUserId and n1.userId=$secondUserId return p")
    List<UserRelation> findUserRelationByEachId(@Param("firstUserId") String firstUserId, @Param("secondUserId") String secondUserId);

    /**
     * 添加关系
     * @param firstUserId
     * @param secondUserId
     * @return
     */
    @Query("match (fu:User),(su:User) where fu.userId=$firstUserId and su.userId=$secondUserId create p=(fu)-[r:FRIEND]->(su) return p")
    List<UserRelation> addUserRelation(@Param("firstUserId") String firstUserId, @Param("secondUserId") String secondUserId);

}

创建 UserService

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserRelationRepository userRelationRepository;

    public void addUserNode(UserNode userNode) {
        userRepository.addUserNodeList(userNode.getUserId(),userNode.getName(), userNode.getAge());
    }

    public void addRelationship(String firstId,String secondId){
        userRelationRepository.addUserRelation(firstId,secondId);
    }

    public void findUserRelationByEachId(String firstId,String secondId){
        userRelationRepository.findUserRelationByEachId(firstId, secondId);
    }
}

创建 controller

@RestController
public class Neo4jController {

    @Autowired
    private UserService userService;

    @RequestMapping("/saveUser")
    public String saveUserNode() {

        UserNode node = new UserNode();
        node.setNodeId(1l);
        node.setUserId("2");
        node.setName("赵六");
        node.setAge(26);

        userService.addUserNode(node);
        return "success";       
    }


    @RequestMapping("/addship")
    public String saveShip(){
        userService.addRelationship("1","2");
        return "success";
    }

    @RequestMapping("/findShip")
    public String findShip(){
        userService.findUserRelationByEachId("1","2");
        return "success";
    }

}

给启动类添加注解

@SpringBootApplication
@EnableNeo4jRepositories(basePackages="com.yao.neo4j.repository")
@EntityScan(basePackages="com.yao.neo4j.entity")
public class Neo4jApplication {

    public static void main(String[] args) {
        SpringApplication.run(Neo4jApplication.class, args);
    }

}

启动测试,插入两个节点

在这里插入图片描述
添加关系
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_子栖_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值