Springboot + neo4j in social e-commerce, tell how you are bound to offline

Time:2020-7-11

In the last two articles, we mainly explained the basic knowledge of neo4j and the basic use of neo4j. In this article, we take the binding relationship in social e-commerce as an example and use springboot + neo4j to realize it.

Review of neo4j articles:

  • Don’t understand neo4j? It’s OK. Learn together
  • Don’t understand neo4j? It doesn’t matter. First learn to add, delete, modify and check

1、 Analysis

Among the social e-commerce companies, what we have done better now should be peanut diaries and Fen Xiang, which is popular recently. Maybe you often receive shared products from friends or relatives, and when you click on the shared products, Congratulations, you may have been bound as a subordinate (fan). Of course, we just give an example here.

Next, we will use springboot + neo4j to realize the products shared by others. You can click it, register as a new user, and then be bound as offline to explain in detail.

2、 Code architecture

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

Springboot + neo4j in social e-commerce, tell how you are bound to offline

3、 Code implementation

1. Introduce neo4j dependency

Springboot + neo4j in social e-commerce, tell how you are bound to offline

2. Add neo4j configuration

Springboot + neo4j in social e-commerce, tell how you are bound to offline

3. Add neo4j node class

Here, a memberinvit node is added to annotate the object class similar to table mapping in MySQL. It is called orm in MySQL and OGM in neo4j. The @ nodeentity annotation and @ ID annotation are used here.

  • @Nodeentity declares that this class is the node class of neo4j
  • @Primary key for ID 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;

/**Subordinates to development*/
@Relationship(type = “develop”)
private Set<MemberInvit> fans;

/**
*Development methods
*@ param memberinvit user invitation information
*/
public void develop(MemberInvit memberInvit) {
if (fans == null) {
fans = new HashSet<>();
}
fans.add(memberInvit);
}
}

4. Create the repositories class

Of course, the following methods may not be used in our binding examples, but they are all 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
* @date 2020-01-13
*/
@Repository
public interface MemberInvitRepository extends Neo4jRepository<MemberInvit, String> {

/**
*Query node information according to 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 node 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);


/**
*Queries the nearest parent node of a node’s specified level
* @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);

/**
*Queries all the 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);

}

Here it inherits neo4jrepositiory. Click to view the source code and you will find that it is packaged with simple addition, deletion, modification and query.

Springboot + neo4j in social e-commerce, tell how you are bound to offline

5. Add an official user

Springboot + neo4j in social e-commerce, tell how you are bound to offline

After adding, we have two ways to view it. One is to view it in the neo4j console, and the other is to view it in the code. Here, we first query the neo4j console:

Springboot + neo4j in social e-commerce, tell how you are bound to offline

It shows that official users have been added successfully.

6. Query official users

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

Springboot + neo4j in social e-commerce, tell how you are bound to offline

7. Bind users

In addition to official use, suppose that the first person registered is Lao Wang next door, that is, Lao Wang next door is bound to the official user. You can also ask the official user to develop the next door Laowang as offline.

Springboot + neo4j in social e-commerce, tell how you are bound to offline

We can view the relationship created under the console:

match (p:MemberInvit) return p
perhaps
match (p:MemberInvit)-[d:develop]->(s:MemberInvit) return p,d,s

Springboot + neo4j in social e-commerce, tell how you are bound to offline

Next door, Lao Wang was very popular and developed Xiao Zhang, Xiao Li, Xiao Wu, Xiao Zhou, etc

Springboot + neo4j in social e-commerce, tell how you are bound to offline

Let’s view the binding relationship from the console:

Springboot + neo4j in social e-commerce, tell how you are bound to offline

In this way, the binding relationship is formed.

4、 Source code download

For convenience, the source code has been uploaded to GitHub, address: https://github.com/joeBeckham/springboot-learn

5、 Say at the end

There is often a commission rebate in social e-commerce. If you buy something, what percentage will your superior get? In this way, you will use the method of “query the direct parent node of a node” in Dao layer, that is: findparent.

If you buy something, you may not directly return it to your superior. You may find a superior of a certain level. At this time, you will use the “query all parent nodes of a specified level of a node” in the Dao layer, that is: findparentbylevel.

If you buy something, you may return it to the nearest parent node of the specified level. At this time, you will use the “query the nearest parent node of a node’s specified level” in the Dao layer, that is: findparentinfobylevel.

We will not give examples one by one.

The above is how to use springboot + neo4j to implement the binding logic in social e-commerce.

Welcome to my official account. I will receive the latest article at the first time: search the official account: Code cafe or scan the bottom 2 D code.

Springboot + neo4j in social e-commerce, tell how you are bound to offline

Recommended Today

Webassembly and trust

Rust’s support for webassembly is the most perfect. After all, a large part of Mozilla’s original development of trust was to write servo (browser rendering engine). In addition, the two wasm runtimes, wasmer and wasmtime, which we introduced, are also written with rust. For the use of rust to write wasm module and the use […]