springboot整合neo4j

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<>());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章