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: localhost
  port: 7687
  username: neo4j
  password: 123456

management:
  endpoint:
    health:
      show-details: always

logging:
  level:
    org.neo4j.ogm.drivers.bolt.request: DEBUG
    org.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;

	@Bean
	public 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)
	@Override
	public 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);
	}

#vo

import 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
	@Override
	public 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);
	}

}

启动成功就可以演示了。

相关推荐
撩得Android一次心动15 分钟前
Android LiveData 全面解析:使用Java构建响应式UI【源码篇】
android·java·android jetpack·livedata
组合缺一19 分钟前
Solon AI (Java) v3.9 正式发布:全能 Skill 爆发,Agent 协作更专业!仍然支持 java8!
java·人工智能·ai·llm·agent·solon·mcp
MSTcheng.23 分钟前
【C++】C++11新特性(二)
java·开发语言·c++·c++11
一 乐27 分钟前
校园二手交易|基于springboot + vue校园二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
KIKIiiiiiiii28 分钟前
微信个人号API二次开发中的解决经验
java·人工智能·python·微信
80530单词突击赢29 分钟前
SpringBoot整合SpringMVC全解析
java·spring boot·后端
vx1_Biye_Design39 分钟前
基于Spring Boot+Vue的学生管理系统设计与实现-计算机毕业设计源码46223
java·vue.js·spring boot·spring·eclipse·tomcat·maven
vx_Biye_Design40 分钟前
基于Spring Boot+vue的湖北旅游景点门票预约平台的设计--毕设附源码29593
java·vue.js·spring boot·spring cloud·servlet·eclipse·课程设计
qq5_81151751542 分钟前
web城乡居民基本医疗信息管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
hdsoft_huge1 小时前
1panel面板中部署SpringBoot和Vue前后端分离系统 【图文教程】
vue.js·spring boot·后端