├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── appleyk
│ │ ├── Application.java
│ │ ├── node
│ │ ├── BaseNode.java
│ │ └── User.java
│ │ ├── relation
│ │ └── LikeRelation.java
│ │ └── repository
│ │ ├── LikeRelationRepository.java
│ │ └── UserRepository.java
└── resources
│ ├── application.properties
│ └── logback-boot.xml
└── test
└── java
├── LikeRelationTest.java
└── UserNodeTest.java
/README.md:
--------------------------------------------------------------------------------
1 | # Spring-Boot-Neo4j-Relation
2 | Spring-Boot 集成 Neo4j图形数据库实现关系的构建与查询
3 | 博客地址:https://blog.csdn.net/Appleyk/article/details/80547148
4 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.appleyk
5 | Spring-Boot-Neo4j
6 | 0.0.1-SNAPSHOT
7 | Spring-Boot 集成 Neo4j图形数据库实现关系的构建与查询
8 |
9 | org.springframework.boot
10 | spring-boot-starter-parent
11 | 1.5.12.RELEASE
12 |
13 |
14 | 1.8
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 |
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-devtools
32 |
33 |
34 | true
35 |
36 |
37 |
38 | org.springframework.boot
39 | spring-boot-starter-test
40 | test
41 |
42 |
43 |
44 | junit
45 | junit
46 |
47 |
48 |
49 | org.springframework.boot
50 | spring-boot-starter-data-neo4j
51 |
52 |
53 |
--------------------------------------------------------------------------------
/src/main/java/com/appleyk/Application.java:
--------------------------------------------------------------------------------
1 | package com.appleyk;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.boot.builder.SpringApplicationBuilder;
6 | import org.springframework.boot.web.support.SpringBootServletInitializer;
7 |
8 |
9 | @SpringBootApplication// same as @Configuration @EnableAutoConfiguration @ComponentScan
10 | public class Application extends SpringBootServletInitializer{
11 |
12 | public static void main(String[] args) {
13 | SpringApplication.run(Application.class, args);
14 | }
15 |
16 | @Override
17 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
18 | return application.sources(Application.class);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/appleyk/node/BaseNode.java:
--------------------------------------------------------------------------------
1 | package com.appleyk.node;
2 |
3 | import org.neo4j.ogm.annotation.GraphId;
4 |
5 | import com.fasterxml.jackson.annotation.JsonIdentityInfo;
6 | import com.fasterxml.jackson.annotation.ObjectIdGenerators;
7 |
8 | @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
9 | public abstract class BaseNode {
10 |
11 | @GraphId
12 | private Long id;
13 | protected String name;
14 |
15 | public BaseNode() {
16 | }
17 |
18 | public String getName() {
19 | return name;
20 | }
21 |
22 | public void setName(String name) {
23 | this.name = name;
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/appleyk/node/User.java:
--------------------------------------------------------------------------------
1 | package com.appleyk.node;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.neo4j.ogm.annotation.NodeEntity;
7 | import org.neo4j.ogm.annotation.Relationship;
8 |
9 | import com.appleyk.relation.LikeRelation;
10 |
11 | @NodeEntity
12 | public class User extends BaseNode{
13 |
14 | /**
15 | * 用户ID
16 | */
17 | private Long uid;
18 |
19 | /**
20 | * 用户性别
21 | */
22 | private String sex;
23 |
24 | /**
25 | * 用户年龄
26 | */
27 | private Integer age;
28 |
29 | /**
30 | * 兴趣爱好
31 | */
32 | private List hobbies;
33 |
34 | /**
35 | * 添加关系喜欢,方向为 ->,表明当前节点是startNode
36 | */
37 | @Relationship(type="Like",direction = Relationship.OUTGOING)
38 | private List likes;
39 |
40 |
41 |
42 | public User(){
43 | hobbies = new ArrayList<>();
44 | likes = new ArrayList<>();
45 | }
46 |
47 | public Long getUid() {
48 | return uid;
49 | }
50 |
51 | public void setUid(Long uid) {
52 | this.uid = uid;
53 | }
54 |
55 | public String getSex() {
56 | return sex;
57 | }
58 |
59 | public void setSex(String sex) {
60 | this.sex = sex;
61 | }
62 |
63 | public Integer getAge() {
64 | return age;
65 | }
66 |
67 | public void setAge(Integer age) {
68 | this.age = age;
69 | }
70 |
71 | public List getHobbies() {
72 | return hobbies;
73 | }
74 |
75 | public void setHobbies(List hobbies) {
76 | this.hobbies = hobbies;
77 | }
78 |
79 | public void addHobby(String hobby){
80 | hobbies.add(hobby);
81 | }
82 |
83 | public void addLikes(User user,String reason,Integer since,Integer relationID){
84 | LikeRelation like = new LikeRelation(this, user, reason, since, relationID);
85 | this.likes.add(like);
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/src/main/java/com/appleyk/relation/LikeRelation.java:
--------------------------------------------------------------------------------
1 | package com.appleyk.relation;
2 |
3 | import org.neo4j.ogm.annotation.EndNode;
4 | import org.neo4j.ogm.annotation.GraphId;
5 | import org.neo4j.ogm.annotation.Property;
6 | import org.neo4j.ogm.annotation.RelationshipEntity;
7 | import org.neo4j.ogm.annotation.StartNode;
8 |
9 | import com.appleyk.node.BaseNode;
10 |
11 | @RelationshipEntity(type = "Like")
12 | public class LikeRelation {
13 |
14 | @GraphId
15 | private Long id;
16 |
17 | /**
18 | * 定义关系的起始节点 == StartNode
19 | */
20 |
21 | @StartNode
22 | private BaseNode startNode;
23 |
24 | /**
25 | * 定义关系的终止节点 == EndNode
26 | */
27 |
28 | @EndNode
29 | private BaseNode endNode;
30 |
31 |
32 | /**
33 | * 定义关系的属性
34 | */
35 |
36 | @Property(name = "reason")
37 | private String reason;
38 | @Property(name = "since")
39 | private Integer since;
40 | @Property(name = "relationID")
41 | private Integer relationID;
42 |
43 | public LikeRelation() {
44 | }
45 |
46 | public LikeRelation(BaseNode startNode,BaseNode endNode,String reason,
47 | Integer since,Integer relationID){
48 | this.startNode = startNode;
49 | this.endNode = endNode;
50 | this.reason = reason;
51 | this.since = since;
52 | this.relationID = relationID;
53 | }
54 |
55 | public Long getId() {
56 | return id;
57 | }
58 |
59 | public void setId(Long id) {
60 | this.id = id;
61 | }
62 |
63 | public BaseNode getStartNode() {
64 | return startNode;
65 | }
66 |
67 | public void setStartNode(BaseNode startNode) {
68 | this.startNode = startNode;
69 | }
70 |
71 | public BaseNode getEndNode() {
72 | return endNode;
73 | }
74 |
75 | public void setEndNode(BaseNode endNode) {
76 | this.endNode = endNode;
77 | }
78 |
79 | public String getReason() {
80 | return reason;
81 | }
82 |
83 | public void setReason(String reason) {
84 | this.reason = reason;
85 | }
86 |
87 | public Integer getSince() {
88 | return since;
89 | }
90 |
91 | public void setSince(Integer since) {
92 | this.since = since;
93 | }
94 |
95 | public Integer getRelationID() {
96 | return relationID;
97 | }
98 |
99 | public void setRelationID(Integer relationID) {
100 | this.relationID = relationID;
101 | }
102 |
103 | }
104 |
--------------------------------------------------------------------------------
/src/main/java/com/appleyk/repository/LikeRelationRepository.java:
--------------------------------------------------------------------------------
1 | package com.appleyk.repository;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.data.neo4j.annotation.Query;
6 | import org.springframework.data.neo4j.repository.GraphRepository;
7 |
8 | import com.appleyk.relation.LikeRelation;
9 |
10 | public interface LikeRelationRepository extends GraphRepository{
11 |
12 | /**
13 | * 查询关系
14 | * @param relationName
15 | * @return
16 | */
17 | @Query("match p = (n)-[r:Like]-(b) return p")
18 | List getLikes();
19 |
20 |
21 | /**
22 | * 为两个已经存在的节点添加关系
23 | * @param startNodeID -- 起始节点
24 | * @param endNodeID -- 终止节点
25 | * @param rID -- 关系的ID
26 | * @param year -- 关系的开始年限
27 | * @param reason -- 关系产生的原因
28 | * @return
29 | */
30 | @Query("match(a),(b) where a.uid={0} and b.uid = {1}"
31 | + " create p = (a)-[r:Like{relationID:{2},since:{3},reason:{4}}]->(b) return p ")
32 | List createLikes(Long startNodeID,Long endNodeID,
33 | Integer rID,Integer year,String reason);
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/appleyk/repository/UserRepository.java:
--------------------------------------------------------------------------------
1 | package com.appleyk.repository;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.data.neo4j.annotation.Query;
6 | import org.springframework.data.neo4j.repository.GraphRepository;
7 | import org.springframework.data.repository.query.Param;
8 | import org.springframework.stereotype.Repository;
9 |
10 | import com.appleyk.node.User;
11 |
12 | @Repository
13 | public interface UserRepository extends GraphRepository{
14 |
15 | List getUsersByName(@Param("name") String name);
16 |
17 | /**
18 | * 根据年龄查询用户节点
19 | * @param age
20 | * @return
21 | */
22 | @Query("match(n:User) where n.age >{age} return n")
23 | List getUsers(@Param("age") Integer age);
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.port=8080
2 | server.session.timeout=10
3 | server.tomcat.uri-encoding=utf8
4 |
5 | #在application.properties文件中引入日志配置文件
6 | #===================================== log =============================
7 | logging.config=classpath:logback-boot.xml
8 |
9 | #Neo4j配置
10 | spring.data.neo4j.username=neo4j
11 | spring.data.neo4j.password=n123
12 | #数据库uri地址
13 | spring.data.neo4j.uri=http://localhost:7474
14 |
--------------------------------------------------------------------------------
/src/main/resources/logback-boot.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | %d %p (%file:%line\)- %m%n
8 |
9 | UTF-8
10 |
11 |
12 |
13 |
14 |
15 |
17 |
18 | opt/spring-boot-web/logs/sys.log
19 |
20 |
21 |
22 |
23 |
24 | log/sys.%d.%i.log
25 |
26 | 30
27 |
28 |
29 | 10MB
30 |
31 |
32 |
33 |
34 |
35 | %d %p (%file:%line\)- %m%n
36 |
37 |
38 | UTF-8
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/src/test/java/LikeRelationTest.java:
--------------------------------------------------------------------------------
1 | import java.util.List;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.boot.test.context.SpringBootTest;
7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8 |
9 | import com.appleyk.Application;
10 | import com.appleyk.node.User;
11 | import com.appleyk.relation.LikeRelation;
12 | import com.appleyk.repository.LikeRelationRepository;
13 |
14 | @RunWith(SpringJUnit4ClassRunner.class)
15 | @SpringBootTest(classes = Application.class)
16 | public class LikeRelationTest {
17 |
18 | @Autowired
19 | private LikeRelationRepository likeRepository;
20 |
21 |
22 | /**
23 | * 在已有的两个节点的基础上创建关系
24 | */
25 | @Test
26 | public void createLikeRelation() throws Exception {
27 |
28 | LikeRelation like = new LikeRelation();
29 |
30 | /**
31 | * 节点 == 刘大壮
32 | */
33 | User startNode = new User();
34 | startNode.setUid(1001L);
35 |
36 | /**
37 | * 节点 == 马晓丽
38 | */
39 | User endNode = new User();
40 | endNode.setUid(1002L);
41 |
42 | like.setStartNode(startNode);
43 | like.setEndNode(endNode);
44 |
45 | like.setRelationID(520);
46 | like.setSince(2018);
47 | like.setReason("晓丽是女神");
48 |
49 | List likes = likeRepository.createLikes(startNode.getUid(),
50 | endNode.getUid(), like.getRelationID(),like.getSince(),like.getReason());
51 |
52 | /**
53 | * 遍历创建的关系
54 | */
55 | for (LikeRelation likeRelation : likes) {
56 | User sNode = (User) likeRelation.getStartNode();
57 | User eNode = (User) likeRelation.getEndNode();
58 | System.out.println(sNode.getName() + "--喜欢-->" + eNode.getName());
59 | }
60 | }
61 |
62 |
63 | /**
64 | * 创建节点并创建关系
65 | */
66 | @Test
67 | public void createLikes(){
68 | LikeRelation like = new LikeRelation();
69 |
70 | /**
71 | * 节点 == 韩梅梅
72 | */
73 | User startNode = new User();
74 | startNode.setUid(1003L);
75 | startNode.setName("韩梅梅");
76 | startNode.setAge(18);
77 | startNode.setSex("女");
78 | startNode.addHobby("看书");
79 | startNode.addHobby("逛街");
80 |
81 | /**
82 | * 节点 == 李晓明
83 | */
84 | User endNode = new User();
85 | endNode.setUid(1004L);
86 | endNode.setName("李晓明");
87 | endNode.setAge(19);
88 | endNode.setSex("男");
89 | endNode.addHobby("游戏");
90 | endNode.addHobby("音乐");
91 | endNode.addHobby("篮球");
92 |
93 | like.setStartNode(startNode);
94 | like.setEndNode(endNode);
95 |
96 | like.setRelationID(520);
97 | like.setSince(2018);
98 | like.setReason("晓明好帅啊");
99 |
100 | LikeRelation relation = likeRepository.save(like);
101 | System.out.println(relation.getStartNode().getName()+"-->喜欢--"+
102 | relation.getEndNode().getName()+",理由:"+relation.getReason());
103 |
104 | }
105 |
106 | /**
107 | * 查询关系
108 | */
109 | @Test
110 | public void findLikes(){
111 | List likes = likeRepository.getLikes();
112 | for (LikeRelation likeRelation : likes) {
113 | User sNode = (User) likeRelation.getStartNode();
114 | User eNode = (User) likeRelation.getEndNode();
115 | System.out.println(sNode.getName() + "--喜欢-->" + eNode.getName()
116 | +" == reason: "+likeRelation.getReason());
117 | }
118 | }
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/src/test/java/UserNodeTest.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 | import java.util.List;
3 |
4 | import org.junit.Test;
5 | import org.junit.runner.RunWith;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.boot.test.context.SpringBootTest;
8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9 |
10 | import com.appleyk.Application;
11 | import com.appleyk.node.User;
12 | import com.appleyk.relation.LikeRelation;
13 | import com.appleyk.repository.UserRepository;
14 |
15 |
16 | @RunWith(SpringJUnit4ClassRunner.class)
17 | @SpringBootTest(classes=Application.class)
18 | public class UserNodeTest {
19 |
20 |
21 | @Autowired
22 | UserRepository userRepository;
23 |
24 | /**
25 | * 创建用户节点 【批量创建】
26 | * @throws Exception
27 | */
28 | @Test
29 | public void createUser() throws Exception{
30 |
31 | List userNodes = new ArrayList<>();
32 | User userNode1 = new User();
33 | userNode1.setUid(1001L);
34 | userNode1.setName("刘大壮");
35 | userNode1.setAge(22);
36 | userNode1.setSex("男");
37 | userNode1.addHobby("游戏");
38 | userNode1.addHobby("睡觉");
39 | userNode1.addHobby("撸串");
40 |
41 | User userNode2 = new User();
42 | userNode2.setUid(1002L);
43 | userNode2.setName("马晓丽");
44 | userNode2.setAge(17);
45 | userNode2.setSex("女");
46 | userNode2.addHobby("逛街");
47 | userNode2.addHobby("美食");
48 | userNode2.addHobby("化妆");
49 |
50 | userNodes.add(userNode1);
51 | userNodes.add(userNode2);
52 | Iterable iterable = userRepository.save(userNodes);
53 | for (User user : iterable) {
54 | System.out.println("创建节点:【"+user.getName()+"】成功!");
55 | }
56 |
57 | }
58 |
59 | /**
60 | * 创建节点 == 内置关系
61 | */
62 | @Test
63 | public void createNodeandRelation(){
64 |
65 | User startNode = new User();
66 | startNode.setUid(1011L);
67 | startNode.setName("刘泽");
68 | startNode.setAge(22);
69 | startNode.setSex("男");
70 | startNode.addHobby("游戏");
71 | startNode.addHobby("睡觉");
72 |
73 | User endNode1 = new User();
74 | endNode1.setUid(1012L);
75 | endNode1.setName("张婷");
76 | endNode1.setAge(17);
77 | endNode1.setSex("女");
78 | endNode1.addHobby("逛街");
79 | endNode1.addHobby("美食");
80 | endNode1.addHobby("化妆");
81 |
82 | User endNode2 = new User();
83 | endNode2.setUid(1013L);
84 | endNode2.setName("林志玲");
85 | endNode2.setAge(45);
86 | endNode2.setSex("女");
87 | endNode2.addHobby("逛街");
88 | endNode2.addHobby("美食");
89 | endNode2.addHobby("化妆");
90 |
91 | startNode.addLikes(endNode1, "心地善良,人美", 2015,521 );
92 | startNode.addLikes(endNode2, "女神姐姐", 2011, 520);
93 |
94 | User userNode = userRepository.save(startNode);
95 | System.out.println("节点"+userNode.getName()+"创建成功!");
96 |
97 | }
98 |
99 | /**
100 | * 根据用户的name查询user节点列表
101 | */
102 | @Test
103 | public void findUserByName(){
104 | List users = userRepository.getUsersByName("刘大壮");
105 | System.out.println("共查出来节点有:"+users.size()+"个");
106 | }
107 |
108 | /**
109 | * 根据用户的年龄查询user节点【年龄大于18】
110 | */
111 | @Test
112 | public void findUserByAge(){
113 | List users = userRepository.getUsers(18);
114 | for (User user : users) {
115 | System.out.println("共查出来节点有:"+users.size()+"个, == "+user.getName());
116 | }
117 | }
118 | }
119 |
--------------------------------------------------------------------------------