SpringBoot整合neo4j使用詳解
一、Spring Data Neo4j
和很多其他的中間件類似,都提供了類似jpa的方式與springboot進(jìn)行集成,比如大家熟悉的springdata-jpa,操作es的jpa,操作mongo的jpa等,而 Neo4j也提供了與springboot整合的jpa方式,即Spring Data Neo4j,接下來就來演示springboot中如何集成和使用Spring Data Neo4j。
二、環(huán)境準(zhǔn)備
提前搭建neo4j服務(wù);
springboot版本,2.3.5;
提前準(zhǔn)備一個springboot的工程;
三、整合步驟
按照下面的步驟進(jìn)行操作
3.1 導(dǎo)入必須的maven依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.15</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${boot-web.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-neo4j</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lomok.version}</version> </dependency> </dependencies>
3.2 添加配置文件
更多的配置參考官網(wǎng),下面給出的是基本的連續(xù)配置。
server.port=8088 spring.data.neo4j.uri= bolt://IP:7687 spring.data.neo4j.username=neo4j spring.data.neo4j.password=neo4j
3.3 自定義節(jié)點(diǎn)與實(shí)體類映射
比如在本次演示案例中,有兩個節(jié)點(diǎn)操作對象,分別為Person和PersonRelation,兩者之間具有一定的關(guān)系,然后通過程序?qū)ζ渫瓿上嚓P(guān)的crud操作。
自定義Person類
@Data @Builder @NodeEntity("person") public class Person implements Serializable { @Id @GeneratedValue private Long id; @Property("name") private String name; }
PersonRelation類
@Data @NoArgsConstructor @RelationshipEntity(type = "徒弟") public class PersonRelation implements Serializable { @Id @GeneratedValue private Long id; @StartNode private Person parent; @EndNode private Person child; @Property private String relation; public PersonRelation(Person parent, Person child, String relation) { this.parent = parent; this.child = child; this.relation = relation; } }
3.4 自定義jpa
分別自定義兩個操作節(jié)點(diǎn)對象的Repository,繼承Neo4jRepository接口,使用jpa開發(fā)過的同學(xué)對此應(yīng)該不陌生。
PersonRepository
public interface PersonRepository extends Neo4jRepository<Person,Long> { /** * 查詢某個節(jié)點(diǎn)的所有子節(jié)點(diǎn) * @param pId * @return */ @Query("Match (p:person) -[*]->(s:person) where id(p)={0} return s") List<Person> findChildList(Long pId); @Query("Match (p:person {name:{0}}) -[*]->(s:person) return s") List<Person> findChildList(String name); /** * 查詢當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn) * @param name * @return */ @Query("Match (p:person) -[*]->(s:person {name:{0}}) return p") List<Person> findParentList(String name); List<Person> findByName(String name); }
PersonRelationRepository
public interface PersonRelationRepository extends Neo4jRepository<PersonRelation,Long> { }
四、整合測試
下面編寫單元測試對上面的代碼進(jìn)行效果測試
4.1 保存Person以及關(guān)系數(shù)據(jù)
import com.congge.entity.Person; import com.congge.entity.PersonRelation; import com.congge.repository.PersonRelationRepository; import com.congge.repository.PersonRepository; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; @SpringBootTest @RunWith(SpringRunner.class) public class PersonTest { @Autowired private PersonRepository personRepository; @Autowired private PersonRelationRepository personRelationRepository; @Test public void testSave() { Person person = Person.builder().name("唐僧").build(); Person person2 = Person.builder().name("孫悟空").build(); Person person3 = Person.builder().name("豬八戒").build(); Person person4 = Person.builder().name("沙僧").build(); Person person5 = Person.builder().name("白龍馬").build(); List<Person> personList = new ArrayList<>(Arrays.asList( person, person2, person3, person4, person5)); personRepository.saveAll(personList); System.out.println("person 數(shù)據(jù)保存成功"); PersonRelation personRelation = new PersonRelation(person, person2, "徒弟"); PersonRelation personRelation2 = new PersonRelation(person, person3, "徒弟"); PersonRelation personRelation3 = new PersonRelation(person, person4, "徒弟"); PersonRelation personRelation4 = new PersonRelation(person, person5, "徒弟"); List<PersonRelation> personRelationList = new ArrayList<>(Arrays.asList( personRelation, personRelation2, personRelation3, personRelation4 )); // 保存關(guān)系數(shù)據(jù) personRelationRepository.saveAll(personRelationList); System.out.println("person 關(guān)系數(shù)據(jù)保存成功"); } }
運(yùn)行上面的代碼
執(zhí)行成功后,可以去web界面上檢查剛剛保存的數(shù)據(jù)
4.2 查詢數(shù)據(jù)
@Test public void testDelete(){ // 刪除所有person節(jié)點(diǎn) personRepository.deleteAll(); // 刪除所有personRelation關(guān)系數(shù)據(jù) personRelationRepository.deleteAll(); //根據(jù)id刪除 personRepository.deleteById(0l); } /** * 查詢所有 */ @Test public void testFindAll() { Iterable<Person> allPerson = personRepository.findAll(); allPerson.forEach(item -> { System.out.println(item.getId()); System.out.println(item.getName()); System.out.println(); }); } /** * 根據(jù)id查詢 */ @Test public void testFindById() { Optional<Person> personOptional = personRepository.findById(0l); if (personOptional.isPresent()) { System.out.println(personOptional.get().getName()); } } /** * 分頁查詢 */ @Test public void testPage() { //設(shè)置分頁、排序條件,page從0開始 PageRequest pageRequest = PageRequest.of(1, 2, Sort.by(Sort.Order.desc("id"))); Page<Person> page = personRepository.findAll(pageRequest); page.getContent().forEach(person -> { System.out.println(person.getId() + ":" + person.getName()); }); } @Test public void testFindByName() { List<Person> personList = personRepository.findByName("唐僧"); for(Person p : personList){ System.out.println(p.getName()); } }
如果jpa中常用的方法還不能滿足要求的話,可以嘗試自定義編寫語句進(jìn)行實(shí)現(xiàn)。
4.3 JPA自定義方法規(guī)則
使用jpa中的規(guī)則,進(jìn)行自定義查詢,下面總結(jié)了一些常用的jpa使用規(guī)則,可以利用這些API完成一些更高級的業(yè)務(wù)場景開發(fā)
Keyword | Sample | Cypher snippet |
---|---|---|
After | findByLaunchDateAfter(Date date) | n.launchDate > date |
Before | findByLaunchDateBefore(Date date) | n.launchDate < date |
Containing (String) | findByNameContaining(String namePart) | n.name CONTAINS namePart |
Containing (Collection) | findByEmailAddressesContains(Collection addresses) findByEmailAddressesContains(String address) | ANY(collectionFields IN [addresses] WHERE collectionFields in n.emailAddresses) ANY(collectionFields IN address WHERE collectionFields in n.emailAddresses) |
In | findByNameIn(Iterable names) | n.name IN names |
Between | findByScoreBetween(double min, double max) findByScoreBetween(Range range) | n.score >= min AND n.score <= max Depending on the Range definition n.score >= min AND n.score <= max or n.score > min AND n.score < max |
StartingWith | findByNameStartingWith(String nameStart) | n.name STARTS WITH nameStart |
EndingWith | findByNameEndingWith(String nameEnd) | n.name ENDS WITH nameEnd |
Exists | findByNameExists() | EXISTS(n.name) |
True | findByActivatedIsTrue() | n.activated = true |
False | findByActivatedIsFalse() | NOT(n.activated = true) |
Is | findByNameIs(String name) | n.name = name |
NotNull | findByNameNotNull() | NOT(n.name IS NULL) |
Null | findByNameNull() | n.name IS NULL |
GreaterThan | findByScoreGreaterThan(double score) | n.score > score |
GreaterThanEqual | findByScoreGreaterThanEqual(double score) | n.score >= score |
LessThan | findByScoreLessThan(double score) | n.score < score |
LessThanEqual | findByScoreLessThanEqual(double score) | n.score <= score |
Like | findByNameLike(String name) | n.name =~ name |
NotLike | findByNameNotLike(String name) | NOT(n.name =~ name) |
Near | findByLocationNear(Distance distance, Point point) | distance( point(n),point({latitude:lat, longitude:lon}) ) < distance |
Regex | findByNameRegex(String regex) | n.name =~ regex |
And | findByNameAndDescription(String name, String description) | n.name = name AND n.description = description |
Or | findByNameOrDescription(String name, String description) | n.name = name OR n.description = description (Cannot be used to OR nested properties) |
五、寫在文末
本文詳細(xì)總結(jié)了如何在springboot中集成與使用neo4j,并通過代碼演示了如何使用,更多的用法有興趣的同學(xué)還可以深入研究。
以上就是SpringBoot整合neo4j使用詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合neo4j的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring純注解配置實(shí)現(xiàn)代碼示例解析
這篇文章主要介紹了Spring純注解配置實(shí)現(xiàn)代碼示例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08親身體驗(yàn)Intellij?Idea從卡頓到順暢全過程
這篇文章主要介紹了親身體驗(yàn)Intellij?Idea從卡頓到順暢全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09Mybatis-Plus自動生成代碼的實(shí)現(xiàn)示例
在工作中,程序員很多時候都是在寫類似的代碼,可以使用自動生成代碼,本文主要介紹了Mybatis-Plus自動生成代碼的實(shí)現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2023-11-11@RequiredArgsConstructor如何實(shí)現(xiàn)構(gòu)造器注入
這篇文章主要介紹了@RequiredArgsConstructor如何實(shí)現(xiàn)構(gòu)造器注入問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09Java把數(shù)字格式化為貨幣字符串實(shí)例代碼
這篇文章主要介紹了Java把數(shù)字格式化為貨幣字符串實(shí)例代碼,需要的朋友可以參考下2014-02-02