package com.neo4j.controller;
import com.neo4j.domain.Movie;
import com.neo4j.domain.Person;
import com.neo4j.domain.Roles;
import com.neo4j.domain.SystemEntity;
import com.neo4j.repository.MovieRepository;
import com.neo4j.repository.PersonRepository;
import com.neo4j.repository.SystemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;
@Controller
@RequestMapping("/neo4j")
public class Neo4jController {
@Autowired
private MovieRepository movieRepository;
@Autowired
private PersonRepository personRepository;
@Autowired
private SystemRepository systemRepository;
/**
* 使用repository操作图数据
*
* @return
*/
@GetMapping("/testByRepository")
@ResponseBody
public List<Person> testByRepository() {
// 删除所有节点和关系(删除节点会响应删除关联关系),避免后续创建节点重复影响
movieRepository.deleteAll();
personRepository.deleteAll();
// 创建节点
Movie movie = new Movie("流浪地球", "是由中国电影股份有限公司、北京京西文化旅游股份有限公司、郭帆文化传媒(北京)有限公司、北京登峰国际文化传播有限公司联合出品,由郭帆执导,吴京特别出演、屈楚萧、赵今麦、李光洁、吴孟达等领衔主演的科幻冒险电影");
// 添加关系
movie.getActorsAndRoles().add(new Roles(new Person(1994, "刘启"), Collections.singletonList("初级驾驶员")));
movie.getActorsAndRoles().add(new Roles(new Person(2002, "刘培强"), Collections.singletonList("中国航天员")));
movie.getActorsAndRoles().add(new Roles(new Person(1952, "韩子昂"), Collections.singletonList("高级驾驶员")));
movie.getActorsAndRoles().add(new Roles(new Person(2002, "韩朵朵"), Collections.singletonList("初中生")));
movie.getActorsAndRoles().add(new Roles(new Person(1981, "王磊"), Collections.singletonList("救援队队长")));
movie.getActorsAndRoles().add(new Roles(new Person(1991, "李一一"), Collections.singletonList("技术观察员")));
movie.getActorsAndRoles().add(new Roles(new Person(1974, "何连科"), Collections.singletonList("救援队队员")));
movie.getActorsAndRoles().add(new Roles(new Person(1991, "Tim"), Collections.singletonList("中美混血儿")));
movie.getDirectors().add(new Person(1974, "吴京"));
// 存入图数据库持久化
movieRepository.save(movie);
// 查询
Person person = personRepository.findPersonEntityByName("刘启");
System.out.println(person);
Movie movie1 = movieRepository.findMovieByTitle("流浪地球");
System.out.println(movie1);
Movie movie2 = movieRepository.findMovieById(movie.getId());
System.out.println(movie2);
// 注意:repository的save方法【对应的实体若id一致】则为修改,否则为新建。
person.setBorn(1997);
personRepository.save(person);
person = personRepository.findPersonEntityByName("刘启");
List<Person> all = personRepository.findAll();
return all;
}
/**
* 新增SystemEntity节点
*/
@GetMapping("/addSystemNode")
public void addSystemNode() {
systemRepository.deleteAll();
SystemEntity systemEntity = new SystemEntity();
systemEntity.setName("系统A"); // 26
systemRepository.save(systemEntity);
System.out.println("系统A" + "----------" + systemEntity.getId());
SystemEntity systemEntity1 = new SystemEntity();
systemEntity1.setName("系统B");// 27
systemRepository.save(systemEntity1);
System.out.println("系统B" + "----------" + systemEntity1.getId());
SystemEntity systemEntity2 = new SystemEntity();
systemEntity2.setName("系统C");// 28
systemRepository.save(systemEntity2);
System.out.println("系统C" + "----------" + systemEntity2.getId());
SystemEntity systemEntity3 = new SystemEntity();
systemEntity3.setName("系统D");// 29
systemRepository.save(systemEntity3);
System.out.println("系统D" + "----------" + systemEntity3.getId());
SystemEntity systemEntity4 = new SystemEntity();
systemEntity4.setName("系统E");// 30
systemRepository.save(systemEntity4);
System.out.println("系统E" + "----------" + systemEntity4.getId());
SystemEntity systemEntity5 = new SystemEntity();
systemEntity5.setName("系统F");// 31
systemRepository.save(systemEntity5);
System.out.println("系统F" + "----------" + systemEntity5.getId());
}
@GetMapping("/addInvokeRelation")
public void addInvokeRelation() {
systemRepository.addInvokeRelation(26L, 27L);
systemRepository.addInvokeRelation(26L, 28L);
systemRepository.addInvokeRelation(29L, 26L);
systemRepository.addInvokeRelation(29L, 28L);
systemRepository.addInvokeRelation(29L, 28L);
}
@GetMapping("/addConsumeRelation")
public void addConsumeRelation() {
systemRepository.addConsumeRelation(30L, 31L);
systemRepository.addConsumeRelation(29L, 30L);
}
// 分页查询
@GetMapping("/pageListSystem")
@ResponseBody
public List<SystemEntity> pageListSystem(String name) {
PageRequest pageRequest = PageRequest.of(0, 4, Sort.by(Sort.Direction.DESC, "name"));
Page<SystemEntity> page = systemRepository.findByName(name, pageRequest);
List<SystemEntity> list = page.getContent();
list.forEach(System.out::println);
return list;
}
/**
* 删除指定节点直接的关系 DELETE <node1-name>,<node2-name>,<relationship-name>
*
* @return
*/
@DeleteMapping("/deleteConsumeRelation")
@ResponseBody
public String deleteConsumeRelation2() {
try {
Long from = 28L, to = 29L;
systemRepository.deleteConsumeRelation(from, to);
} catch (Exception e) {
e.printStackTrace();
return "删除关系失败";
}
return "删除关系成功";
}
@GetMapping("/findSystemById")
@ResponseBody
public SystemEntity findSystemById(@RequestParam("id") Long id) {
SystemEntity systemEntity = systemRepository.findSystemById(id);
return systemEntity;
}
@GetMapping("/getAllSystemNode")
@ResponseBody
public List<SystemEntity> getAllSystemNode() {
List<SystemEntity> all = systemRepository.findAll();
return all;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
springboot集成neo4j,这里使用Spring DATA Neo4j模块来实现。 主要使用以下 Spring Framework 注释来开发此应用程序: S.No. Spring DATA Neo4j注解 用法 1 @GraphEntity 定义域类Neo4j Entity 2 @GraphID 定义节点或关系id 3 @GraphProperty 定义节点或关系属性 Spring DATA Neo4j模块的附加功能: 支持Neo4j属性图模块 支持Neo4j Lucence索引 支持Neo4j CQL 支持Neo4jTemplate
资源推荐
资源详情
资源评论
收起资源包目录
springboot集成neo4j,使用继承Neo4jRepository的方式来实现对数据的操作 (114个子文件)
Neo4jController.class 8KB
Movie.class 4KB
Roles.class 3KB
Person.class 3KB
SystemRepository.class 2KB
RepositoryAspect.class 2KB
SystemEntity.class 1KB
DemoApplication.class 747B
MovieRepository.class 568B
DemoApplicationTests.class 531B
PersonRepository.class 509B
.gitignore 395B
.gitignore 184B
index.html 103B
index.html 103B
demo.iml 9KB
Neo4jController.java 7KB
SystemRepository.java 2KB
Movie.java 1KB
SystemEntity.java 838B
RepositoryAspect.java 773B
Person.java 738B
Roles.java 662B
MovieRepository.java 410B
DemoApplication.java 398B
PersonRepository.java 352B
DemoApplicationTests.java 215B
HELP.md 848B
application.properties 202B
application.properties 202B
workspace.xml 9KB
uiDesigner.xml 9KB
pom.xml 2KB
Project_Default.xml 1KB
jarRepositories.xml 879B
Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_11_4.xml 787B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_4_2.xml 764B
Maven__org_springframework_boot_spring_boot_starter_data_neo4j_2_4_2.xml 764B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 751B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_11_4.xml 746B
Maven__org_springframework_boot_spring_boot_starter_logging_2_4_2.xml 743B
Maven__org_springframework_boot_spring_boot_starter_tomcat_2_4_2.xml 736B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_11_4.xml 732B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_4_2.xml 729B
Maven__org_springframework_boot_spring_boot_starter_test_2_4_2.xml 722B
Maven__org_springframework_boot_spring_boot_starter_json_2_4_2.xml 722B
Maven__org_springframework_boot_spring_boot_starter_web_2_4_2.xml 715B
Maven__org_springframework_boot_spring_boot_starter_aop_2_4_2.xml 715B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_41.xml 711B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_11_4.xml 702B
compiler.xml 699B
Maven__org_springframework_data_spring_data_commons_2_4_3.xml 687B
Maven__org_springframework_boot_spring_boot_starter_2_4_2.xml 687B
Maven__org_junit_platform_junit_platform_commons_1_7_0.xml 684B
Maven__jakarta_activation_jakarta_activation_api_1_2_2.xml 684B
Maven__jakarta_annotation_jakarta_annotation_api_1_3_5.xml 684B
Maven__com_fasterxml_jackson_core_jackson_databind_2_11_4.xml 681B
Maven__org_junit_platform_junit_platform_engine_1_7_0.xml 677B
Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_41.xml 676B
Maven__org_springframework_data_spring_data_neo4j_6_0_3.xml 673B
Maven__org_junit_jupiter_junit_jupiter_params_5_7_0.xml 666B
Maven__org_springframework_boot_spring_boot_test_2_4_2.xml 666B
Maven__org_junit_jupiter_junit_jupiter_engine_5_7_0.xml 666B
Maven__jakarta_xml_bind_jakarta_xml_bind_api_2_3_3.xml 662B
Maven__org_apache_logging_log4j_log4j_to_slf4j_2_13_3.xml 659B
Maven__org_mockito_mockito_junit_jupiter_3_6_28.xml 656B
Maven__org_springframework_spring_expression_5_3_3.xml 653B
Maven__com_fasterxml_jackson_core_jackson_core_2_11_4.xml 653B
Maven__org_reactivestreams_reactive_streams_1_0_3.xml 646B
Maven__org_junit_jupiter_junit_jupiter_api_5_7_0.xml 645B
Maven__org_neo4j_driver_neo4j_java_driver_4_1_1.xml 641B
Maven__net_bytebuddy_byte_buddy_agent_1_10_19.xml 636B
Maven__org_springframework_spring_context_5_3_3.xml 632B
Maven__org_springframework_boot_spring_boot_2_4_2.xml 631B
Maven__org_neo4j_neo4j_cypher_dsl_2020_1_4.xml 627B
Maven__org_springframework_spring_webmvc_5_3_3.xml 625B
Maven__org_apache_logging_log4j_log4j_api_2_13_3.xml 624B
Maven__org_apiguardian_apiguardian_api_1_1_0.xml 623B
Maven__io_github_classgraph_classgraph_4_8_151.xml 622B
Maven__ch_qos_logback_logback_classic_1_2_3.xml 619B
Maven__org_springframework_spring_beans_5_3_3.xml 618B
Maven__org_junit_jupiter_junit_jupiter_5_7_0.xml 617B
Maven__org_springframework_spring_core_5_3_3.xml 611B
Maven__org_springframework_spring_test_5_3_3.xml 611B
Maven__org_springframework_spring_aop_5_3_3.xml 604B
Maven__org_springframework_spring_jcl_5_3_3.xml 604B
Maven__org_springframework_spring_web_5_3_3.xml 604B
Maven__ch_qos_logback_logback_core_1_2_3.xml 598B
Maven__org_springframework_spring_tx_5_3_3.xml 597B
Maven__com_jayway_jsonpath_json_path_2_4_0.xml 597B
Maven__net_bytebuddy_byte_buddy_1_10_19.xml 594B
Maven__net_minidev_accessors_smart_1_2.xml 593B
Maven__org_assertj_assertj_core_3_18_1.xml 593B
Maven__org_aspectj_aspectjweaver_1_9_6.xml 593B
Maven__org_mockito_mockito_core_3_6_28.xml 593B
Maven__org_neo4j_neo4j_ogm_core_4_0_0.xml 592B
Maven__org_skyscreamer_jsonassert_1_5_0.xml 588B
Maven__org_xmlunit_xmlunit_core_2_7_0.xml 586B
Maven__org_slf4j_jul_to_slf4j_1_7_30.xml 585B
Maven__org_neo4j_neo4j_ogm_api_4_0_0.xml 585B
共 114 条
- 1
- 2
资源评论
emo哥老白
- 粉丝: 193
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功