专业的编程技术博客社区

网站首页 > 博客文章 正文

Springboot2.3集成neo4j的过程和踩坑记

baijin 2024-11-24 11:54:01 博客文章 3 ℃ 0 评论

最近有个需求是要使用neo4j这个数据库,看官方的介绍是个图形数据库,官方没有看到和springboot整合的文档(可能是我没找着),那就自己动手吧,找了好久网上的neo4j的资料很少,好多都是过时了的,大家互相抄,跟着那些没有经过验证的文章让我踩了不少坑。折腾了好久皇天不负有心人终于成功了,那我就把整合的过程记录下来,方便遇到同样问题的小伙伴查看,下面正式开始:

在浏览器访问http://安装neo4j机器的IP:7474,会出现下面的页面,默认用户名密码是neo4j

至此,neo4j就算是启动成功了,下面我们开始在idea中建立springboot工程,增加下面的maven依赖:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-http-driver</artifactId>
        </dependency>
    </dependencies>

在启动类中开启对neo4j的支持:

@SpringBootApplication
public class Neo4jdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(Neo4jdemoApplication.class, args);
    }
}

application.properties配置文件:

spring.data.neo4j.uri=http://192.168.2.101:7474
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456

增加配置类(网上的好多人说不需要配置类,其实会报sessionFactory空指针异常):

@Configuration
@EnableNeo4jRepositories("com.example.neo4jdemo.dao") // 声明neo4j repository存放地址
public class Neo4jConfig {

    @Value("${spring.data.neo4j.uri}")
    private  String uri;
    @Value("${spring.data.neo4j.username}")
    private  String userName;
    @Value("${spring.data.neo4j.password}")
    private  String password;

    @Bean
    public org.neo4j.ogm.config.Configuration getConfiguration() {
        org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder()
                .uri(uri).connectionPoolSize(100)
                .credentials(userName, password)
                .withBasePackages("com.example.neo4jdemo.dao")
                .build();

        return configuration;
    }

    @Bean
    public SessionFactory sessionFactory() {
        return new SessionFactory(getConfiguration());
    }

    @Bean("neo4jTransaction")
    public Neo4jTransactionManager neo4jTransactionManager(SessionFactory sessionFactory) {
        return new Neo4jTransactionManager(sessionFactory);
    }
}

service类:

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public List<UserEntity> getUser(){
        return userRepository.getUserNodeList();
    }

    @Transactional
    public void addUser(UserEntity userEntity){
        userRepository.save(userEntity);
    }
}

Dao接口:

@Repository
public interface UserRepository extends Neo4jRepository<UserEntity,Long> {


}

现在就可以启动啦,看看效果:

下面我们写个接口测试一下看:

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/addUser")
    public String addUserNode(){
        UserEntity userEntity = new UserEntity();
        userEntity.setName("张三");
        userEntity.setAge(20);
        userService.addUser(userEntity);
        return "success";
    }
}

接口请求成功,我们看看数据库的情况

很明显"张三"这个节点已经成功保存了,neo4j和springboot的整合就完成了

完整demo地址:https://gitee.com/vic_miao/neo4jdemo.git

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表