spring boot neo4j 整合配置(四)
整合我採用了目前比較流行的框架 Spring boot,方便簡單快捷。
Spring boot採用了目前最新的1.4.2的版本。搭建步驟如下:
首先pom.xml配置:
設定打包型別:首選打包為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預設使用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.開始還以為需要自己去建一個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配置完成後 建一個包 在包裡建立一個啟動類:
@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 作用在於使用將程式達成war包在 tomcat啟動應用,實現 CommandLineRunner 作用在於服務啟動後 需要做一些初始化的事情。
@SpringBootApplication 該註解可以替代 @Configuration,@EnableAutoConfiguration,@ComponentScan三個註解 簡化配置。
** 需要注意的是:
ServerApplication類一定要放包在最外層(最外層的意思是放在其他有需要注入的類的包的外層),因為spring boot預設的掃描註解是平級開始從上而下的方式,如果放在裡面,會導致其他類不能正常注入。**
resources目錄下建立預設的配置檔案application.properties 新增屬性
server.port=8080 tomcat啟動後的埠號
spring.data.neo4j.username=neo4j 資料庫賬號
spring.data.neo4j.password=neo4j 資料庫密碼
spring.data.neo4j.uri=http://localhost:7474 資料庫uri地址
資料庫安裝檔案直接到官網下載安裝即可
官方下載地址:https://neo4j.com/
該預設檔案的其他更多配置 可引數該博主的文章:http://blog.csdn.net/yuchao2015/article/details/52588407
寫好配置檔案後 基礎配置就算差不多完成了,下面編寫測試(註釋:我所建的測試類 全部都在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類,實現增刪查改,實現自己兩個介面,第一個介面通過名字查詢任務,spring data neo4j 支援方法命名約定查詢 findBy{task的屬性名},類似於jpa的方式,findBy後面的屬性名一定要domain類裡存在,否則會報錯。
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的時候 不會返回id和空值屬性,如果需要返回完整資訊,需要自己建一個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() + "/測試任務", Task.class);
System.out.println(gson.toJson(test.getBody()));
}
@Test
public void saveTask() {
Task task = new Task();
task.setTaskName("測試任務");
ResponseEntity<Task> test = this.restTemplate.postForEntity(
this.base.toString() + "/task",task, Task.class);
System.out.println(gson.toJson(test.getBody()));
}
}
一切準備就緒 開始測試吧! 先執行saveTask 增加一個task節點,然後檢視資料庫
資料已存在於資料庫,大功告成。
相關文章
- Spring boot學習(四)Spring boot整合DruidSpring BootUI
- Spring Boot (四)模板引擎Thymeleaf整合Spring Boot
- 使用Spring Boot整合Apollo配置中心Spring Boot
- 怎樣使用Spring Boot 整合配置 HTTPSSpring BootHTTP
- Spring security(四)-spring boot +spring security簡訊認證+redis整合Spring BootRedis
- Spring Boot 2.0(八):Spring Boot 整合 MemcachedSpring Boot
- Spring Boot(十八):使用 Spring Boot 整合 FastDFSSpring BootAST
- Spring Boot整合rabbitmqSpring BootMQ
- Spring Boot 整合 rabbitmqSpring BootMQ
- Spring Boot 整合 elasticsearchSpring BootElasticsearch
- Spring Boot 整合 dockerSpring BootDocker
- Spring Boot 整合 elkSpring Boot
- Spring Boot 整合 ApolloSpring Boot
- Spring Boot整合RedisSpring BootRedis
- Spring Boot 整合redisSpring BootRedis
- Spring Boot 整合 MyBatisSpring BootMyBatis
- Spring Boot整合SocketSpring Boot
- spring boot整合jooqSpring Boot
- Spring Boot 整合 KafkaSpring BootKafka
- spring boot 整合mybatisSpring BootMyBatis
- Spring Boot整合Spring BatchSpring BootBAT
- Spring Boot整合Spring SecuritySpring Boot
- Spring Boot整合Spring AopSpring Boot
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- Spring boot學習(三) Spring boot整合mybatisSpring BootMyBatis
- Spring Boot實戰系列(7)整合Consul配置中心Spring Boot
- 使用Spring Boot實現資料庫整合配置案例Spring Boot資料庫
- Spring Boot + Mybatis + Spring MVC環境配置(四):MVC框架搭建Spring BootMyBatisMVC框架
- Spring Boot:整合Spring Data JPASpring Boot
- spring-boot+spring-session整合SpringbootSession
- Spring Boot & 配置Spring Boot
- Spring Boot 整合 Apache DubboSpring BootApache
- Spring Boot Actuator 整合 PrometheusSpring BootPrometheus
- spring boot整合HadoopSpring BootHadoop
- spring boot整合shiroSpring Boot
- ElasticSearch與Spring Boot整合ElasticsearchSpring Boot
- spring boot(三)整合 redisSpring BootRedis
- spring-boot整合kindeditorSpringboot