基于SDN4.2.4的neo4j实例
首先添加maven依赖
(1)SND的声明
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
(2)neo4j-ogm-test的声明
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.9.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-test</artifactId>
<version>2.1.1</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>3.1.1</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>3.1.1</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.neo4j.test</groupId>
<artifactId>neo4j-harness</artifactId>
<version>3.1.1</version>
<scope>test</scope>
</dependency>
domain model(com.neo4j.domain)
@NodeEntity
public class Movie {
@GraphId
private Long id;
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Movie(){
}
public Movie(String title){
this.title = title;
}
}
repository(com.neo4j.repositories)
public interface PersonRepository extends Neo4jRepository<Person,Long>{
Person findByName(String name);
}
configuration(ogm.repositories,SDN会自动扫描该文件,以及配置方法的声明和一个使用实例)
driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
URI=http://neo4j:peijie@localhost:7474
@Configuration
@EnableNeo4jRepositories(basePackages = "com.neo4j.repository")
@EnableTransactionManagement
@ComponentScan("com.neo4j")
public class MyConfiguration {
@Bean
public SessionFactory sessionFactory() {
// with domain entity base package(s)
return new SessionFactory("com.neo4j.entity");
}
@Bean
public Neo4jTransactionManager transactionManager() {
return new Neo4jTransactionManager(sessionFactory());
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MyConfiguration.class})
public class PersonTest {
@Autowired
PersonRepository personRepository;
@Test
public void test(){
Person person = new Person("哈士奇","安徽","拆家");
personRepository.save(person);
System.out.println("保存成功!");
}
}