12.整合neo4j
neo4j
官网下载:
https://neo4j.com/download-center/#community
教程:
http://neo4j.com.cn/public/cypher/default.html
bin下运行neo4j.bat console
在浏览器地址栏里输入http://localhost:7474
默认会跳转到 http://localhost:7474/browser
刚开始时,会弹出登录页面,默认的初始密码是neo4j,登录进去后会让你设置新的密码,设完后进入neo4j管理界面
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
spring.data.neo4j.uri=http://localhost:7474
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123123
@MapperScan("com.fly.Mapper")
@EnableNeo4jRepositories(basePackages = "com.fly.UserDao")
public class SpringDemoApp{
package com.fly.pojo;
import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
@NodeEntity(label = "User")
public class UserNode {
@GraphId
private Long nodeId;
@Property
private String userId;
@Property
private String name;
@Property
private String age;
public Long getNodeId() {
return nodeId;
}
public void setNodeId(Long nodeId) {
this.nodeId = nodeId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
package com.fly.pojo;
import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.StartNode;
@RelationshipEntity(type = "UserRelation")
public class UserRelation {
@GraphId
private Long id;
@StartNode
private UserNode startNode;
@EndNode
private UserNode endNode;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public UserNode getStartNode() {
return startNode;
}
public void setStartNode(UserNode startNode) {
this.startNode = startNode;
}
public UserNode getEndNode() {
return endNode;
}
public void setEndNode(UserNode endNode) {
this.endNode = endNode;
}
}
package com.fly.UserDao;
import com.fly.pojo.UserRelation;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface UserRelationRepository extends GraphRepository<UserRelation> {
@Query("match p=(n:User)<-[r:UserRelation]->(n1:User) where n.userId={firstUserId} and n1.userId={secondUserId} return p")
List<UserRelation> findUserRelationsByEachId(@Param("firstUserId")String firstUserId,@Param("secondUserId")String secondUserId);
@Query("match (fu:User),(su:User) where fu.userId={firstUserId} and su.userId={secondUserId} create p=(fu)-[r:UserRelation]->(su) return p")
List<UserRelation> addUserRelation(@Param("firstUserId")String firstUserId,@Param("secondUserId")String secondUserId);
}
package com.fly.UserDao;
import com.fly.pojo.UserNode;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface UserRepository extends GraphRepository<UserNode> {
@Query("MATCH (n:User) RETURN n")
List<UserNode> getUserNodeList();
@Query("create (n:User{age:{age},name:{name}}) RETURN n")
List<UserNode> addUserNodeList(@Param("age")String age,@Param("name")String name);
@Query("create (n:User{age:{age},name:{name},userId:{userId}}) RETURN n")
List<UserNode> addUserNodeList(@Param("age")String age,@Param("name")String name,@Param("userId")String userId);
}
package com.fly.service;
import com.fly.UserDao.UserRepository;
import com.fly.pojo.UserNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public void addUserNode(UserNode userNode){
userRepository.addUserNodeList(userNode.getAge(),userNode.getName());
}
public List<UserNode> getUserNodeList(){
return userRepository.getUserNodeList();
}
}
import com.app.SpringDemoApp;
import com.fly.UserDao.UserRelationRepository;
import com.fly.pojo.UserNode;
import com.fly.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@SpringBootTest(classes = SpringDemoApp.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Autowired
private UserRelationRepository userRelationRepository;
@Test
public void test1(){
UserNode userNode = new UserNode();
userNode.setNodeId(1L);
userNode.setUserId("001");
userNode.setName("张三儿");
userNode.setAge("25");
UserNode userNode2 = new UserNode();
userNode2.setNodeId(2L);
userNode2.setUserId("002");
userNode2.setName("李四儿");
userNode2.setAge("24");
userService.addUserNode(userNode);
userService.addUserNode(userNode2);
}
@Test
public void test2(){
List<UserNode> userNodeList = userService.getUserNodeList();
for (UserNode userNode : userNodeList) {
System.out.printf(userNode.getUserId());
}
}
@Test
public void test3(){
userRelationRepository.addUserRelation("001","002");
}
}