In the last two articles, we mainly explained the basic knowledge of Neo4j and its basic use. In this article, we will take examples to have an in-depth understanding. We will use SpringBoot+Neo4j to achieve the binding relationship in social e-commerce as an example.

Neo4j Article Review:

  • Don’t understand the secondary? That’s ok. Learn together
  • Don’t understand the secondary? It doesn’t matter. Learn to add, delete, change and check first

A, analysis,

Social e-commerce, now do better should belong to peanut diary, and the recent hot fragrance. Maybe you often get shared items from friends or relatives, and when you click on them, congratulations, you’re probably already a follower. Of course, we’re just giving examples here.

Here we will use SpringBoot+Neo4j to implement the goods shared by others, you click, register as a new user, and then be bound to the offline to explain in detail.

Second, code architecture

The code structure is as follows. Controller and service are not written here. Test is used to test for convenience.

Three, code implementation

1. Introduce Neo4j dependencies

2. Add the neo4j configuration

3. Add a Neo4j node class

Add a MemberInvit node, somewhat annotated like the table mapping object class in Mysql, called ORM in Mysql, and OGM in Neo4j. The @NodeEntity and @ID annotations are used here.

  • @nodeEntity declares that this class is a node class of Neo4j
  • @id Primary key of Neo4j.
package com.xsjt.learn.neo4j.model;

import lombok.*;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/ * * *@author Joe
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@NodeEntity(label = "MemberInvit")
public class MemberInvit implements Serializable {

    /** user id */
    @Id
    private Long mId;

    /** User name */
    private String name;

    /** User level */
    private Integer level;

    /** development of the subordinate */
    @Relationship(type = "develop")
    private Set<MemberInvit> fans;

    /** * Development method *@paramMemberInvit User invitation information */
    public void develop(MemberInvit memberInvit) {
        if (fans == null) {
            fans = newHashSet<>(); } fans.add(memberInvit); }}Copy the code

Create repositories class

Of course, the following methods may not be used in our binding example, but they are essential in social e-commerce.

package com.xsjt.learn.neo4j.dao;

import com.xsjt.learn.neo4j.model.MemberInvit;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;

/** * dao layer *@author Joe
 * @dateThe 2020-01-13 * /
@Repository
public interface MemberInvitRepository extends Neo4jRepository<MemberInvit.String> {

    /** * Query node information based on the node ID *@param mId
     * @return* /
    MemberInvit findBymId(Long mId);

    /** * Query all child nodes * of a node@param mId
     * @return* /
    @Query("Match (p:MemberInvit{mId:{mId}})-[*]->(s:MemberInvit) return s ")
    List<MemberInvit> findChildList(Long mId);

    /** * Query the immediate parent of a node *@param mId
     * @return* /
    @Query("Match (p:MemberInvit)-[*]->(s:MemberInvit{mId:{mId}}) return p limit 1")
    MemberInvit findParent(@Param("mId") Long mId);

    /** * Query all parent nodes * of a node@param mId
     * @return* /
    @Query("Match (p:MemberInvit)-[*]->(s:MemberInvit{mId:{mId}}) return p")
    List<MemberInvit> findParentList(@Param("mId") Long mId);


    /** * Query the nearest parent * of a specified level of a node@param mId
     * @param level
     * @return* /
    @Query("Match (p:MemberInvit{level:{level}})-[*]->(s:MemberInvit{mId:{mId}}) return p limit 1")
    MemberInvit findParentInfoByLevel(@Param("mId") Long mId, @Param("level") Integer level);

    /** * Query all parent nodes * of a specified level of a node@param mId
     * @param level
     * @return* /
    @Query("Match (p:MemberInvit{level:{level}})-[*]->(s:MemberInvit{mId:{mId}}) return p")
    List<MemberInvit> findParentByLevel(@Param("mId") Long mId, @Param("level") Integer level);

}
Copy the code

Here inherited Neo4jRepository, click to view the source code will find, which encapsulates a simple add, delete, change, search.

5. Add an official user

Once added, we have two ways to view it, one is in the console of Neo4j and the other is in the code. Here, we first query in the console of Neo4j:

This indicates that the official user has been added successfully.

6. Query official users

Here we use code to query the official user we just created.

7. Bind users

Besides official user, suppose what register first is next door old king, namely next door old king is bound below official user, also can call official user developed next door old king to be down line.

Let’s look at the relationship created on the console:

match (p:MemberInvit) returnMatch (p:MemberInvit)-[d:develop]->(s:MemberInvit)return p,d,s
Copy the code

Next door Lao Wang enjoys a wide popularity and has developed Xiao Zhang, Xiao Li, Xiao Wu, Xiao Zhou and others:

We look at the binding relationship from the console:

In this way, the binding relationship has been formed.

Download the source code

For convenience, the source code has been uploaded to Github at github.com/joeBeckham/…

Five, say at the end

In social e-commerce, there is often a commission. When you buy something, your superior will get the percentage. In this case, the method of “Query the immediate parent of a node” in the DAO layer will be used, namely: findParent.

FindParentByLevel (findParentByLevel); findParentByLevel (findParentByLevel); findParentByLevel (findParentByLevel); findParentByLevel

FindParentInfoByLevel: findParentInfoByLevel: findParentInfoByLevel: findParentInfoByLevel: findParentInfoByLevel: findParentInfoByLevel: findParentInfoByLevel: findParentInfoByLevel: findParentInfoByLevel

We’re not going to give you any examples.

This is how we use SpringBoot + Neo4j to implement binding logic in social e-commerce.

Please pay attention to my official account and receive the latest articles at the first time. Search the official account: Mika or scan the qr code below:

This article is published by OpenWrite!