Neo4jOgm2.1的使用

本文介绍了在Mac OS上使用Neo4j OGM 2.1的步骤,包括如何创建Maven项目,配置pom.xml,设置实体类,以及通过HTTPDriver进行配置。提供了两种配置方式:Java代码配置和ogm.properties文件自动配置,并给出了官方的测试代码运行结果。

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

一. 准备工作
本人系统:Mac OS
安装maven3.0:安装教程
如果eclipse中没有安装maven插件的话需要自己安装

二. neo4j ogm 2.1使用
1. 新建maven project
2.配置pom.xml,配置完成后会自动将所需要的各个jar包倒入到maven dependencies下。

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <neo4j.ogm.version>2.1.1</neo4j.ogm.version>
  </properties>

<dependencies>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-api</artifactId>
            <version>${neo4j.ogm.version}</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-core</artifactId>
            <version>${neo4j.ogm.version}</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-http-driver</artifactId>
            <version>${neo4j.ogm.version}</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-compiler</artifactId>
            <version>${neo4j.ogm.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.14</version>
        </dependency>
           <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>
</dependencies>

3.开始编写代码,设置实体类
使用neo4j ogm的官方手册给出的Movie和Actor的例子:

@NodeEntity
public class Actor {

    @GraphId
    private Long id;
    private String name;

    @Relationship(type = "ACTS_IN", direction = "OUTGOING")
    private Set<Movie> movies = new HashSet<>();

    public Actor() {
    }

    public Actor(String name) {
        this.name = name;
    }

    public void actsIn(Movie movie) {
        movies.add(movie);
        movie.getActors().add(this);
    }
}


@NodeEntity
public class Movie {

    @GraphId
    private Long id;
    private String title;
    private int released;

    @Relationship(type = "ACTS_IN", direction = "INCOMING")
    Set<Actor> actors;

    public Movie() {
    }

    public Movie(String title, int year) {
        this.title = title;
        this.released = year;
    }

}

4 .配置
此处使用的是httpdriver,以此为例为例:
方法一:使用java代码进行配置
有两种写法:
(1)

Configuration configuration = new Configuration();
        configuration.driverConfiguration()
         .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
         .setURI("http://localhost:7474")
         .setCredentials("xxx", "xxxxx");

session则使用如下代码:

SessionFactory sessionFactory = new SessionFactory(configuration"com.qcl.test.entities");
        Session session = sessionFactory.openSession();

(2)

 Components.configuration()
         .driverConfiguration()
    .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
         .setURI("http://xxx:xxxxx@localhost:7474")
         .setCredentials("xxx", "xxxxx");

session如下:

SessionFactory sessionFactory = new SessionFactory("com.qcl.test.entities");
        Session session = sessionFactory.openSession();

方法二:使用ogm.properties自动配置
配置内容如下:

driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
URI=http://xxx:xxxxx@localhost:7474

只需要将ogm.properties放在src/main/resources下面,然后使用上面的session代码就可以自动配置了。

5 .官方给出的完整测试代码:

//Set up the Session
SessionFactory sessionFactory = new SessionFactory("movies.domain");
Session session = sessionFactory.openSession();

Movie movie = new Movie("The Matrix", 1999);

Actor keanu = new Actor("Keanu Reeves");
keanu.actsIn(movie);

Actor carrie = new Actor("Carrie-Ann Moss");
carrie.actsIn(movie);

//Persist the movie. This persists the actors as well.
session.save(movie);


//Load a movie
Movie matrix = session.load(Movie.class, movie.getId());
for(Actor actor : matrix.getActors()) {
    System.out.println("Actor: " + actor.getName());
}

三. 运行结果:
这里写图片描述

附录:完整代码可见本人github ,或者参见官方手册。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值