1. 程式人生 > >Spring Boot配置neo4j(簡單版)

Spring Boot配置neo4j(簡單版)

圖資料庫的開發已經變得越來越流行,與springBoot結合也是很正常,在很早前就已經發布了相關Lib,但是當前網上的Spring Boot整合neo4j都極其複雜,也導致了本人在開發過程中耗費很長時間,顧寫下這個文章幫助大眾理順neo4j整合。只需要簡單六步,順序按照編寫先後順序。 首先看整體Demo專案架構: (一)首先導包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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>
com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
(二)編寫配置檔案application.properties
server.port=8080
spring.data.neo4j.uri=http://{neo4jweb端訪問地址}:7474 
spring.data.neo4j.username=neo4j賬號
spring.data.neo4j.password=neo4j密碼
(三)model檔案---節點User和關係UserRelation,這兩個檔案與neo4j裡面的節點和關係的屬性一一對應,此處是我的示例,具體與各自的neo4j內容對於,label必須與neo4j庫里名字一致
@NodeEntity(label = "User")
public class UserNode {
    @GraphId
private Long nodeId;
@Property(name = "userId")
    private String userId;
@Property(name = "name")
    private String name;
@Property(name = "age")
    private int age;
    public Long getNodeId() {
        return nodeId;
}

    public void setNodeId(Long nodeId) {
        this.nodeId = nodeId;
}

    public String getUserId() {
        return userId;
}

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

    public String getName() {
        return name;
}

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

    public int getAge() {
        return age;
}

    public void setAge(int age) {
        this.age = age;
}
}
@RelationshipEntity(type = "UserRelation")
public class UserRelation {
    @GraphId
private Long id;
@StartNode
private UserNode startNode;
@EndNode
private UserNode endNode;
    public Long getId() {
        return id;
}

    public void setId(Long id) {
        this.id = id;
}

    public UserNode getStartNode() {
        return startNode;
}

    public void setStartNode(UserNode startNode) {
        this.startNode = startNode;
}

    public UserNode getEndNode() {
        return endNode;
}

    public void setEndNode(UserNode endNode) {
        this.endNode = endNode;
}
}
(四)repository檔案----UserRepository和UserRelationRepository,@Query裡面編寫Cypher查詢
@Component
public interface UserRepository extends GraphRepository<UserNode> {

    @Query("MATCH (n:User) RETURN n ")
    List<UserNode> getUserNodeList();
@Query("create (n:User{age:{age},name:{name}}) RETURN n ")
    List<UserNode> addUserNodeList(@Param("name") String name, @Param("age")int age);
}
@Component
public interface UserRelationRepository extends GraphRepository<UserRelation> {

    @Query("match p=(n:User)<-[r:UserRelation]->(n1:User) where n.userId={firstUserId} and n1.userId={secondUserId} return p")
    List<UserRelation> findUserRelationByEachId(@Param("firstUserId") String firstUserId, @Param("secondUserId") String secondUserId);
@Query("match (fu:User),(su:User) where fu.userId={firstUserId} and su.userId={secondUserId} create p=(fu)-[r:UserRelation]->(su) return p")
    List<UserRelation> addUserRelation(@Param("firstUserId") String firstUserId, @Param("secondUserId") String secondUserId);
}
(五)service檔案
@Service
public class Neo4jService {

    @Autowired
private UserRepository userRepository;
@Autowired
private UserRelationRepository userRelationRepository;
/**
     * Always use constructor based dependency injection in your beans.
     * Always use assertions for mandatory dependencies.
     */
public int addUser(UserNode userNode){
        userRepository.addUserNodeList(userNode.getName(),userNode.getAge());
        return 1;
}
}
(六)neo4j檔案注入config----Neo4jConfig,注意basePackges的路徑。
@Configuration
@EnableNeo4jRepositories(basePackages = "com.example.demo.repository")
public class Neo4jConfig {
}