1. 程式人生 > >springboot整合neo4j

springboot整合neo4j

剛開始按網上部落格搭建 spring boot 和 neo4j一直報sessionFactory找不到,直到下載了spring-data-neo4j的例項demo對比才搭建成功,而且使用者名稱是neo4j,不是建立一個數據庫時的名字,搞了幾個小時終於搞成功了。以後還是多看官方文件,搜來搜去,什麼鬼都搜不到。記錄一下自己踩的坑。

pom檔案

是springboot進行過封裝的spring-boot-starter-data-neo4j提供了一些基礎的方法,有點像spring-data-jpa

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.xp</groupId>
   <artifactId>neo4j-demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>neo4j-demo</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-neo4j</artifactId>
      </dependency>
         <!--<dependency>-->
            <!--<groupId>org.neo4j.driver</groupId>-->
            <!--<artifactId>neo4j-java-driver</artifactId>-->
         <!--</dependency>-->

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

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


</project>
@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<UserNode,Long>{

}

 測試類

@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