springboot+jdk1.8整合neo4j


springboot+jdk1.8整合neo4j


看了两天的neo4j,因为工作要用准备整合到自己项目里,踩了不少坑,记下笔记省的以后再用。
neo4j需要4.0版本以下的,因为4.0版本以上需要jdk11

springboot里面有集成的依赖,只要导入即可

		<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-neo4jartifactId>
            <version>2.3.7.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.neo4jgroupId>
            <artifactId>neo4j-ogm-http-driverartifactId>
            <version>3.2.1version>
        dependency>

配置application.properties(废弃):

org.neo4j.driver.uri=bolt://localhost:7687
org.neo4j.driver.authentication.username=neo4j
org.neo4j.driver.authentication.password=password

定义实体类:

@NodeEntity(label = "test")
@Data
public class TestNode {

    @Id
    private Long id;

    @Property("user_name")
    private String userName;
}
//@NodeEntity:声明该类为Neo4j的节点类

定义仓库

public interface TestRepository extends Neo4jRepository<TestNode,Long> {

    /**
     * CQL语法
     * @param id
     * @return
     */
    @Query("match (t:test{user_name:{id}})return t")
    public List<TestNode> findByCode(@Param("id") Long id);


    @Query("create (t:test{code:{id},user_name:{userName}}) return t")
    public TestNode saveTestee(@Param("id")Long id,@Param("userName")String userName);
}

定义service层

@Service
public class Neo4jService {

    @Autowired
    private TestRepository testRepository;

    public List<TestNode> findByCode(Long id){
        return testRepository.findByCode(id);
    }


    public TestNode saveTest(Long id,String userName){
        return testRepository.saveTest(id,userName);
    }

定义controller层调用

@RestController
@RequestMapping("/test")
public class TesteeKnowledgeController {

    private static final Logger log = LoggerFactory.getLogger(TesteeKnowledgeController.class);


    @Autowired
    private Neo4jService neo4jService;




    @GetMapping(path = "/getTest/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<TestNode> getTest(@PathVariable("id")Long id) {

    return neo4jService.findByCode(id);
    }

    @GetMapping(path = "/saveTestee", produces = MediaType.APPLICATION_JSON_VALUE)
    public TestNode saveTestee(@RequestParam("id")Long id,@RequestParam("name")String name) {

        return neo4jService.saveTestee(id,name);
    }
}

到这一步启动报错,TestRepository没有注入到bean里去,创建neo4j的配置文件,并把neo4j的连接信息写入到代码里(application.properties就不用了)

@Configuration
/**
*不要使用这个注解,不然spring会去找它下面的mapper.xml文件
*/
//@EnableNeo4jRepositories(basePackages = "com.example.domain.TestRepository")
@EnableTransactionManagement
class Neo4jTestConfig {


    @Bean
    public org.neo4j.ogm.config.Configuration configuration() {
        org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder()
                .uri("bolt://localhost:7687")
                .credentials("neo4j", "pwssword")
                .build();
        return configuration;
    }

    @Bean
    public Neo4jTransactionManager transactionManager() throws Exception {
        return new Neo4jTransactionManager(sessionFactory());
    }

    @Bean
    public SessionFactory sessionFactory() {
        return new SessionFactory(configuration(),"com.xlpg.domain");
    }

创建SessionFactory一定要传入configuration(),不然无效。
接下来启动调用
springboot+jdk1.8整合neo4j_第1张图片
差不多一步一坑,主要还是时间太短了,没有时间去仔细研究neo4j数据库

技术太差了,第一篇博客留给自己做纪念吧

你可能感兴趣的:(java,neo4j)