Springboot整合Neo4j開發(fā)及單元測試環(huán)境搭建

本文是基于Maven搭建的springboot和spring-data-neo4j整合的開發(fā)環(huán)境,本文底部提供該demo工程的github地址。

環(huán)境搭建步驟:

1.使用任何一款自己熟悉的IDE開發(fā)工具創(chuàng)建一個普通的Maven工程;
2.maven工程建好之后,在工程目錄下的pom.xml文件中添加依賴;
3.配置springboot啟動入口;
4.整合springboot和spring-data-neo4j
5.配置neo4j連接(本文使用的是http連接方式)
6.添加測試依賴及配置
7.創(chuàng)建Neo4j簡單的OGM

添加maven依賴

在創(chuàng)建好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>
創(chuàng)建工程包結(jié)構(gòu)

添加好Maven依賴之后創(chuàng)建如下圖所示的包結(jié)構(gòu):


工程包結(jié)構(gòu)
整合springboot和spring-data-neo4j

1、在org.cooze.springboot.neo4j包下創(chuàng)建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,文件內(nèi)容如下:

#服務端口
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包下創(chuàng)建Neo4j節(jié)點實體Student.java,代碼如下:

@NodeEntity
public class Student {
    @GraphId
    private Long id;//必須指定Long類型的一個圖形數(shù)據(jù)庫id,圖數(shù)據(jù)庫用于自編號,保證圖數(shù)據(jù)庫中的節(jié)點唯一。
    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包下創(chuàng)建StudentRepositry.java接口,直接繼承spring-data-neo4j的GraphRepository接口,接口內(nèi)容如下:

public interface StudentRepositry extends GraphRepository<Student> {
    @Query("MATCH (n:Student) WHERE n.name = {name} RETURN n")
    Student getStudentByName(@Param("name") String Name);

   //這兩個方法實現(xiàn)的功能是一樣的,上面那個是直接寫Neo4j的Cypher語句來查詢數(shù)據(jù),很強大很好用很靈活,Cypher語句不在本文描述的范圍
    Student findByName( String Name );
}

5、在org.cooze.springboot.neo4j.service包下創(chuàng)建StudentService.java接口,接口代碼如下:

public interface StudentService {
    Student getStudent(String name);
    Student addStudent(Student student);
}

6、在org.cooze.springboot.neo4j.service包下創(chuàng)建impl包和StudentServiceImpl.java實現(xiàn)類,代碼如下:

@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包下創(chuàng)建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個步驟,就已經(jīng)完成了springboot和spring-data-neo4j的整合工作。但是感覺還是了缺點什么,想想,原來是缺了單元測試,下面開始介紹基于以上環(huán)境搭建的單元測試環(huán)境。

單元測試環(huán)境搭建

單元測試依賴已經(jīng)在開始的時候添加了,在這里就不重復介紹了。
1、在項目目錄下的src/test/java創(chuàng)建創(chuàng)建包結(jié)構(gòu)org.cooze.springboot.neo4j.test;
2、在org.cooze.springboot.neo4j.test包下創(chuàng)建單元測試配置類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包下創(chuàng)建StudentTest.java單元測試類,代碼如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Configure.class)
public class StudentTest {

    //如果報錯有紅線沒關(guān)系,不影響單元測試,那只是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()方法后,在數(shù)據(jù)庫中查找存在如下圖數(shù)據(jù):

數(shù)據(jù)插入結(jié)果

到此,單元測試環(huán)境也搭好了。

本文項目代碼github地址:

https://github.com/Cooze/sample-project/tree/master/springboot-neo4j-env
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容