springboot整合neo4j

刚开始按网上博客搭建 spring boot 和 neo4j一直报sessionFactory找不到,直到下载了spring-data-neo4j的实例demo对比才搭建成功,而且用户名是neo4j,不是创建一个数据库时的名字,搞了几个小时终于搞成功了。以后还是多看官方文档,搜来搜去,什么鬼都搜不到。记录一下自己踩的坑。

 

工程地址:https://github.com/CNXXPP/neo4j-demo

 

pom文件

是springboot进行过封装的spring-boot-starter-data-neo4j提供了一些基础的方法,有点像spring-data-jpa



   4.0.0

   com.xp
   neo4j-demo
   0.0.1-SNAPSHOT
   jar

   neo4j-demo
   Demo project for Spring Boot

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

   
      UTF-8
      UTF-8
      1.8
   

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

      
         org.springframework.boot
         spring-boot-starter-test
         test
      
   

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


@NodeEntity(label = "user")
public class UserNode implements Serializable {

    @Id @GeneratedValue
    private Long id;

    private  String name;

    private String userId;

    private String companyId;

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

    @Override
    public String toString() {
        return "UserNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", userId='" + userId + '\'' +
                ", companyId='" + companyId + '\'' +
                '}';
    }
}

dao类 

@Repository
public interface UserRepository extends Neo4jRepository{

}

 测试类

@Component
public class UserRepositoryTest extends Neo4jDemoApplicationTests{

    Logger logger = LoggerFactory.getLogger(UserRepositoryTest.class);

    @Autowired
    UserRepository userRepository;

    @Test
    public void createUserNode(){
        System.out.println(userRepository);
        UserNode userNode = new UserNode();
        userNode.setName("xupan");
        userNode.setUserId("123");
        UserNode save = userRepository.save(userNode);
        logger.info(save.toString());
        Assert.assertTrue(save!=null);
    }

    @Test
    public void delAll(){
       userRepository.deleteById(new Long(27));

    }
}

启动类的@EnableNeo4jRepositories一定要加 

@SpringBootApplication
@EnableNeo4jRepositories
public class Neo4jDemoApplication {

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

Application.yml

spring:
  data:
    neo4j:
      uri: bolt://localhost:7687
      username: neo4j
      password: 1123

springboot整合neo4j_第1张图片

你可能感兴趣的:(neo4j)