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 {

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">int</span> NEO4J_PORT = 7474<span style="color: #000000;">;

@Bean
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> SessionFactory getSessionFactory() {
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> SessionFactory("com.neo4j.domain"<span style="color: #000000;">);
}

   //配置事务
@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;
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Long getId() {
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> id;
}

@Override
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">boolean</span><span style="color: #000000;"> equals(Object o) {
    </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span> ==<span style="color: #000000;"> o)
        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">if</span> (o == <span style="color: #0000ff;">null</span> || id == <span style="color: #0000ff;">null</span> || getClass() !=<span style="color: #000000;"> o.getClass())
        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    Entity entity </span>=<span style="color: #000000;"> (Entity) o;
    </span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">id.equals(entity.id))
        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
}

@Override
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> hashCode() {
    </span><span style="color: #0000ff;">return</span> (id == <span style="color: #0000ff;">null</span>) ? -1<span style="color: #000000;"> : id.hashCode();
}
    </span><span style="color: #008000;">//</span><span style="color: #008000;">属性</span>
<span style="color: #0000ff;">private</span><span style="color: #000000;"> String userId;

</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String name;

</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String desc;
</span><span style="color: #008000;">//</span><span style="color: #008000;">配置转换</span>
@DateString("yy-MM-dd"<span style="color: #000000;">)
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Date dateTime;
    </span><span style="color: #008000;">//</span><span style="color: #008000;">设置关系</span>
@Relationship(type = "HAVE_PROP", direction=<span style="color: #000000;">Relationship.OUTGOING)
List</span>&lt;Property&gt; properties = <span style="color: #0000ff;">new</span> ArrayList&lt;Property&gt;<span style="color: #000000;">();

@Relationship(type </span>= "HAVE_SERVICE", direction=<span style="color: #000000;">Relationship.OUTGOING)
Set</span>&lt;Service&gt; services = <span style="color: #0000ff;">new</span> HashSet&lt;Service&gt;<span style="color: #000000;">();

@Relationship(type </span>= "HAVE_PROP"<span style="color: #000000;">)
Set</span>&lt;ThingPropRelation&gt; propRelations = <span style="color: #0000ff;">new</span> HashSet&lt;ThingPropRelation&gt;<span style="color: #000000;">();

@Relationship(type </span>= "HAVE_SERVICE"<span style="color: #000000;">)
Set</span>&lt;ThingServiceRelation&gt; serviceRelations = <span style="color: #0000ff;">new</span> HashSet&lt;ThingServiceRelation&gt;<span style="color: #000000;">();

@Relationship(type </span>= "HAVE_SERVICE"<span style="color: #000000;">)
Set</span>&lt;ThingServiceRelation&gt; serviceRelations = <span style="color: #0000ff;">new</span> HashSet&lt;ThingServiceRelation&gt;<span style="color: #000000;">();

}

复制代码

设置节点的关系

复制代码
//设置关系实体
@RelationshipEntity(type="HAVE_PROP")
public class ThingPropRelation extends Entity {
    //开始节点
    @StartNode
    Thing thing;
     //结束节点
    @EndNode
    Property property;
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ThingPropRelation() {
}

</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Thing getThing() {
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> thing;
}

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setThing(Thing thing) {
    </span><span style="color: #0000ff;">this</span>.thing =<span style="color: #000000;"> thing;
}

</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Property getProperty() {
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> property;
}

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setProperty(Property property) {
    </span><span style="color: #0000ff;">this</span>.property =<span style="color: #000000;"> property;
}

}

复制代码

6、设置repository

复制代码
//接口继承GraphRepository
//提供基础的保存修改删除查询功能
//特殊查询可以通过@Query注解实现
public interface ThingRepository extends GraphRepository<Thing> {
    Thing findByName(String name);
@Query(</span>"MATCH (t:Thing {name:{0}})-[r:HAVE_PROP]-&gt;(p) RETURN p"<span style="color: #000000;">)
Iterable</span>&lt;Property&gt;<span style="color: #000000;"> getThingPropertyByThingName(String thingName);

}

复制代码

7、应用

@Autowired
private ThingRepository thingRepository; 

调用

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

原文地址:https://www.cnblogs.com/changj/p/6021775.html

posted @ 2019-08-15 17:26  星朝  阅读(978)  评论(0编辑  收藏  举报