SpringBoot集成neo4j示例

SpringBoot集成neo4j

配置

  • xml配置:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-neo4j</artifactId>
     <version>2.1.9.RELEASE</version>
</dependency>
  • SpringBoot配置:
@SpringBootApplication
@EnableNeo4jRepositories
public class TestApplication {

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

}

到這裏SpringBoot配置完成,接下來是增刪改查

  • 實體類
  1. 節點類
@Data
@NoArgsConstructor
@NodeEntity
public class Bill {

    @Id @GeneratedValue
    private Long id;

    @Property
    private Integer billNo;
    @Property
    private String name;
    @Property
    private BigDecimal quantity;

}
  1. 關係類
@RelationshipEntity(type = "MERGE")
@Data
@NoArgsConstructor
public class BillRelation {

    @Id
    @GeneratedValue
    private Long id;

    @Property
    private Integer billNo;
    @Property
    private Integer type;
    @Property
    private BigDecimal quantity;
	
    //開始節點
    @StartNode
    private BillNode startNode;
	
    //結束節點
    @EndNode
    private BillNode endNode;
}
  • dao
  1. 節點dao,和jpa一樣,可以進行自定義查詢或者使用jpa語法。
@Repository
public interface BillNodeRepository extends Neo4jRepository<BillNode,Long> {

    @Query("MATCH (p1:Bill ) where p1.billNo=1 return p1")
    BillNode findBillNodeByBillNo();

}
  1. 關係dao
@Repository
public interface BillRelationRepository extends Neo4jRepository<BillRelation,Long> {
	
    //查詢兩個節點的所有路徑
    @Query("OPTIONAL MATCH (p1:Bill {billNo:1}),(p2:Bill {billNo:8}),p=(p1)-[:MERGE*..]->(p2) return p")
    List<BillRelation> findAllPath();

}

測試

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplicationTests {

    @Autowired
    private BillRelationRepository billRelationRepository;

    @Autowired
    private BillNodeRepository billNodeRepository;

    @Test
    public void testAdd() {
        //節點
        BillNode node1=new BillNode();
        node1.setName("bill1");
        node1.setBillNo(9);
        node1.setQuantity(new BigDecimal(15));
        billNodeRepository.save(node1);
        BillNode node2=new BillNode();
        node2.setName("bill2");
        node2.setBillNo(10);
        node2.setQuantity(new BigDecimal(7));
        billNodeRepository.save(node2);

		//關係
        BillRelation relation1=new BillRelation();
        relation1.setBillNo(10);
        relation1.setQuantity(new BigDecimal(15));
        relation1.setType(3);
        relation1.setStartNode(node1);
        relation1.setEndNode(node2);
        billRelationRepository.save(relation1);
    }

    
    @Test
    public void testQuery() {
        //自定義查詢節點
        BillNode billNode = billNodeRepository.findBillNodeByBillNo();
        System.out.println(billNode);
        
        //jpa語法查詢id爲1的節點
        BillNode billNode1 = billNodeRepository.findById(1L).orElse(null);
        System.out.println(billNode1);
        
        //查詢所有路徑
        List<BillRelation> all = billRelationRepository.findAllPath();
        System.out.println(all);
    }
}

neo4j cypher常用語法

創建

創建node

CREATE (bill1:Bill {billNo:1,name:"節點1",quantity:'10'})

創建relationship


CREATE
  (baseBill)-[:MERGE {billNo:1,type:1,quantity:'10'}]->(truck1)

查詢

1.查詢節點

  1. 屬性值billNo=1的節點
OPTIONAL MATCH (p1:Bill) where p1.billNo=1 return p1
  1. 內置id=1的節點
OPTIONAL MATCH (p1:Bill) where id(p1)=1 return p1

注:neo4j的內置屬性需要通過函數調用

  1. 所有節點
MATCH (n) RETURN n LIMIT 25

2.查詢兩個節點的關係

  1. 從p1到p2節點的任意數量關係的可變長度路徑
OPTIONAL MATCH (p1:Bill {billNo:1}),(p2:Bill {billNo:2}),p=(p1)-[*]->(p2) return p
  1. 最短路徑
  • 所有最短路徑
OPTIONAL MATCH (p1:Bill {billNo:1}),(p2:Bill {billNo:8}),p=allShortestPaths((p1)-[*..5]->(p2)) return p
  • 返回一條最短路徑
OPTIONAL MATCH (p1:Bill {billNo:1}),(p2:Bill {billNo:8}),p=shortestPath((p1)-[*..5]->(p2)) return p

[...]這裏爲正則表達式

常用查詢:

  • [*]`表示查詢所有路徑長度的relationship

  • [*1…10]`表示查詢路徑長度在1-10之間的relationship

  • [*…10]`表示查詢路徑長度10以內的relationship

  1. 所有關係
MATCH p=()--() RETURN p LIMIT 25

注:-->,<--,--代表關係的方向

3.限制路徑查詢條件

  1. 不經過屬性值billNo爲8的relationship
OPTIONAL MATCH (p1:Bill {billNo:1}),(p2:Bill {billNo:8}),p=(p1)-[*]->(p2) with *,relationships(p) as r where all(x in r where x.billNo<>8) return p
  1. 不經過屬性值billNo爲5的node
OPTIONAL MATCH (p1:Bill {billNo:1}),(p2:Bill {billNo:8}),p=(p1)-[*]->(p2)  where all(x in nodes(p) where x.billNo<>5) return p
  1. relationship類型爲MERGE
OPTIONAL MATCH (p1:Bill {billNo:1}),(p2:Bill {billNo:8}),p=(p1)-[*]->(p2) with *,relationships(p) as r where all(x in r where type(x)='MERGE') return p

注:這裏因爲路徑是list所以需要遍歷

刪除

  1. 刪除type爲MERGE的relationship
MATCH (n)
OPTIONAL MATCH (n)-[r:MERGE]-()
DELETE r
  1. 刪除所有node和relationship
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r

補充

1.MATCH與OPTIONAL MATCH的區別

  • MATCH語句用於指定的模式檢索數據庫
  • OPTIONAL MATCH語句用於搜索模式中描述的匹配項,對於找不到的項,用null代替

2.常用的relationship函數

Function Description
relationships() 返回路徑中的所有relationship列表
nodes() 返回路徑中的所有node列表
startNode() 返回關係的開始節點
endNode() 返回關係的結束節點
properties() 返回關係或節點的所有屬性,以map結構
type() 返回關係的類型
id() 返回關係或節點的id

更多的函數查看noe4j doc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章