当前位置: 首页 > news >正文

springBoot 集成Neo4j 实战演示

springBoot 集成Neo4j 实战演示,今天分享一下:

JDK11环境下

1、pom文件

<!-- 1. 继承Spring Boot Starter Parent --><!-- 这是Spring Boot Maven项目的标准方式,它提供了默认的依赖管理和插件配置 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- 从仓库查找父pom,不本地查找 --></parent><!-- 这些依赖的版本由spring-boot-starter-parent统一管理,通常无需显式指定版本 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-neo4j</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

2、yml配置

datasource:host: localhostport: 7687username: neo4jpassword: 123456management:endpoint:health:show-details: alwayslogging:level:org.neo4j.ogm.drivers.bolt.request: DEBUGorg.springframework.data.neo4j: DEBUG

3、代码核心配置


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;@EnableNeo4jRepositories("ru.vladigeras.springneo4j.repository")
@EntityScan(basePackages = "ru.vladigeras.springneo4j.model")
@Configuration
public class Neo4jConfiguration {@Value("${datasource.host}")private String host;@Value("${datasource.port}")private String port;@Value("${datasource.username}")private String username;@Value("${datasource.password}")private String password;@Beanpublic org.neo4j.ogm.config.Configuration configuration() {return new org.neo4j.ogm.config.Configuration.Builder().uri("bolt://" + host + ":" + port).credentials(username, password).build();}
}

4、查询接口配置和实现

@Repository
public interface LineRepository extends Neo4jRepository<LineNode, Long> {
}#业务接口
List<LineNode> getLines();#实现
@Transactional(readOnly = true)@Overridepublic List<LineNode> getLines() {List<LineNode> result = new ArrayList<>();Iterator<LineNode> iterator = lineRepository.findAll().iterator();iterator.forEachRemaining(result::add);return result;}#控制侧@ApiOperation("GetLines")@GetMapping("/lines")@ResponseStatus(HttpStatus.OK)public List<Line> getLines() {return stationService.getLines().stream().map(StationMapper::of).collect(Collectors.toList());}#实体import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;@ApiModel(description = "Line info")
public class Line {@ApiModelProperty("identifier")private Long id;@ApiModelProperty("line name")private String name;public Long getId() {return id;}public Line setId(Long id) {this.id = id;return this;}public String getName() {return name;}public Line setName(String name) {this.name = name;return this;}
}

5新增接口配置实现

#控制层	@ApiOperation("CreateLine")@PostMapping("/lines")@ResponseStatus(HttpStatus.CREATED)public void add(@ApiParam("Line info")@RequestBody NewLine line) {stationService.save(line);}#voimport io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;@ApiModel(description = "Line info")
public class NewLine {@ApiModelProperty("line name")private String name;public String getName() {return name;}public NewLine setName(String name) {this.name = name;return this;}
}#接口
void save(NewLine line);#接口实现@Transactional@Overridepublic void save(NewLine line) {LineNode lineNode = new LineNode();lineNode.setName(line.getName());lineRepository.save(lineNode);}

6、启动类


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringNeo4jApplication {public static void main(String[] args) {SpringApplication.run(SpringNeo4jApplication.class, args);}}

启动成功就可以演示了。

http://www.dtcms.com/a/407001.html

相关文章:

  • 深圳专业网站建设平台网页深圳龙华区发达吗
  • 正点原子【第四期】Linux之驱动开发学习笔记-6.1 pinctrl和gpio子系统
  • 网站要跟换域名怎么做wordpress支持大数据处理
  • 【LeetCode 每日一题】165. 比较版本号
  • 实验一 中断功能实验
  • 网站建设合同细节品牌注册号是哪个
  • 用Requests+BeautifulSoup实现天气预报数据采集:从入门到实战
  • 叫别人做网站要给什么东西长春哪家网站做的好
  • 怎样给网站做流量二级域名做网站域名
  • C语言⽂件操作讲解(1)
  • 记录commandlinerunner错误解决
  • Next.js + TanStack Query 架构中三种常见的数据请求模式
  • ISO 雨晨 26200.6588 Windows 11 企业版 LTSC 25H2 自用 edge 140.0.3485.81
  • 企业手机app开发公司网站优化策划方案
  • 建网站的免费空间百度检索入口
  • 厦门品牌网站建设丽水市住房和城乡建设局网站
  • EsModule和Common.js区别
  • [Vue warn]: Error in mounted hook: “ReferenceError: Jessibuca is not defined“
  • Linux 修炼:进程控制(一)
  • 如何把代码放在网站首页教程门户网站开发如何提高产品排名
  • 建设网站公司招聘做非法网站要多少钱
  • 网站标题权重云建站不能用了吗
  • Python快速入门专业版(四十六):Python类的方法:实例方法、类方法、静态方法与魔术方法
  • 嵌入式 - 内核驱动2 - 实现设备驱动
  • 做机械设计的网站长沙网站推广
  • 景泰做网站wordpress资源分享
  • Vue 3 中的事件总线
  • OCR 如何精准识别越南身份证?
  • FPGA自学笔记--VIVADO FIFO IP核控制和使用
  • 张家界官方网站查询网ip138子域名