首先创建一个springboot项目,这里不再介绍。
连接neo4j数据库的依赖包
org.neo4j neo4j-ogm-http-driver 2.1.5
spring-boot-starter-data-neo4j依赖包
org.springframework.boot spring-boot-starter-data-neo4j
mybatis-plus依赖包
com.baomidou mybatis-plus-boot-starter 3.2.0
全部的依赖包
org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.projectlombok lombok 1.18.20 org.neo4j neo4j-ogm-http-driver 2.1.5 com.baomidou mybatis-plus-boot-starter 3.2.0 org.springframework.boot spring-boot-starter-data-neo4j
server:port: 1111
spring:neo4j:uri: bolt://localhost:7687authentication:username: neo4jpassword: 12345678data:neo4j:database: neo4j
创建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@GeneratedValueprivate Long id;private String name;// 这个不是必须的,根据自己的需要添加这个,这个是一个neo4j数据库中的关系@Relationship(type="is_last_known_in", direction = Relationship.OUTGOING)private List affiliationList;
}
创建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 {@Autowiredprivate AuthorService authorService;@ApiOperation(value = "根据关键词查询作者")@GetMapping("/{keyword}")public List queryAuthorByKeyword(@PathVariable String keyword){return authorService.queryAuthorByKeyword(keyword);}
}
创建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 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 {@Autowiredprivate AuthorMapper authorMapper;@Autowiredprivate AffiliationMapper affiliationMapper;@Overridepublic List queryAuthorByKeyword(String word) {List authorList = authorMapper.queryAuthorByKeyword(word);for(Author author : authorList){List affiliations = affiliationMapper.queryAffiliationByAuthor(author.getName());author.setAffiliationList(affiliations);}return authorList;}
}
创建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 {/*** 根据文献标题查询作者* @param title 文献标题* @return 作者列表*/@Query("MATCH p=(n:Paper{title:$title})-[r:is_written_by]->(a:Author) RETURN a")List 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 queryAuthorByKeyword(@Param("word")String word);
}
在test文件夹下编写测试类
@Testvoid getImage(){List authorList = authorMapper.queryAuthorByTitle("中国海、陆相页岩层系岩相组合多样性与非常规油气勘探意义");System.out.println("=====查询结果=====");for(Author author : authorList){System.out.println(author);}}
输出结果
如有疑问可留言或评论
下一篇:Docker(二)