[Dry goods] Understand java+neo4j in one article

1. Neo4j installation

1. Install jdk, this will not be discussed, the version is 1.8

2. Install neo4j

Official website: https://neo4j.com/download-center/

It is divided into community edition and enterprise edition (paid) as follows:

 

Note: The 4.X version needs to correspond to java11 or above, so we choose 3.5 

After the download is complete, unzip it, enter the conf directory, and modify the configuration file

"dbms.default_listen_address=0.0.0.0" uncomment

Then enter the bin directory and run ./neo4j start

Visit http://<ip>:7474/browser/ after startup

 2. SQL syntax of neo4j

Some simple examples:

Query all related attributes of Lao Li: MATCH (n)-[r*1..]->(m) where n.name='Lao Li' return n,r,m

Query all Zhang San's colleague relationship: match (n)-[r:`colleague`]-(m) where n.name='Zhang San' return m,n,r;

Query related attributes of all colleagues of Lao Li: match(n)-[r:`colleague`]-(q)-[b]-(m) where n.name='Lao Li' return n,m,r, b,q
 

3. Java combat neo4j

Introduce neo4j dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-neo4j</artifactId>
    <version>2.5.8</version>
</dependency>
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-ogm-http-driver</artifactId>
    <version>3.2.1</version>
</dependency>

Write entity class

package com.zjlab.leqing.entity;

import lombok.Data;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author bing.bai
 * @create 2022/7/4
 */
@Node
@Data
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private String sex;

    private Person() {
        // Empty constructor required as of Neo4j API 2.0.5
    }



    public Person(String name) {
        this.name = name;
    }

    /**
     * Neo4j doesn't REALLY have bi-directional relationships. It just means when querying
     * to ignore the direction of the relationship.
     * https://dzone.com/articles/modelling-data-neo4j
     */
    @Relationship(type = "房产")
    public Set<House> houses;

    @Relationship(type = "车")
    public Set<Car> cars;

    @Relationship(type = "同事")
    public Set<Person> associates;

    public void haveHouse(House house) {
        if (houses == null) {
            houses = new HashSet<>();
        }
        houses.add(house);
    }

    public void haveCar(Car car) {
        if (cars == null) {
            cars = new HashSet<>();
        }
        cars.add(car);
    }

    public void workWith(Person person) {
        if (associates == null) {
            associates = new HashSet<>();
        }
        associates.add(person);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(id, person.id) && Objects.equals(name, person.name) && Objects.equals(sex, person.sex) && Objects.equals(houses, person.houses) && Objects.equals(cars, person.cars) && Objects.equals(associates, person.associates);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

Write mapper:

package com.zjlab.leqing.mapper;

import com.zjlab.leqing.entity.Person;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;

import java.util.List;

/**
 * @author bing.bai
 * @create 2022/7/4
 */
public interface PersonRepository extends Neo4jRepository<Person, Long> {

   List<Person> findByNameIsLike(String name);

   @Query("match (n:House)<-[r:`房产`]-(m:Person) where n.address contains {address} return m,n,r;")
   Person findByHouseAddress(String address);

   @Query("MATCH (n)-[r*1..]->(m) where n.name contains {name} return n,r,m")
   List<Person> findAllByName(String name);
}

Add annotations to the startup class:

Write the controller:

Final Results:

 

Guess you like

Origin blog.csdn.net/babing18258840900/article/details/125676786