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") @EnableTransactionManagementpublic 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");
原文地址:https://www.cnblogs.com/changj/p/6021775.html


浙公网安备 33010602011771号