Getting Started with Spring Boot and Neo4j: Step‑by‑Step Guide and Sample Code
This article introduces Neo4j as a graph database, walks through its Windows installation, and provides a complete Spring Boot integration tutorial—including Maven dependencies, YAML configuration, entity and repository definitions, REST controller implementation, and end‑to‑end testing of user and follow‑relationship APIs.
Neo4j is a graph‑oriented database that stores entities (nodes) and the relationships between them, making it ideal for social graphs, recommendation engines, knowledge graphs, fraud detection, and topology modeling. Its data model consists of nodes, relationships (directed and typed), and properties, and it is queried with Cypher (CQL) using keywords such as MATCH, CREATE, RETURN, and WHERE.
The Windows installation requires downloading the community edition that matches JDK 1.8 (e.g., version 3.5.28), extracting the archive, launching the server with neo4j console, and accessing the web UI at http://localhost:7474 using the default credentials neo4j/neo4j (the password must be changed on first login).
To integrate Neo4j with Spring Boot, add the Maven dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>(along with spring-boot-starter-web and lombok if needed). Configure the connection in application.yml:
spring:
neo4j:
uri: bolt://localhost:7687
authentication:
username: neo4j
password: 123456Create a User entity annotated with @Node, @Id, @GeneratedValue, and a
@Relationship(type = "FOLLOW", direction = Relationship.Direction.OUTGOING)field to model follow links. The annotations map the class to a Neo4j node, designate the primary key, and define relationship semantics.
Define a UserRepository extending Neo4jRepository<User, Long> and add custom Cypher queries, for example:
@Query("MATCH (u:User) RETURN u")
List<User> findAllUser();
@Query("MATCH (u:User) WHERE id(u) = $id RETURN u")
Optional<User> findUserById(@Param("id") Long id);
@Query("MATCH (u:User)-[:FOLLOW]->(f:User) WHERE id(u) = $id RETURN f")
List<User> findMyFollow(@Param("id") Long id);
@Query("MATCH (u:User),(f:User) WHERE id(u) = $uid AND id(f) = $fid MERGE (u)-[:FOLLOW]->(f)")
void saveFollowRelation(@Param("uid") Long uid, @Param("fid") Long fid);Implement a UserController with REST endpoints to add a user (clearing relationships on creation), create a follow relation, list all users, retrieve a user by ID, and list the users a given user follows. Example snippets:
@PostMapping
public User add(@RequestBody User user) {
user.setFollowUsers(null);
return userRepository.save(user);
}
@PostMapping("/follow")
public String follow(@RequestParam Long uid, @RequestParam Long fid) {
userRepository.saveFollowRelation(uid, fid);
return "关注成功";
}Running the application and invoking the APIs demonstrates the full workflow: POST {"name":"张三"} returns id=57, POST {"name":"李四"} returns id=58, POST /user/follow?uid=58&fid=57 creates the follow edge, GET /user lists both users, and GET /user/follow/58 returns 张三, confirming the relationship was stored correctly.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
