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

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

(1)pom.xml

<?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

#neo4j
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456
spring.data.neo4j.uri=http://10.17.12.158:7474

2、实体类

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;
}

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

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> {

}
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

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);
}
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

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
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值