springboot整合neo4j

本文介绍如何在SpringBoot项目中整合Neo4j图数据库,包括pom.xml依赖配置、application.properties配置文件设置、实体类定义、DAO层实现及Service层CQL命令执行等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot整合neo4j 执行指令

之前上网搜索配置都很凌乱,于是在 springboot neo4j文档 中摸索了下。
- pom配置

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>
        <!-- add this dependency if you want to use the HTTP driver -->
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-http-driver</artifactId>
            <version>3.0.1</version>
        </dependency>
  • application.properties
# 写入自己neo4j配置
spring.data.neo4j.uri=http://fp-bd13:7474
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=admin
  • 实体Po
@Data //lombok注解
@NodeEntity
@ToString
public class Table {
    @Id
    @GeneratedValue
    private Long id;
    @Property
    private String name;
    @Property
    private String desc;
    @Property
    private String fields;
}
  • Dao

继承 Neo4jRepository 接口

@Repository
public interface TableDao extends Neo4jRepository<Table,Long> {
}
  • Service

注入Dao,如果注入neo4j的SessionFactory获取session可以用来执行cql命令。

@RestController
@RequestMapping("/v1/table")
public class TableService {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    TableDao tableDao;

    @RequestMapping("/create")
    public void create(@RequestBody Table table) {
        tableDao.save(table);
    }

    @RequestMapping("/executeCQL")
    public void executeCQL(String cypher){
        // cypher = "match (n: Table} ) delete n"
        sessionFactory.openSession().query(cypher,new HashMap<>());
    }
}
### Spring Boot 整合 Neo4j 示例教程 #### 三、添加Neo4j依赖 为了使Spring Boot项目能够与Neo4j图数据库交互,需在项目的`pom.xml`文件中加入必要的依赖项。这些依赖不仅包含了Neo4j的Java驱动程序,还集成了Spring Data Neo4j所提供的简化接口,从而让开发人员能更加便捷地执行CRUD操作和其他高级功能[^1]。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency> ``` #### 四、配置Neo4j连接信息 接着,在`application.properties`或`application.yml`文件内指定Neo4j服务器的具体地址、端口号、用户名及密码等参数,以便应用程序建立到目标数据库的有效链接[^4]。 对于`.properties`格式: ```properties spring.data.neo4j.uri=bolt://localhost:7687 spring.data.neo4j.username=neo4j spring.data.neo4j.password=password ``` 而对于`.yml`格式,则如下所示: ```yaml spring: data: neo4j: uri: bolt://localhost:7687 username: neo4j password: password ``` #### 五、创建实体类 基于领域模型设计原则定义相应的实体对象,并利用注解方式映射至Neo4j中的节点(Node)或关系(Relationship),这有助于提高代码可读性和维护性的同时也增强了系统的灵活性和扩展能力[^3]。 例如,下面是一个简单的用户实体示例: ```java import org.neo4j.ogm.annotation.GeneratedValue; import org.neo4j.ogm.annotation.Id; import org.neo4j.ogm.annotation.NodeEntity; @NodeEntity public class User { @Id @GeneratedValue private Long id; private String name; public User() {} public User(String name) { this.name = name; } // getters and setters... } ``` #### 六、编写Repository接口 借助Spring Data JPA风格的仓库模式来封装针对特定类型的持久化逻辑,只需声明继承自`CrudRepository<T,ID>`或其他更具体的泛型接口即可获得一系列常用方法的支持,无需手动编码SQL语句就能完成增删改查等功能[^2]。 ```java import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository<User, Long> { } ``` #### 七、测试验证 最后一步就是通过单元测试或者其他形式检验整个流程是否正常工作,比如向Neo4j实例插入新记录并查询返回预期的结果集合等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值