最近在看美团技术团队对图谱的应用文章,觉得现在图数据库其实应用场景还是挺多的,推荐:https://tech.meituan.com/2021/07/15/construction-and-application-of-lifestyle-general-needs-net.html

添加依赖

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

application.yml

spring:
  neo4j:
    # 连接地址
    uri: bolt://localhost:7687
    authentication:
      # neo4j 账号
      username: neo4j
      # neo4j 密码
      password: root

创建实体

@Node
@Data
public class Customer implements Serializable {

    @Id
    private Long id;

    private String name;
}

Neo4jRepository

/*
 * CQL 参考 : https://www.w3cschool.cn/neo4j/
 */
public interface CustomerRepository extends Neo4jRepository<Customer, Long> {

    // MATCH (a:Customer{name:'张三'}) MATCH (b:Customer{name:'李四'}) CREATE (a) - [r:兄弟] -> (b) RETURN r
    @Query("MATCH (a:Customer{name:'张三'}) MATCH (b:Customer{name:'李四'}) CREATE (a) - [r:兄弟] -> (b)")
    void relevance();

    // MATCH (a:Customer{name:'张三'})-[r]-(b:Customer{name:'李四'}) DELETE a,b,r

    // MATCH (n:Customer{name:'张三'})  return n
    // MATCH (n:Customer) WHERE n.name = '张三' return n

    // MATCH (n:Customer) WHERE n.name <> '张' and  n.id <> 1 return n

    // MATCH (c:Customer{name:'张三'}) - [r] -(cc:Customer{name:'李四'}) DELETE r

    // MATCH (c:Customer{name:'张三'}) REMOVE c.id

    // MATCh (c:Customer{name:'张三'}) set c.name = '张三1'

}

Neo4jServiceImplp

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerRepository customerRepository;

    /**
     * 查询 neo4j 所有节点
     *
     * @return
     */
    @Override
    public List<Customer> select() {
        // 查询全部
        List<Customer> all = customerRepository.findAll();

        // 按条件查询
        // List<Customer> all = customerRepository.findAll(Example.of(new Customer()));
        return all;
    }

    /**
     * 创建客户
     *
     * @return
     */
    @Override
    public String create() {
        List<Customer> customerList = new ArrayList<>();
        Customer customer = new Customer();
        customer.setId(new Long(1));
        customer.setName("张三");
        Customer customer2 = new Customer();
        customer2.setId(new Long(2));
        customer2.setName("李四");
        customerList.add(customer);
        customerList.add(customer2);
        customerList.forEach(customer1 -> {
            customerRepository.save(customer1);
        });
        return "创建成功";
    }

    /**
     * 创建关联关系
     *
     * @return
     */
    @Override
    public String relevance() {
        customerRepository.relevance();
        return "创建关联关系成功";
    }

    /**
     * 删除
     *
     * @return
     */
    @Override
    public String delete() {
        customerRepository.deleteAll();
        return "删除成功";
    }

}

Controller

@RestController
@Slf4j
public class CustomerController {

    @Autowired
    CustomerService customerService;

    /**
     * 查询 neo4j 所有节点
     *
     * @return
     */
    @GetMapping("select")
    public List<Customer> select(){
        return customerService.select();
    }

    /**
     * 创建节点
     *
     * @return
     */
    @GetMapping("create")
    public Object create(){
        return customerService.create();
    }

    /**
     * 关联节点间的关系
     *
     * @return
     */
    @GetMapping("relevance")
    public String relevance(){
        return customerService.relevance();
    }

    /**
     * 删除节点
     *
     * @return
     */
    @GetMapping("delete")
    public String delete(){
        return customerService.delete();
    }

}

学习没有捷径可以走,希望能一直坚持