SpringBoot集成Neo4j

Java框架

浏览数:80

2019-8-23

目前在网络上对于SpringBoot集成Neo4j的文章比较少,而且大部分都过时了,经过一番折腾,终于集成成功。

依赖最新的Spring Data Neo4j

<dependency>    
    <groupId>org.springframework.data</groupId>    
    <artifactId>spring-data-neo4j</artifactId>    
    <version>4.1.4.RELEASE</version>
</dependency>
<parent>    
    <groupId>org.springframework.boot</groupId>    
    <artifactId>spring-boot-starter-parent</artifactId>    
    <version>1.4.0.RELEASE</version>    
    <relativePath/>
</parent>

配置

@org.springframework.context.annotation.Configuration
public class Neo4jConfigurationService extends Neo4jConfiguration {

    @Override
    @Bean
    public SessionFactory getSessionFactory() {
        Configuration config = new Configuration();
        DriverConfiguration driverConfiguration = config.driverConfiguration();
        driverConfiguration.setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver");
        driverConfiguration.setCredentials("neo4j", "root");
        driverConfiguration.setURI("http://localhost:7474");

        return new SessionFactory(config, "com.cis.biz.neo4j");
    }

    @Bean
    @Override
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session getSession() throws Exception {
        return super.getSession();
    }
}

引用

@Service
public class Neo4jService {

    @Autowired
    private Neo4jOperations neo4jTemplate;

    public void test1() {

        for (int i = 0; i < 10; i++) {
            ActorEntity actor = new ActorEntity();
            actor.setName("ff");
            neo4jTemplate.save(actor);
        }

    }

}

作者:青风雨