springboot 整合 Neo4j
创建 SpringBoot 应用 springboot 版本为 2.3.0.RELEASE
添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
添加配置文件
spring.data.neo4j.uri=bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456
创建节点类 UserNode
//表示节点类型
@NodeEntity(label="User")
@Data
public class UserNode {
@Id
private Long nodeId;
@Property
private String userId;
@Property
private String name;
@Property
private int age;
}
创建关系
//表示关系类型
@RelationshipEntity(type="UserRelation")
@Data
public class UserRelation {
@Id
private Long id;
@StartNode
private UserNode startNode;
@EndNode
private UserNode endNode;
}
创建UserRepository
@Component
public interface UserRepository extends Neo4jRepository<UserNode,Long> {
/**
* 查询所有节点
* @return
*/
@Query("MATCH (n:User) RETURN n ")
List<UserNode> getUserNodeList();
/**
* 创建节点
* @param userId
* @param name
* @param age
* @return
*/
@Query("create (n:User{userId:$userId,age:$age,name:$name}) RETURN n ")
List<UserNode> addUserNodeList(@Param("userId") String userId,@Param("name") String name, @Param("age")int age);
}
创建 UserRelationRepository
@Component
public interface UserRelationRepository extends Neo4jRepository<UserRelation,Long> {
/**
* 通过id 查询关系
* @param firstUserId
* @param secondUserId
* @return
*/
@Query("match p=(n:User)<-[r:FRIEND]->(n1:User) where n.userId=$firstUserId and n1.userId=$secondUserId return p")
List<UserRelation> findUserRelationByEachId(@Param("firstUserId") String firstUserId, @Param("secondUserId") String secondUserId);
/**
* 添加关系
* @param firstUserId
* @param secondUserId
* @return
*/
@Query("match (fu:User),(su:User) where fu.userId=$firstUserId and su.userId=$secondUserId create p=(fu)-[r:FRIEND]->(su) return p")
List<UserRelation> addUserRelation(@Param("firstUserId") String firstUserId, @Param("secondUserId") String secondUserId);
}
创建 UserService
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private UserRelationRepository userRelationRepository;
public void addUserNode(UserNode userNode) {
userRepository.addUserNodeList(userNode.getUserId(),userNode.getName(), userNode.getAge());
}
public void addRelationship(String firstId,String secondId){
userRelationRepository.addUserRelation(firstId,secondId);
}
public void findUserRelationByEachId(String firstId,String secondId){
userRelationRepository.findUserRelationByEachId(firstId, secondId);
}
}
创建 controller
@RestController
public class Neo4jController {
@Autowired
private UserService userService;
@RequestMapping("/saveUser")
public String saveUserNode() {
UserNode node = new UserNode();
node.setNodeId(1l);
node.setUserId("2");
node.setName("赵六");
node.setAge(26);
userService.addUserNode(node);
return "success";
}
@RequestMapping("/addship")
public String saveShip(){
userService.addRelationship("1","2");
return "success";
}
@RequestMapping("/findShip")
public String findShip(){
userService.findUserRelationByEachId("1","2");
return "success";
}
}
给启动类添加注解
@SpringBootApplication
@EnableNeo4jRepositories(basePackages="com.yao.neo4j.repository")
@EntityScan(basePackages="com.yao.neo4j.entity")
public class Neo4jApplication {
public static void main(String[] args) {
SpringApplication.run(Neo4jApplication.class, args);
}
}
启动测试,插入两个节点
添加关系