集成我采用了目前比較流行的框架 Spring boot悔醋,方便簡單快捷。
Spring boot采用了目前最新的1.4.2的版本妄帘。搭建步驟如下:
首先pom.xml配置:
設(shè)置打包類型:首選打包為war <packaging>war</packaging> 妆毕。
依賴說明:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
1.spring-boot-starter-parent
包含了大量配置好的依賴管理趾诗,1.4.2為boot的版本號犀呼。
2.Spring默認(rèn)使用jdk1.6幸撕,如果你想使用jdk1.8,你需要在pom.xml
的屬性里面添加java.version外臂。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
1.通過spring-boot-starter-web 支持以 REST 方式暴露接口坐儿,該包集成了spring-mvc、spring-web宋光、spring-boot-starter-tomcat貌矿,對外提供REST API。
2.開始還以為需要自己去建一個(gè)neo4j的配置文件加載配置罪佳,后面一看沒想到他提供了集成neo4j的支持逛漫,方便了不少,只需添加spring-boot-starter-data-neo4j官方支持的neo4j依賴包赘艳。
3.添加單元測試依賴:spring-boot-starter-test酌毡。
完整配置如下:
<?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>kop-instance-data</groupId>
<artifactId>kop-instance-data</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<neo4j.version>3.0.1</neo4j.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>com.voodoodyne.jackson.jsog</groupId>
<artifactId>jackson-jsog</artifactId>
<version>1.1</version>
<scope>compile</scope>
</dependency>
<!-- end::dependencies[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
pom.xml配置完成后 建一個(gè)包 在包里創(chuàng)建一個(gè)啟動(dòng)類:
@SpringBootApplication
public class ServerApplication extends SpringBootServletInitializer implements CommandLineRunner {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ServerApplication.class);
}
public static void main(String[] args) throws IOException {
SpringApplication.run(ServerApplication.class, args);
}
@Override
public void run(String... strings) throws Exception {
System.out.println("初始化。蕾管。枷踏。。娇掏。呕寝。。婴梧。下梢。。塞蹭。孽江。");
}
}
集成 SpringBootServletInitializer 作用在于使用將程序達(dá)成war包在 tomcat啟動(dòng)應(yīng)用,實(shí)現(xiàn) CommandLineRunner 作用在于服務(wù)啟動(dòng)后 需要做一些初始化的事情番电。
@SpringBootApplication 該注解可以替代 @Configuration,@EnableAutoConfiguration,@ComponentScan三個(gè)注解 簡化配置岗屏。
** 需要注意的是:
ServerApplication類一定要放包在最外層(最外層的意思是放在其他有需要注入的類的包的外層),因?yàn)閟pring boot默認(rèn)的掃描注解是平級開始從上而下的方式漱办,如果放在里面这刷,會導(dǎo)致其他類不能正常注入。**
resources目錄下建立默認(rèn)的配置文件application.properties 添加屬性
server.port=8080 tomcat啟動(dòng)后的端口號
spring.data.neo4j.username=neo4j 數(shù)據(jù)庫賬號
spring.data.neo4j.password=neo4j 數(shù)據(jù)庫密碼
spring.data.neo4j.uri=http://localhost:7474 數(shù)據(jù)庫uri地址
數(shù)據(jù)庫安裝文件直接到官網(wǎng)下載安裝即可
官方下載地址:https://neo4j.com/
該默認(rèn)文件的其他更多配置 可參數(shù)該博主的文章:http://blog.csdn.net/yuchao2015/article/details/52588407
寫好配置文件后 基礎(chǔ)配置就算差不多完成了娩井,下面編寫測試(注釋:我所建的測試類 全部都在ServerApplication類所在包的下面):
domain類:
@NodeEntity
public class Task {
@GraphId
private Long id;
private String taskName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
}
repositories類:
@Repository
public interface TaskRepository extends GraphRepository<Task> {
Task findByTaskName(@Param("taskName") String taskName);
@Query("MATCH (t:Task) WHERE t.taskName =~ ('(?i).*'+{taskName}+'.*') RETURN t")
Collection<Task> findByNameContaining(@Param("taskName") String taskName);
}
TaskRepository 繼承GraphRepository類暇屋,實(shí)現(xiàn)增刪查改,實(shí)現(xiàn)自己兩個(gè)接口,第一個(gè)接口通過名字查詢?nèi)蝿?wù)洞辣,spring data neo4j 支持方法命名約定查詢 findBy{task的屬性名}咐刨,類似于jpa的方式昙衅,findBy后面的屬性名一定要domain類里存在,否則會報(bào)錯(cuò)定鸟。
Controller類:
@RestController
@RequestMapping("/task")
public class TaskController {
@Autowired
TaskRepository taskRepository;
@RequestMapping(value = "", method = RequestMethod.POST, consumes = "application/json")
@Transactional
public Task saveTask(@RequestBody Task taskInfo) {
Task task = taskRepository.save(taskInfo);
return task;
}
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public Task create(@PathVariable String name) {
Task task = taskRepository.findByTaskName(name);
return task;
}
}
返回task的時(shí)候 不會返回id和空值屬性而涉,如果需要返回完整信息,需要自己建一個(gè)dto联予,包裝一下再返回 啼县。
單元測試方法:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TaskTest {
@LocalServerPort
private int port;
private URL base;
private Gson gson = new Gson();
@Autowired
private TestRestTemplate restTemplate;
@Before
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/");
}
@Test
public void findByTaskName() {
ResponseEntity<Task> test = this.restTemplate.getForEntity(
this.base.toString() + "/測試任務(wù)", Task.class);
System.out.println(gson.toJson(test.getBody()));
}
@Test
public void saveTask() {
Task task = new Task();
task.setTaskName("測試任務(wù)");
ResponseEntity<Task> test = this.restTemplate.postForEntity(
this.base.toString() + "/task",task, Task.class);
System.out.println(gson.toJson(test.getBody()));
}
}
一切準(zhǔn)備就緒 開始測試吧! 先運(yùn)行saveTask 增加一個(gè)task節(jié)點(diǎn)躯泰,然后查看數(shù)據(jù)庫
數(shù)據(jù)已存在于數(shù)據(jù)庫谭羔,大功告成。