?? Neo4j ?? |
?? 算法刷題專欄 | 面試必備算法 | 面試高頻算法 ??
?? 越難的東西,越要努力堅持,因為它具有很高的價值,算法就是這樣?
?? 作者簡介:碩風和煒,CSDN-Java領域優(yōu)質創(chuàng)作者??,保研|國家獎學金|高中學習JAVA|大學完善JAVA開發(fā)技術棧|面試刷題|面經八股文|經驗分享|好用的網站工具分享??????
?? 恭喜你發(fā)現一枚寶藏博主,趕快收入囊中吧??
?? 人生如棋,我愿為卒,行動雖慢,可誰曾見我后退一步?????
?? Neo4j ?? |
?? 知識回顧
大家根據自己情況的情況自行選擇之前的文章進行學習
【Docker安裝部署Neo4j保姆級教程】
【使用Neo4j進行圖數據可視化】
【Neo4j教程之CQL命令基本使用】
【Neo4j教程之CQL函數基本使用】
?? Spring Data Neo4j官方指導手冊
Spring Data Neo4j官方指導手冊
第一步進入Spring官方,選擇SpringData模塊
第二步選擇Spring Data Neo4j
第三步查看Spring Data Neo4j指導手冊相關的內容
?? Docker啟動Neo4j
注意:此處我們直接通過Docker來啟動Neo4j,前面的教程中也有,不會的同學可以先去學習
docker start neo4j
?? Spring Boot集成Neo4J配置信息
?? 新創(chuàng)建一個SpringBoot項目
?? 導入spring-boot-starter-data-neo4j依賴
添加對應的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
?? 配置neo4j相關的連接信息
添加對應的配置文件
spring.data.neo4j.uri= bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=123456
?? Spring Boot集成Neo4J案例實操
?? 創(chuàng)建對應的Person實體信息
import lombok.Data;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
/**
* 創(chuàng)作一個對應 Person 實體對象 -> 對應我們 Neo4j數據庫中的 Node 對象
*/
@Data
@NodeEntity("Person")
public class Person {
@Id
@GeneratedValue
private Long id;
@Property
private String name;
}
?? 創(chuàng)建對應的Repository接口
import entity.Person;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepository extends Neo4jRepository<Person,Long> {
}
?? 創(chuàng)建節(jié)點訪問測試
代碼案例
import dao.PersonRepository;
import entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootNeo4jApplicationTests {
@Autowired
private PersonRepository personRepository;
@Test
void contextLoads() {
Person person = new Person();
person.setName("碩風和煒");
personRepository.save(person);
}
}
訪問測試
?? Spring Boot集成Neo4J案例實操2
?? 創(chuàng)建對應的PersonRelation實體信息
import lombok.Data;
import org.neo4j.ogm.annotation.*;
import java.io.Serializable;
@Data
@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;
}
}
?? Person實體中增加對應的構造方法
import lombok.Data;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
/**
* 創(chuàng)作一個對應 Person 實體對象 -> 對應我們 Neo4j數據庫中的 Node 對象
*/
@Data
@NodeEntity("Person")
public class Person {
@Id
@GeneratedValue
private Long id;
@Property
private String name;
@Property
private Integer age;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
}
?? 創(chuàng)建對應的PersonRelationRepository接口
import com.ljw.springboot.neo4j.entity.PersonRelation;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRelationRepository extends Neo4jRepository<PersonRelation,Long> {
}
?? 創(chuàng)建節(jié)點訪問測試
代碼案例
import com.ljw.springboot.neo4j.dao.PersonRelationRepository;
import com.ljw.springboot.neo4j.dao.PersonRepository;
import com.ljw.springboot.neo4j.entity.Person;
import com.ljw.springboot.neo4j.entity.PersonRelation;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootNeo4jApplicationTests {
@Autowired
private PersonRelationRepository personRelationRepository;
@Test
void nodeRelation(){
Person p1 = new Person("唐僧",4321);
Person p2 = new Person("孫悟空",3421);
Person p3 = new Person("豬八戒",2413);
Person p4 = new Person("沙僧",1234);
PersonRelation pr1 = new PersonRelation(p1,p2,"徒弟");
PersonRelation pr2 = new PersonRelation(p1,p3,"徒弟");
PersonRelation pr3 = new PersonRelation(p1,p4,"徒弟");
personRelationRepository.save(pr1);
personRelationRepository.save(pr2);
personRelationRepository.save(pr3);
}
}
訪問測試
?? 總結
本文展示了如何使用Neo4J和Spring Boot整合,如何在Spring Boot應用程序中使用Neo4J數據庫來持久化數據、使用CQL語言進行高效的查詢操作,并提供了一個非常簡單的例子以演示如何使用Neo4J數據庫。相信通過本文的學習,讀者已經掌握了如何使用Neo4J和Spring Boot整合,并且具備將此應用于實際項目的能力。
?? 共勉
最后,我想和大家分享一句一直激勵我的座右銘,希望可以與大家共勉! |
文章來源:http://www.zghlxwxcb.cn/news/detail-497019.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-497019.html
到了這里,關于【Springboot集成Neo4j完整版教程】的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!