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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.xp</groupId>
  6. <artifactId>neo4j-demo</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>neo4j-demo</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-data-neo4j</artifactId>
  26. </dependency>
  27. <!--<dependency>-->
  28. <!--<groupId>org.neo4j.driver</groupId>-->
  29. <!--<artifactId>neo4j-java-driver</artifactId>-->
  30. <!--</dependency>-->
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-test</artifactId>
  34. <scope>test</scope>
  35. </dependency>
  36. </dependencies>
  37. <build>
  38. <plugins>
  39. <plugin>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-maven-plugin</artifactId>
  42. </plugin>
  43. </plugins>
  44. </build>
  45. </project>
  1. @NodeEntity(label = "user")
  2. public class UserNode implements Serializable {
  3. @Id @GeneratedValue
  4. private Long id;
  5. private String name;
  6. private String userId;
  7. private String companyId;
  8. public String getName() {
  9. return name;
  10. }
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. public String getUserId() {
  15. return userId;
  16. }
  17. public void setUserId(String userId) {
  18. this.userId = userId;
  19. }
  20. public String getCompanyId() {
  21. return companyId;
  22. }
  23. public void setCompanyId(String companyId) {
  24. this.companyId = companyId;
  25. }
  26. @Override
  27. public String toString() {
  28. return "UserNode{" +
  29. "id=" + id +
  30. ", name='" + name + '\'' +
  31. ", userId='" + userId + '\'' +
  32. ", companyId='" + companyId + '\'' +
  33. '}';
  34. }
  35. }

dao类 

  1. @Repository
  2. public interface UserRepository extends Neo4jRepository<UserNode,Long>{
  3. }

 测试类

  1. @Component
  2. public class UserRepositoryTest extends Neo4jDemoApplicationTests{
  3. Logger logger = LoggerFactory.getLogger(UserRepositoryTest.class);
  4. @Autowired
  5. UserRepository userRepository;
  6. @Test
  7. public void createUserNode(){
  8. System.out.println(userRepository);
  9. UserNode userNode = new UserNode();
  10. userNode.setName("xupan");
  11. userNode.setUserId("123");
  12. UserNode save = userRepository.save(userNode);
  13. logger.info(save.toString());
  14. Assert.assertTrue(save!=null);
  15. }
  16. @Test
  17. public void delAll(){
  18. userRepository.deleteById(new Long(27));
  19. }
  20. }

启动类的@EnableNeo4jRepositories一定要加 

  1. @SpringBootApplication
  2. @EnableNeo4jRepositories
  3. public class Neo4jDemoApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Neo4jDemoApplication.class, args);
  6. }
  7. }

Application.yml

  1. spring:
  2. data:
  3. neo4j:
  4. uri: bolt://localhost:7687
  5. username: neo4j
  6. password: 1123

原文地址:https://blog.csdn.net/d597180714/article/details/81079848
posted @ 2019-07-23 15:45  星朝  阅读(4598)  评论(0编辑  收藏  举报