Spring Boot使用neo4j

springboot整合neo4j图数据库 https://github.com/CNXXPP/neo4j-demo

1. pom.xml



    4.0.0

    com.example
    demo
    0.0.1-SNAPSHOT
    jar

    demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-data-neo4j
        
        
        
            org.neo4j
            neo4j-ogm-http-driver
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




2. model

  • UserNode
package com.example.demo.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.neo4j.ogm.annotation.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

@NodeEntity(label = "user")
public class UserNode implements Serializable {
    @Id @GeneratedValue
    private Long id
    private  String name;
    private String userId;
    private String companyId;
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
     public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }

3. Repository

  • UserRepository
package com.example.demo.repository;
import com.example.demo.model.UserNode;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends Neo4jRepository{
}

4. application.properties

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

5. 测试

  • UserRepositoryTest
package com.example.demo;
import com.example.demo.model.ServiceNode;
import com.example.demo.model.UserNode;
import com.example.demo.repository.UserRepository;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.UUID;

@Component
public class UserRepositoryTest extends DemoApplicationTests {
    Logger logger = LoggerFactory.getLogger(UserRepositoryTest.class);
    @Autowired
    UserRepository userRepository;
    @Test
    public void createUserNode() {
        System.out.println(userRepository);
        UserNode userNode = new UserNode();
        userNode.setName("老鼠");
        userNode.setUserId("123");
        UserNode save = userRepository.save(userNode);
        logger.info(save.toString());
        Assert.assertTrue(save != null);
    }

    @Test
    public void delAll() {
        userRepository.deleteAll();
    }
}

你可能感兴趣的:(java相关)