10 Aralık 2019 Salı

SpringData Neo4J Kullanımı

Maven
Şu satırı dahil ederiz.
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-neo4j</artifactId>
  <version>2.2.2.RELEASE</version>
</dependency>
Şu satırı dahil ederiz
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
  <groupId>org.neo4j.driver</groupId>
  <artifactId>neo4j-java-driver</artifactId>
  <version>4.3.4</version>
</dependency>
Gradle
Şu satırı dahil ederiz
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
    implementation 'org.neo4j.driver:neo4j-java-driver:4.3.4'
}
application.properties
Açıklaması şöyle
In general, for Spring Boot 4.0 and above, the prefix spring.data.neo4j is used to configure the Neo4j database.

For earlier versions of Spring Boot, such as 3.x, the prefix spring.neo4j may be used instead.
Örnek
Şöyle yaparız
spring.data.neo4j.open-in-view=false
spring.data.neo4j.uri=bolt://localhost
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=xxx
Örnek - Eski
Şöyle yaparız
spring.neo4j.uri=bolt://localhost:7687
spring.neo4j.authentication.username=neo4j
spring.neo4j.authentication.password=your_password

Neo4jRepository Arayüzü
Örnek
Şeklen şöyle


Şöyle yaparız
import org.neo4j.springframework.data.core.schema.Id;
import org.neo4j.springframework.data.core.schema.Node;
import org.neo4j.springframework.data.core.schema.Relationship;
@Node
public class User {
  @Id
  private final String username;
  private final String name;
  @Relationship(type = "HAS_SKILL")
  private List<Skill> skills;
  @Relationship(type = "HAS_POSITION")
  private List<Position> positions;
  @Relationship(type = "WORKS_AT")
  private Company company;
  @Relationship(type = "CONNECTED_TO")
  private List<User> connections;
  // Constructors, getters, and setters omitted for brevity
}
@Node
public class Skill {
    @Id
    private final String name;
    // Constructors, getters, and setters omitted for brevity
}
@Node
public class Position {
    @Id
    private final String title;
    @Relationship(type = "BELONGS_TO")
    private Company company;
    // Constructors, getters, and setters omitted for brevity
}

@Node
public class Company {
    @Id
    private final String name;
    @Relationship(type = "HAS_INDUSTRY")
    private Industry industry;
    // Constructors, getters, and setters omitted for brevity
}
@Node
public class Industry {
    @Id
    private final String name;
    // Constructors, getters, and setters omitted for brevity
}
Şöyle yaparız
import org.neo4j.springframework.data.repository.Neo4jRepository;
public interface UserRepository extends Neo4jRepository<User, String> {

  @Query("MATCH (u:User)-[:HAS_SKILL]->(s:Skill) 
  WHERE s.name = $skillName RETURN u")
  List<User> findBySkill(String skillName);

  @Query("MATCH (u:User)-[:HAS_SKILL]->(s:Skill), (u)-[:WORKS_AT]->(c:Company)-[:HAS_INDUSTRY]->(i:Industry) 
    WHERE s.name = $skillName AND i.name = $industryName RETURN u")
  List<User> findBySkillAndIndustry(String skillName, String industryName);

}
@EnableNeo4jRepositories Anotasyonu
Örnek
Şöyle yaparız.
@EnableNeo4jRepositories(basePackages="org.xx.yy.movies.repositories")

Hiç yorum yok:

Yorum Gönder