本文是基于Maven搭建的springboot和spring-data-neo4j整合的开发环境,本文底部提供该demo工程的github地址。
环境搭建步骤:
1.使用任何一款自己熟悉的IDE开发工具创建一个普通的Maven工程;
2.maven工程建好之后,在工程目录下的pom.xml文件中添加依赖;
3.配置springboot启动入口;
4.整合springboot和spring-data-neo4j
5.配置neo4j连接(本文使用的是http连接方式)
6.添加测试依赖及配置
7.创建Neo4j简单的OGM
添加maven依赖
在创建好maven工程之后,将以下依赖直接添加到工程目录下的pom.xml文件中。
<properties>
<springboot.version>1.5.2.RELEASE</springboot.version>
<spring.version>4.3.7.RELEASE</spring.version>
<junit.version>4.12</junit.version>
<jdk.compile.version>3.5.1</jdk.compile.version>
<jdk.version>1.8</jdk.version>
<charset>UTF-8</charset>
</properties>
<dependencies>
<!--springboot以及spring-data-neo4j依赖配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
<version>${springboot.version}</version>
</dependency>
<!--测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${springboot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>${springboot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${jdk.compile.version}</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${charset}</encoding>
</configuration>
</plugin>
</plugins>
</build>
创建工程包结构
添加好Maven依赖之后创建如下图所示的包结构:
工程包结构
整合springboot和spring-data-neo4j
1、在org.cooze.springboot.neo4j包下创建SpringBoot启动引导类BootStrap.java,代码如下:
//扫描neo4j库操作包路径,一定要有而且要配置正确
@EnableNeo4jRepositories(basePackages = {"org.cooze.springboot.neo4j.repository." })
//扫描neo4j实体类包路径,重申一定要有而且要配置正确
@EntityScan(basePackages = {"org.cooze.springboot.neo4j.entity."})
@SpringBootApplication
public class BootStrap {
public static void main(String[] args){
SpringApplication.run(BootStrap.class,args);
}
}
2、在maven工程的src/main/resources目录下穿件Springboot的配置文件application.yml,文件内容如下:
#服务端口
service:
port: 8080
#neo4j连接配置
spring:
data:
neo4j:
username: neo4j
password: 123456
uri: http://localhost:7474
#日志配置(log4j日志配置请点击文章底部的github地址,到git上获取)
logging:
config:
classpath: log4j2.xml
3、在org.cooze.springboot.neo4j.entity包下创建Neo4j节点实体Student.java,代码如下:
@NodeEntity
public class Student {
@GraphId
private Long id;//必须指定Long类型的一个图形数据库id,图数据库用于自编号,保证图数据库中的节点唯一。
private String name;
private String sex;
private int age;
public Student() {
}
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
//getter setter方法省略.....
}
4、在org.cooze.springboot.neo4j.repository包下创建StudentRepositry.java接口,直接继承spring-data-neo4j的GraphRepository接口,接口内容如下:
public interface StudentRepositry extends GraphRepository<Student> {
@Query("MATCH (n:Student) WHERE n.name = {name} RETURN n")
Student getStudentByName(@Param("name") String Name);
//这两个方法实现的功能是一样的,上面那个是直接写Neo4j的Cypher语句来查询数据,很强大很好用很灵活,Cypher语句不在本文描述的范围
Student findByName( String Name );
}
5、在org.cooze.springboot.neo4j.service包下创建StudentService.java接口,接口代码如下:
public interface StudentService {
Student getStudent(String name);
Student addStudent(Student student);
}
6、在org.cooze.springboot.neo4j.service包下创建impl包和StudentServiceImpl.java实现类,代码如下:
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepositry studentRepositry;
@Override
public Student getStudent(String name) {
return studentRepositry.findByName(name);
}
@Override
public Student addStudent(Student student) {
return studentRepositry.save(student);
}
}
7、在org.cooze.springboot.neo4j.controller包下创建StudentController.java类,代码如下:
@RestController
public class StudentController {
@Autowired
private StudentRepositry studentRepositry;
@Autowired
private StudentService studentService;
@PostMapping(value = "/student")
@ResponseBody
public Student addStudent(@RequestBody Student student){
return studentService.addStudent(student);
}
@GetMapping(value = "/student/{name}")
public Student getStudent(@PathVariable(name = "name") String name){
return studentService.getStudent(name);
}
}
通过以上7个步骤,就已经完成了springboot和spring-data-neo4j的整合工作。但是感觉还是了缺点什么,想想,原来是缺了单元测试,下面开始介绍基于以上环境搭建的单元测试环境。
单元测试环境搭建
单元测试依赖已经在开始的时候添加了,在这里就不重复介绍了。
1、在项目目录下的src/test/java创建创建包结构org.cooze.springboot.neo4j.test;
2、在org.cooze.springboot.neo4j.test包下创建单元测试配置类Configure.java,代码如下:
@EnableAutoConfiguration
@EnableNeo4jRepositories(basePackages = {"org.cooze.springboot.neo4j.repository."})
@EntityScan(basePackages = {"org.cooze.springboot.neo4j.entity."})
@EnableTransactionManagement
@ComponentScan
@Configuration
public class Configure {
public static final String URI = "http://neo4j:123456@localhost:7474";
@Bean
public org.neo4j.ogm.config.Configuration getConfiguration(){
org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
config.driverConfiguration()
.setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
.setURI(URI);
return config;
}
}
3、在org.cooze.springboot.neo4j.test包下创建StudentTest.java单元测试类,代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Configure.class)
public class StudentTest {
//如果报错有红线没关系,不影响单元测试,那只是IDE检查有问题而已
@Autowired
private StudentRepositry studentRepositry;
@Test
public void addStudent(){
Student student = studentRepositry.save(new Student("张三","男",10));
}
@Test
public void findStudent1(){
Student student = studentRepositry.findByName("张三");
}
@Test
public void findStudent2(){
Student student = studentRepositry.getStudentByName("张三");
}
}
运行测试类中的addStudent()方法后,在数据库中查找存在如下图数据:
数据插入结果到此,单元测试环境也搭好了。
本文项目代码github地址:
https://github.com/Cooze/sample-project/tree/master/springboot-neo4j-env
网友评论