您的位置:首页 > 博客中心 > 数据库 >

Hello World 之Spring Boot 调用图数据库Neo4j

时间:2022-03-15 15:03

  1. 明日歌
  2. [清]钱鹤滩
  3. 明日复明日,明日何其多!
  4. 我生待明日,万事成蹉跎

spring.data.neo4j.username=neo4j
  • spring.data.neo4j.password=helloworld
  • application.yml版:      

    1. spring:
    2. data:
    3. neo4j:
    4. username: neo4j
    5. password: helloworld

    @NodeEntity
  • public class Legend {
  • @Id @GeneratedValue private Long id;
  • private String name;
  • private Legend() {
  • // Empty constructor required as of Neo4j API 2.0.5
  • };
  • public Legend(String name) {
  • this.name = name;
  • }
  • /**
  • * Neo4j doesn‘t REALLY have bi-directional relationships. It just means when querying
  • * to ignore the direction of the relationship.
  • * https://dzone.com/articles/modelling-data-neo4j
  • */
  • @Relationship(type = "FANS", direction = Relationship.UNDIRECTED)
  • public Set<Legend> fans;
  • public void fansWith(Legend legend) {
  • if (fans == null) {
  • fans = new HashSet<>();
  • }
  • fans.add(legend);
  • }
  • public String toString() {
  • //java 8 stream and optional
  • return this.name + "‘s fans => "
  • + Optional.ofNullable(this.fans).orElse(
  • Collections.emptySet()).stream()
  • .map(Legend::getName)
  • .collect(Collectors.toList());
  • }
  • public String getName() {
  • return name;
  • }
  • public void setName(String name) {
  • this.name = name;
  • }
  • }
  • import org.springframework.data.repository.CrudRepository;
  • public interface LegendRepo extends CrudRepository<Legend, Long> {
  • Legend findByName(String name);
  • }
  • MATCH (n:`Legend`) WHERE n.`name` = { `name_0` } WITH n RETURN n,
  • [ [ (n)-[r_f1:`FANS`]-(l1:`Legend`) | [ r_f1, l1 ] ] ], ID(n) with params {name_0=杨过}
  • 如果要表达Or或者And关系(假设legend有属性level),方法名将会是

    findByNameAndLevel(String name,String level)

    如果要分页,需要继承PagingAndSortingRepository,而不是CrudRepository 。有关springdata的更多细节,笔者将会在以后的博客中详细介绍。

    @SpringBootApplication
  • @EnableNeo4jRepositories
  • public class SpringBootNeo4jApplication {
  • public static void main(String[] args) {
  • SpringApplication.run(SpringBootNeo4jApplication.class, args);
  • }
  • }
  • @RunWith(SpringRunner.class)
  • @SpringBootTest
  • public class SpringBootNeo4jApplicationTests {
  • @Autowired
  • LegendRepo legendRepo;
  • private final static Logger log = LoggerFactory.getLogger(SpringBootNeo4jApplicationTests.class);
  • @Test
  • public void contextLoads() {
  • legendRepo.deleteAll();
  • Legend yangguo = new Legend("杨过");
  • Legend dragonGirl = new Legend("小龙女");
  • Legend guoxiang = new Legend("郭襄");
  • List<Legend> team = Arrays.asList(yangguo, dragonGirl, guoxiang);
  • log.info("Before linking up with Neo4j...");
  • //java 8 stream
  • team.stream().forEach(legend -> log.info("\t" + legend.toString()));
  • legendRepo.save(yangguo);
  • legendRepo.save(dragonGirl);
  • legendRepo.save(guoxiang);
  • yangguo = legendRepo.findByName(yangguo.getName());
  • yangguo.fansWith(dragonGirl);
  • yangguo.fansWith(guoxiang);
  • legendRepo.save(yangguo);
  • dragonGirl = legendRepo.findByName(dragonGirl.getName());
  • dragonGirl.fansWith(guoxiang);
  • // We already know that dragonGirl is a fan of yangguo
  • legendRepo.save(dragonGirl);
  • // We already know guoxiang fans with yangguo and dragongirl
  • log.info("Look up yangguo‘s fans ...");
  • log.info(legendRepo.findByName("杨过").toString());
  • }
  • }
  • 运行代码查看日志:

    Look up yangguo‘s fans ...
    Request: MATCH (n:`Legend`) WHERE n.`name` = { `name_0` } WITH n RETURN n,[ [ (n)-[r_f1:`FANS`]-(l1:`Legend`) | [ r_f1, l1 ] ] ], ID(n) with params {name_0=杨过}
     杨过‘s fans => [郭襄, 小龙女]
    
    4. Neo4j与Spring boot----一生一代一双人

    Neo4j在某些场合能发挥自己的优势,而用spring boot的方式,使得neo4j的使用非常简单,自从有了spring boot,生活变得好轻松。

    关注spring boot,请继续关注我的博客。


    原文地址:https://blog.csdn.net/weixin_41897365/article/details/79835319

    相关推荐

    电脑软件

    热门排行

    今日推荐

    热门手游