springboot整合neo4j

springboot整合neo4j 执行指令

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

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-neo4jartifactId>
        dependency>
        
        <dependency>
            <groupId>org.neo4jgroupId>
            <artifactId>neo4j-ogm-http-driverartifactId>
            <version>3.0.1version>
        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<>());
    }
}

你可能感兴趣的:(SpringBoot,数据库)