Neo4j與springdata集成


1、maven工程需導入的jar包

<!-- neo4j -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.3.2</version>
</dependency>

2、對應spring的jar包

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>

3、加載neo4j的驅動配置

@Configuration
@EnableNeo4jRepositories("com.neo4j.repository")
@EnableTransactionManagement

public class Neo4jApplication extends Neo4jConfiguration {

    public static final int NEO4J_PORT = 7474;
    
    @Bean
    public SessionFactory getSessionFactory() {
        return new SessionFactory("com.neo4j.domain");
    }
   //配置事務
    @Bean
    @Qualifier("neo4jTransactionManager")
    public Neo4jTransactionManager neo4jTransactionManager() throws Exception {
        return new Neo4jTransactionManager(getSession());
    }

}

4、連接方式采用httpDriver

添加配置文件ogm.properties

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

5、domain實體配置

//節點注解(可以添加label標簽)
@NodeEntity
public class Thing{
        //neo4j中節點的ID
       private Long id;

    public Long getId() {
        return id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || id == null || getClass() != o.getClass())
            return false;
        Entity entity = (Entity) o;
        if (!id.equals(entity.id))
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        return (id == null) ? -1 : id.hashCode();
    }
        //屬性
    private String userId;
    
    private String name;
    
    private String desc;
    //配置轉換
    @DateString("yy-MM-dd")
    private Date dateTime;
        //設置關系
    @Relationship(type = "HAVE_PROP", direction=Relationship.OUTGOING)
    List<Property> properties = new ArrayList<Property>();
    
    @Relationship(type = "HAVE_SERVICE", direction=Relationship.OUTGOING)
    Set<Service> services = new HashSet<Service>();
    
    @Relationship(type = "HAVE_PROP")
    Set<ThingPropRelation> propRelations = new HashSet<ThingPropRelation>();
    
    @Relationship(type = "HAVE_SERVICE")
    Set<ThingServiceRelation> serviceRelations = new HashSet<ThingServiceRelation>();
    
    @Relationship(type = "HAVE_SERVICE")
    Set<ThingServiceRelation> serviceRelations = new HashSet<ThingServiceRelation>();
}

設置節點的關系

//設置關系實體
@RelationshipEntity(type="HAVE_PROP")
public class ThingPropRelation extends Entity {
    //開始節點
    @StartNode
    Thing thing;
     //結束節點
    @EndNode
    Property property;

    public ThingPropRelation() {
    }
    
    public Thing getThing() {
        return thing;
    }

    public void setThing(Thing thing) {
        this.thing = thing;
    }

    public Property getProperty() {
        return property;
    }

    public void setProperty(Property property) {
        this.property = property;
    }
}

6、設置repository

//接口繼承GraphRepository
//提供基礎的保存修改刪除查詢功能
//特殊查詢可以通過@Query注解實現
public interface ThingRepository extends GraphRepository<Thing> {
    Thing findByName(String name);
    
    @Query("MATCH (t:Thing {name:{0}})-[r:HAVE_PROP]->(p) RETURN p")
    Iterable<Property> getThingPropertyByThingName(String thingName);
}

7、應用

@Autowired
private ThingRepository thingRepository; 

調用

Thing thing = thingRepository.findByName("thing");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM