首先创建一个springboot项目,这里不再介绍。
1 导入依赖包
连接neo4j数据库的依赖包
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId>
<version>2.1.5</version>
</dependency>
spring-boot-starter-data-neo4j依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
mybatis-plus依赖包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
全部的依赖包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
</dependencies>
2 编写application.yml文件
server:
port: 1111
spring:
neo4j:
uri: bolt://localhost:7687
authentication:
username: neo4j
password: 12345678
data:
neo4j:
database: neo4j
3 编写实体类文件
创建Author.java实体类
package com.yz.geokg.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import java.util.List;
/**
* @author Administrator
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@NodeEntity(label = "Author")
public class Author {
@Id
@GeneratedValue
private Long id;
private String name;
// 这个不是必须的,根据自己的需要添加这个,这个是一个neo4j数据库中的关系
@Relationship(type="is_last_known_in", direction = Relationship.OUTGOING)
private List<Affiliation> affiliationList;
}
4 编写controller
创建AuthorController.java文件
package com.yz.geokg.controller;
import com.yz.geokg.entity.Author;
import com.yz.geokg.entity.Result;
import com.yz.geokg.service.AuthorService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author Administrator
*/
@RestController
@RequestMapping("author")
public class AuthorController {
@Autowired
private AuthorService authorService;
@ApiOperation(value = "根据关键词查询作者")
@GetMapping("/{keyword}")
public List<Author> queryAuthorByKeyword(@PathVariable String keyword){
return authorService.queryAuthorByKeyword(keyword);
}
}
5 编写Service文件
创建AuthorService.java接口文件
package com.yz.geokg.service;
import com.yz.geokg.entity.Author;
import java.util.List;
/**
* @author Administrator
*/
public interface AuthorService {
/**
* 根据关键词查询作者
* @param word 关键词(文章主题)
* @return 作者列表
*/
List<Author> queryAuthorByKeyword(String word);
}
创建AuthorServiceImpl.java接口实现文件
package com.yz.geokg.service.impl;
import com.yz.geokg.entity.Affiliation;
import com.yz.geokg.entity.Author;
import com.yz.geokg.mapper.AffiliationMapper;
import com.yz.geokg.mapper.AuthorMapper;
import com.yz.geokg.service.AuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AuthorServiceImpl implements AuthorService {
@Autowired
private AuthorMapper authorMapper;
@Autowired
private AffiliationMapper affiliationMapper;
@Override
public List<Author> queryAuthorByKeyword(String word) {
List<Author> authorList = authorMapper.queryAuthorByKeyword(word);
for(Author author : authorList){
List<Affiliation> affiliations = affiliationMapper.queryAffiliationByAuthor(author.getName());
author.setAffiliationList(affiliations);
}
return authorList;
}
}
6 编写mapper文件
创建AuthorMapper文件
package com.yz.geokg.mapper;
import com.yz.geokg.entity.Author;
import com.yz.geokg.entity.Illustration;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author Administrator
*/
@Mapper
@Repository
public interface AuthorMapper extends Neo4jRepository<Author, Long> {
/**
* 根据文献标题查询作者
* @param title 文献标题
* @return 作者列表
*/
@Query("MATCH p=(n:Paper{title:$title})-[r:is_written_by]->(a:Author) RETURN a")
List<Author> queryAuthorByTitle(@Param("title")String title);
@Query("MATCH (a:Author)<-[r1:is_written_by]-(p:Paper)-[r2:has_keyword]->(k:KeyWord{word:$word}) RETURN DISTINCT a")
List<Author> queryAuthorByKeyword(@Param("word")String word);
}
7 测试
在test文件夹下编写测试类
@Test
void getImage(){
List<Author> authorList = authorMapper.queryAuthorByTitle("中国海、陆相页岩层系岩相组合多样性与非常规油气勘探意义");
System.out.println("=====查询结果=====");
for(Author author : authorList){
System.out.println(author);
}
}
输出结果

如有疑问可留言或评论