刚开始按网上博客搭建 spring boot 和 neo4j一直报sessionFactory找不到,直到下载了spring-data-neo4j的实例demo对比才搭建成功,而且用户名是neo4j,不是创建一个数据库时的名字,搞了几个小时终于搞成功了。以后还是多看官方文档,搜来搜去,什么鬼都搜不到。记录一下自己踩的坑。

 

工程地址:​​https://github.com/CNXXPP/neo4j-demo​

 

pom文件

是springboot进行过封装的spring-boot-starter-data-neo4j提供了一些基础的方法,有点像spring-data-jpa






​<?xml version="1.0" encoding="UTF-8"?>​






​<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"​






​ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">​






​ <modelVersion>4.0.0</modelVersion>​












​ <groupId>com.xp</groupId>​






​ <artifactId>neo4j-demo</artifactId>​






​ <version>0.0.1-SNAPSHOT</version>​






​ <packaging>jar</packaging>​












​ <name>neo4j-demo</name>​






​ <description>Demo project for Spring Boot</description>​












​ <parent>​






​ <groupId>org.springframework.boot</groupId>​






​ <artifactId>spring-boot-starter-parent</artifactId>​






​ <version>2.0.3.RELEASE</version>​






​ <relativePath/> <!-- lookup parent from repository -->​






​ </parent>​












​ <properties>​






​ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>​






​ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>​






​ <java.version>1.8</java.version>​






​ </properties>​












​ <dependencies>​






​ <dependency>​






​ <groupId>org.springframework.boot</groupId>​






​ <artifactId>spring-boot-starter-data-neo4j</artifactId>​






​ </dependency>​






​ <!--<dependency>-->​






​ <!--<groupId>org.neo4j.driver</groupId>-->​






​ <!--<artifactId>neo4j-java-driver</artifactId>-->​






​ <!--</dependency>-->​












​ <dependency>​






​ <groupId>org.springframework.boot</groupId>​






​ <artifactId>spring-boot-starter-test</artifactId>​






​ <scope>test</scope>​






​ </dependency>​






​ </dependencies>​












​ <build>​






​ <plugins>​






​ <plugin>​






​ <groupId>org.springframework.boot</groupId>​






​ <artifactId>spring-boot-maven-plugin</artifactId>​






​ </plugin>​






​ </plugins>​






​ </build>​


















​</project>​







​@NodeEntity(label = "user")​






​public class UserNode implements Serializable {​












​ @Id @GeneratedValue​






​ private Long id;​












​ private String name;​












​ private String userId;​












​ private String companyId;​












​ public String getName() {​






​ return name;​






​ }​












​ public void setName(String name) {​






​ this.name = name;​






​ }​












​ public String getUserId() {​






​ return userId;​






​ }​












​ public void setUserId(String userId) {​






​ this.userId = userId;​






​ }​












​ public String getCompanyId() {​






​ return companyId;​






​ }​












​ public void setCompanyId(String companyId) {​






​ this.companyId = companyId;​






​ }​












​ @Override​






​ public String toString() {​






​ return "UserNode{" +​






​ "id=" + id +​






​ ", name='" + name + '\'' +​






​ ", userId='" + userId + '\'' +​






​ ", companyId='" + companyId + '\'' +​






​ '}';​






​ }​






​}​


dao类 






​@Repository​






​public interface UserRepository extends Neo4jRepository<UserNode,Long>{​












​}​


 测试类






​@Component​






​public class UserRepositoryTest extends Neo4jDemoApplicationTests{​












​ Logger logger = LoggerFactory.getLogger(UserRepositoryTest.class);​












​ @Autowired​






​ UserRepository userRepository;​












​ @Test​






​ public void createUserNode(){​






​ System.out.println(userRepository);​






​ UserNode userNode = new UserNode();​






​ userNode.setName("xupan");​






​ userNode.setUserId("123");​






​ UserNode save = userRepository.save(userNode);​






​ logger.info(save.toString());​






​ Assert.assertTrue(save!=null);​






​ }​












​ @Test​






​ public void delAll(){​






​ userRepository.deleteById(new Long(27));​












​ }​






​}​


启动类的@EnableNeo4jRepositories一定要加 






​@SpringBootApplication​






​@EnableNeo4jRepositories​






​public class Neo4jDemoApplication {​












​ public static void main(String[] args) {​






​ SpringApplication.run(Neo4jDemoApplication.class, args);​






​ }​






​}​


Application.yml






​spring:​






​ data:​






​ neo4j:​






​ uri: bolt://localhost:7687​






​ username: neo4j​






​ password: 1123​