欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Neo4jOgm2.1的使用

程序员文章站 2022-05-29 20:04:56
...

一. 准备工作
本人系统: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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

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;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

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");
  • 1
  • 2
  • 3
  • 4
  • 5

session则使用如下代码:

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

(2)

 Components.configuration()
         .driverConfiguration()
    .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
         .setURI("http://xxx:aaa@qq.com:7474")
         .setCredentials("xxx", "xxxxx");
  • 1
  • 2
  • 3
  • 4
  • 5

session如下:

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

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

driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
URI=http://xxx:[email protected]: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());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

三. 运行结果:
Neo4jOgm2.1的使用

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