1. The introduction of secondary effects

Neo4j is a high-performance,NOSQL graphical database that stores structured data on the network rather than in tables. It is an embedded, disk-based Java persistence engine with full transactional features, but it stores structured data on a network (mathematically called a graph) rather than in tables. Neo4j can also be thought of as a high-performance graph engine with all the features of a mature database. Programmers work in an object-oriented, flexible network structure rather than rigid, static tables — but they can enjoy all the benefits of a fully transactional, enterprise-class database.

The official website of Neo4j is www.neo4j.org

2. Install the secondary

Docker +Kitematic installation on the MAC, the steps are as follows:

1. Start Docker 2. Search for Neo4j images in Kitematic and install them.

3. After the installation, access the corresponding web address as follows:

3. SpringBoot integration

Next, see how Neo4j is treated in SpringBoot.

3.1 Adding Neo4j dependencies

Create the project and introduce the dependencies into the POM file as follows:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
Copy the code

3.2 Configuration File

Configure Neo4j in the configuration file as follows:

# spring. The secondary configuration data. The secondary. Uri = bolt: / / localhost: 7687 spring. The data. The secondary. The username = secondary spring. The data. The secondary. Password = secondary effectsCopy the code

3.3 Creating an Entity

Here, using the department as an example, create a diagram like this:

* CEO * - Design Department * - Design Group 1 * - Design Group 2 * - Technology Department * - Front-end Technology Department * - Back-end Technology Department * - Test Technology DepartmentCopy the code

So simply create a department entity and a relationship entity.

Where the departmental entities are as follows:

@NodeEntity(label = "dept")
@Data
@Builder
public class Dept {

    @Id
    @GeneratedValue
    private Long id;

    @Property(name = "deptName")
    private String deptName;

}
Copy the code

The relational entities are as follows:

@RelationshipEntity(type = "relationShip")
@Data
@Builder
public class RelationShip {

    @Id
    @GeneratedValue
    private Long id;

    @StartNode
    private Dept parent;

    @EndNode
    private Dept child;
}
Copy the code

Here’s what a few notes mean:

  • @nodeentity: indicates a NodeEntity
  • @relationshipentity: indicates a RelationshipEntity
  • @id: entity primary key
  • @property: indicates the entity attribute
  • @generatedValue: The value of the entity attribute is incremented
  • @startnode: StartNode
  • @endnode: EndNode (child node)

3.4 the repository

Since the spring-data operation neo4j is used, the implementation logic is similar and the interface is created to inherit Neo4jRepository.

DeptRepository is as follows:

import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DeptRepository extends Neo4jRepository<Dept,Long> {

}
Copy the code

RelationShipRepository is as follows:

import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RelationShipRepository extends Neo4jRepository<RelationShip, Long> {

}

Copy the code

3.5 Basic Usage

Here we create some basic methods that work in a similar way to Spring-data-JPA. Since we need to build a graph as described in article 3.1, we create a create method to initialize the data. The complete code is as follows:

@RestController public class TestController { @Resource private DeptRepository deptRepository; @Resource private RelationShipRepository relationShipRepository; @getMapping ("create") public void create(){@getMapping ("create") public void create(){@getMapping ("create") public void create(){ Dept CEO = Dept.builder().deptName("CEO").build(); Dept1 = dept.builder ().deptname (" deptName ").build(); Dept11 = dept.builder ().deptname ().build(); Dept12 = dept.builder ().deptname ().build(); Dept2 = dept.builder ().deptname (" deptName ").build(); Dept21 = dept.builder ().deptname (" deptName ").build(); Dept22 = dept.builder ().deptname (" deptName ").build(); Dept23 = dept.builder ().deptname (" deptName ").build(); List<Dept> depts = new ArrayList<>(Arrays.asList(CEO,dept1,dept11,dept12,dept2,dept21,dept22,dept23)); deptRepository.saveAll(depts); RelationShip relationShip1 = RelationShip.builder().parent(CEO).child(dept1).build(); RelationShip relationShip2 = RelationShip.builder().parent(CEO).child(dept2).build(); RelationShip relationShip3 = RelationShip.builder().parent(dept1).child(dept11).build(); RelationShip relationShip4 = RelationShip.builder().parent(dept1).child(dept12).build(); RelationShip relationShip5 = RelationShip.builder().parent(dept2).child(dept21).build(); RelationShip relationShip6 = RelationShip.builder().parent(dept2).child(dept22).build(); RelationShip relationShip7 = RelationShip.builder().parent(dept2).child(dept23).build(); List<RelationShip> relationShips = new ArrayList<>(Arrays.asList(relationShip1,relationShip2,relationShip3,relationShip4,relationShip5 ,relationShip6,relationShip7)); relationShipRepository.saveAll(relationShips); } @GetMapping("get") public RelationShip get(Long id){ Optional<RelationShip> byId = relationShipRepository.findById(id); return byId.orElse(null); } @GetMapping("deleteRelationShip") public void deleteRelationShip(Long id){ relationShipRepository.deleteById(id); } @GetMapping("deleteDept") public void deleteDept(Long id){ deptRepository.deleteById(id); } @GetMapping("deleteAll") public void deleteAll(){ deptRepository.deleteAll(); relationShipRepository.deleteAll(); }}Copy the code

Execute the create method to initialize the data, and the result is as follows:

The rest of the test methods are not demonstrated here and can be tested on your own.

4.Neo4j basic commands

4.1 Introduction to Operation Commands

Next, the basic operation commands of Neo4j are introduced.

  • CREATE command: command to CREATE a node
  • MATCH command: query command
  • RETURN command: command to RETURN data
  • DELETE command: deletes a node and associated node information
  • REMOVE command: Used to REMOVE labels and attributes

4.2 Simple Exercises

Create command, which can be used to create nodes and relational nodes, for example, we want to create a department, secretary department, as follows, execute the following command:

CREATE (d:dept {deptName:" dept "})Copy the code

After operation, the picture is as follows:

Now it can be seen that the secretary department has no relationship with other nodes. Then create a relationship between the secretary department and the CEO and run the following command:

MATCH (n:dept {deptName:"CEO"}),(m:dept {deptName:"秘书部"}) CREATE (n)-[r:relationShip]->(m) return r;
Copy the code

View the result as shown below:

You can see that the secretary department has been hung under the CEO node.

As can be seen from the above, the general structure of the CQL statement is as follows:

  • MATCH RETURN: returns the matching result.
  • MATCH CREATE RETURN: the relationship is created after the query.
  • MATCH DELETE: deletes a MATCH.

.

5. The source code

Source code address: gitee.com/dalaoyang/s…

6. Reference

Reference Address:

Baike.baidu.com/item/Neo4j/… www.neo4j.org