前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Neo4J:spring-boot-starter-data-neo4j简单应用

Neo4J:spring-boot-starter-data-neo4j简单应用

作者头像
程裕强
发布2019-10-24 20:07:59
2.1K0
发布2019-10-24 20:07:59
举报

1、spring-boot-starter-data-neo4j基本环节

(1)pom.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>neo4j-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>neo4j-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-http-driver</artifactId>
            <version>3.2.1</version>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(2)application.properties

代码语言:javascript
复制
#neo4j
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456
spring.data.neo4j.uri=http://10.17.12.158:7474

2、实体类

代码语言:javascript
复制
package com.example.neo4j.bean;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;

@NodeEntity(label = "Person")
@Data
@NoArgsConstructor
public class PersonBean {

    /**
     * @GraphId也不支持
     * 需要改成@Id + @GeneratedValue
     */
    @Id
    @GeneratedValue
    Long id;

    @Property(name = "name")
    private String name;
}
代码语言:javascript
复制
package com.example.neo4j.bean;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.neo4j.ogm.annotation.*;

@RelationshipEntity(type = "LOVES")
@Data
@NoArgsConstructor
public class LoveBean {
    @Id
    @GeneratedValue
    private Long id;

    @StartNode
    private PersonBean startNode;

    @EndNode
    private PersonBean endNode;
}

3、DAO

代码语言:javascript
复制
package com.example.neo4j.dao;

import com.example.neo4j.bean.PersonBean;
import org.springframework.data.neo4j.repository.Neo4jRepository;

public interface PersonRepository extends Neo4jRepository<PersonBean,Long> {

}
代码语言:javascript
复制
package com.example.neo4j.dao;

import com.example.neo4j.bean.LoveBean;
import org.springframework.data.neo4j.repository.Neo4jRepository;

public interface LoveRepository extends Neo4jRepository<LoveBean,Long> {

}

4、service

代码语言:javascript
复制
package com.example.neo4j.service;

import com.example.neo4j.bean.LoveBean;
import com.example.neo4j.bean.PersonBean;

public interface PersonService {

    PersonBean addPerson(PersonBean person);
    PersonBean findOnePerson(long id);
    LoveBean loves(LoveBean love);
}
代码语言:javascript
复制
package com.example.neo4j.service.impl;

import com.example.neo4j.bean.LoveBean;
import com.example.neo4j.bean.PersonBean;
import com.example.neo4j.dao.LoveRepository;
import com.example.neo4j.dao.PersonRepository;
import com.example.neo4j.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private LoveRepository loveRepository;

    @Override
    public PersonBean addPerson(PersonBean person){
        return personRepository.save(person);
    }

    @Override
    public PersonBean findOnePerson(long id) {
        return personRepository.findById(id).get();
    }

    @Override
    public LoveBean loves(LoveBean love) {
        return loveRepository.save(love);
    }

}

5、web

代码语言:javascript
复制
package com.example.neo4j.web;

import com.example.neo4j.bean.LoveBean;
import com.example.neo4j.bean.PersonBean;
import com.example.neo4j.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @RequestMapping("/addPerson/{name}")
    public PersonBean addPerson(@PathVariable("name")String name) {
        PersonBean person=new PersonBean();
        person.setName(name);
        return personService.addPerson(person);
    }

    @RequestMapping("/loves/{id1}/{id2}")
    public LoveBean loves(@PathVariable("id1")String id1,@PathVariable("id2")String id2){
        PersonBean person1=personService.findOnePerson(Long.parseLong(id1));
        PersonBean person2=personService.findOnePerson(Long.parseLong(id2));
        LoveBean love=new LoveBean();
        love.setStartNode(person1);
        love.setEndNode(person2);
        return personService.loves(love);
    }
}

6、运行效果

http://localhost:8080/person/addPerson/qiang

在这里插入图片描述
在这里插入图片描述

http://localhost:8080/person/addPerson/juan

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

http://localhost:8080/person/loves/1154/1156

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

http://localhost:8080/person/loves/1156/1154

在这里插入图片描述
在这里插入图片描述

http://localhost:8080/person/loves/1156/1154

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、spring-boot-starter-data-neo4j基本环节
  • 2、实体类
  • 3、DAO
  • 4、service
  • 5、web
  • 6、运行效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档