国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring Boot整合neo4j

這篇具有很好參考價值的文章主要介紹了Spring Boot整合neo4j。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Neo4 j概覽

springboot整合neo4j框架,neo4j,spring boot

相關(guān)版本信息

jdk 1.8

neo4j-enterprise-3.5.35

idea 2021

maven 3.6.3

SpringBoot整合neo4j實操

1、配置文件

Pom文件中引入依賴 Spring生態(tài)中Spring-data部分不僅僅提供了Spring-data-jpa , 也提供了Spring-data-neo4j 支持spring和 neo4j的完美融合,pom.xml 文件中依賴

        <dependency>
 ? ? ? ? ?  <groupId>org.springframework.boot</groupId>
 ? ? ? ? ?  <artifactId>spring-boot-starter-data-neo4j</artifactId>
 ? ? ?  </dependency>

yml文件中配置連接屬性

spring:
  data:
 ?  neo4j:
 ? ?  uri: bolt://localhost:7687
 ? ?  username: neo4j
 ? ?  password: 123
#打印neo4j生成的Cypher
logging:
  level:
 ?  org:
 ? ?  neo4j:
 ? ? ?  ogm:
 ? ? ? ?  drivers:
 ? ? ? ? ?  bolt:
 ? ? ? ? ? ?  request:
 ? ? ? ? ? ? ?  BoltRequest: DEBUG
?

2、實體類(NodeEntity)

/**
*部門節(jié)點實體類
*/
@NodeEntity(label = "Dept")
@Data
@Builder
public class Dept {
?
 ?  @Id
 ?  @GeneratedValue
 ?  private Long id;
?
 ?  @Property(name = "deptName")
 ?  private String deptName;
?
}
/**
*關(guān)系 實體類
*/
@RelationshipEntity(type = "relationShip")
@Data
@Builder
public class RelationShip {
?
 ?  @Id
 ?  @GeneratedValue
 ?  private Long id;
?
 ?  @StartNode
 ?  private Dept parent;
?
 ?  @EndNode
 ?  private Dept child;
}   

@NodeEntity: 標(biāo)明是一個節(jié)點實體@RelationshipEntity:標(biāo)明是一個關(guān)系實體@Id:實體主鍵@Property:實體屬性@GeneratedValue:實體屬性值自增@StartNode:開始節(jié)點(可以理解為父節(jié)點)@EndNode:結(jié)束節(jié)點(可以理解為子節(jié)點)、

3、創(chuàng)建repositories類(類似于封裝過的dao)

@Repository
public interface DeptRepository extends Neo4jRepository<Dept,Long> {
?
 ?  @Query("MATCH (d:dept) WHERE d.deptName CONTAINS $title RETURN d")
 ?  List<Dept> findByTitle(@Param("title") String title);
}

@Query:編寫neo4j 的Cypher語句, 變量格式為$

源碼解析:Neo4jRepository.class

@NoRepositoryBean
public interface Neo4jRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
 ?  <S extends T> S save(S var1, int var2);
?
 ?  <S extends T> Iterable<S> save(Iterable<S> var1, int var2);
?
 ?  Optional<T> findById(ID var1, int var2);
?
 ?  Iterable<T> findAll();
?
 ?  Iterable<T> findAll(int var1);
?
 ?  Iterable<T> findAll(Sort var1);
?
 ?  Iterable<T> findAll(Sort var1, int var2);
?
 ?  Iterable<T> findAllById(Iterable<ID> var1);
?
 ?  Iterable<T> findAllById(Iterable<ID> var1, int var2);
?
 ?  Iterable<T> findAllById(Iterable<ID> var1, Sort var2);
?
 ?  Iterable<T> findAllById(Iterable<ID> var1, Sort var2, int var3);
?
 ?  Page<T> findAll(Pageable var1);
?
 ?  Page<T> findAll(Pageable var1, int var2);
}

Neo4jRepository已經(jīng)經(jīng)過多層封裝,包含了實現(xiàn)了crud基本功能外,再集成了分頁功能,之后提煉了常用查詢的方法,提高了代碼復(fù)用性。

4、創(chuàng)建Neo4jConfig配置

package io.fredia.femicro.graph.config;
?
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
?
@Configuration
@EnableNeo4jRepositories(basePackages = "com.example.demo.repository")
@EnableTransactionManagement // 激活隱式事務(wù)
public class Neo4jConfig {
}

注解解析:@Configuration: springboot聲明配置類,一般為單例模式@EnableNeo4jRepositories: Neo4j掃描Repositories所在包,可以理解為mybatis掃描mapper包@EnableTransactionManagement: Neo4j完整的支持ACID,所以此處開啟事務(wù)功能。

5、編寫Service類

@Service
public class DeptService {
?
 ?  @Autowired
 ?  private DeptRepository deptRepository;
?
 ?  public Iterable<Dept> saveAll(List<Dept> depts) {
 ? ? ?  return deptRepository.saveAll(depts);
 ?  }
?
 ?  public void deleteById(Long id) {
 ? ? ?  deptRepository.deleteById(id);
 ?  }
?
 ?  public void deleteAll() {
 ? ? ?  deptRepository.deleteAll();
 ?  }
?
 ?  public List<Dept> findByTitle(String title) {
 ? ? ?  return deptRepository.findByTitle(title);
 ?  }
}

6、編寫Controller類

@RestController
public class TestController {
?
 ?  @Autowired
 ?  private DeptService deptService;
?
 ?  @Autowired
 ?  private RelationShipService relationShipService;
?
 ?  /**
 ? ? * CEO
 ? ? * ?  -設(shè)計部
 ? ? * ? ? ?  - 設(shè)計1組
 ? ? * ? ? ?  - 設(shè)計2組
 ? ? * ?  -技術(shù)部
 ? ? * ? ? ?  - 前端技術(shù)部
 ? ? * ? ? ?  - 后端技術(shù)部
 ? ? * ? ? ?  - 測試技術(shù)部
 ? ? */
 ?  @GetMapping("/create")
 ?  public void create(){
 ? ? ?  Dept CEO = Dept.builder().deptName("CEO").build();
 ? ? ?  Dept dept1 = Dept.builder().deptName("設(shè)計部").build();
 ? ? ?  Dept dept11 = Dept.builder().deptName("設(shè)計1組").build();
 ? ? ?  Dept dept12 = Dept.builder().deptName("設(shè)計2組").build();
?
 ? ? ?  Dept dept2 = Dept.builder().deptName("技術(shù)部").build();
 ? ? ?  Dept dept21 = Dept.builder().deptName("前端技術(shù)部").build();
 ? ? ?  Dept dept22 = Dept.builder().deptName("后端技術(shù)部").build();
 ? ? ?  Dept dept23 = Dept.builder().deptName("測試技術(shù)部").build();
 ? ? ?  List<Dept> depts = new ArrayList<>(Arrays.asList(CEO,dept1,dept11,dept12,dept2,dept21,dept22,dept23));
 ? ? ?  deptService.saveAll(depts);
?
 ? ? ?  RelationShip relationShip1 = RelationShip.builder().parent(CEO).child(dept1).build();
 ? ? ?  RelationShip relationShip2 = RelationShip.builder().parent(CEO).child(dept2).build();
 ? ? ?  RelationShip relationShip3 = RelationShip.builder().parent(dept1).child(dept11).build();
 ? ? ?  RelationShip relationShip4 = RelationShip.builder().parent(dept1).child(dept12).build();
 ? ? ?  RelationShip relationShip5 = RelationShip.builder().parent(dept2).child(dept21).build();
 ? ? ?  RelationShip relationShip6 = RelationShip.builder().parent(dept2).child(dept22).build();
 ? ? ?  RelationShip relationShip7 = RelationShip.builder().parent(dept2).child(dept23).build();
 ? ? ?  List<RelationShip> relationShips = new ArrayList<>(Arrays.asList(relationShip1,relationShip2,relationShip3,relationShip4,relationShip5
 ? ? ? ? ? ? ?  ,relationShip6,relationShip7));
 ? ? ?  relationShipService.saveAll(relationShips);
 ?  }
?
 ?  @GetMapping("getById")
 ?  public RelationShip getById(Long id){
 ? ? ?  Optional<RelationShip> byId = relationShipService.findById(id);
 ? ? ?  return byId.orElse(null);
 ?  }
?
 ?  @GetMapping("deleteRelationShip")
 ?  public void deleteRelationShip(Long id){
 ? ? ?  relationShipService.deleteById(id);
 ?  }
?
 ?  @GetMapping("deleteDept")
 ?  public void deleteDept(Long id){
 ? ? ?  deptService.deleteById(id);
 ?  }
?
 ?  @GetMapping("deleteAll")
 ?  public void deleteAll(){
 ? ? ?  deptService.deleteAll();
 ? ? ?  relationShipService.deleteAll();
 ?  }
 ?  @GetMapping("getByTitle")
 ?  public List<Dept> getByTitle(@RequestParam("title") String title){
 ? ? ?  return deptService.findByTitle(title);
 ?  }
}

測試結(jié)果

啟動項目 訪問本地地址:實現(xiàn)新增功能

http://localhost:8088/create

登錄本機的Neo4j :localhost:7474

查看Neo4j 數(shù)據(jù)庫內(nèi)容: springboot整合neo4j框架,neo4j,spring boot

訪問本地地址:通過Cypher語句實現(xiàn)查詢功能

http://localhost:8088/getByTitle?title=技術(shù)

控制臺打印日志: springboot整合neo4j框架,neo4j,spring boot

查看Neo4j 數(shù)據(jù)庫內(nèi)容: springboot整合neo4j框架,neo4j,spring boot文章來源地址http://www.zghlxwxcb.cn/news/detail-693532.html

到了這里,關(guān)于Spring Boot整合neo4j的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • springboot整合neo4j-使用原生cypher

    該文的實現(xiàn)有更簡單的方式,詳見我的另一篇博客springboot整合neo4j–采用Neo4jClient和Neo4jTemplate方式 Neo4j 提供 JAVA API 以編程方式執(zhí)行所有數(shù)據(jù)庫操作。它支持三種類型的API: 1、Neo4j 原生的 Java API 原生 Java API 是一種低級別的純 JAVA API,用于執(zhí)行數(shù)據(jù)庫操作。 2、Neo4j Cypher Jav

    2024年02月12日
    瀏覽(24)
  • Springboot整合Neo4J圖數(shù)據(jù)庫

    1.引入依賴 JDK11, neo4J4.4.23 2.解決springboot與neo4j兼容問題

    2024年02月09日
    瀏覽(27)
  • 【微服務(wù)】springboot整合neo4j使用詳解

    在上一篇我們詳細了解了neo4j的使用,從搭建到相關(guān)的語法操作,本篇緊接著之前的內(nèi)容,來詳細聊聊如何在springboot應(yīng)用中集成和使用neo4j。 和很多其他的中間件類似,都提供了類似jpa的方式與springboot進行集成,比如大家熟悉的springdata-jpa,操作es的jpa,操作mongo的jpa等,而

    2024年02月08日
    瀏覽(22)
  • springboot整合neo4j-使用原生cypher Java API

    該文的實現(xiàn)有更簡單的方式,詳見我的另一篇博客springboot整合neo4j–采用Neo4jClient和Neo4jTemplate方式 Neo4j 提供 JAVA API 以編程方式執(zhí)行所有數(shù)據(jù)庫操作。它支持三種類型的API: 1、Neo4j 原生的 Java API 原生 Java API 是一種低級別的純 JAVA API,用于執(zhí)行數(shù)據(jù)庫操作。 2、Neo4j Cypher Jav

    2024年02月09日
    瀏覽(23)
  • 圖數(shù)據(jù)庫_Neo4j和SpringBoot整合使用_實戰(zhàn)創(chuàng)建明星關(guān)系圖譜---Neo4j圖數(shù)據(jù)庫工作筆記0010

    2023-09-10 10:37:48 補充 注意:下面是舊版本的語法,如果你發(fā)現(xiàn)@NodeEntity這樣的注解沒有的話可以這樣: 這里就要用@Node 另外如果@StartNode和@EndNode都沒有了,那么說明是用法變了. 關(guān)于最新的用法,在官網(wǎng)有明確的說明和案例,很有用: 下面給出官網(wǎng)的案例:

    2024年02月12日
    瀏覽(23)
  • 圖數(shù)據(jù)庫_Neo4j和SpringBoot Data整合使用_實戰(zhàn)創(chuàng)建明星關(guān)系圖譜---Neo4j圖數(shù)據(jù)庫工作筆記0010

    2023-09-10 10:37:48 補充 注意:下面是舊版本的語法,如果你發(fā)現(xiàn)@NodeEntity這樣的注解沒有的話可以這樣: 這里就要用@Node 另外如果@StartNode和@EndNode都沒有了,那么說明是用法變了. 關(guān)于最新的用法,在官網(wǎng)有明確的說明和案例,很有用: 下面給出官網(wǎng)的案例:

    2024年02月09日
    瀏覽(47)
  • SpringBoot 整合 Neo4j、MySQL 多數(shù)據(jù)源方案(Druid Mybatis DynamicDatasource)

    本文總結(jié)了Neo4j和Spring/SpringBoot、Alibaba Druid、Dynamic Datasource、Mybatis等整合方案,對相應(yīng)配置做了詳細說明。 添加Neo4j JDBC Driver依賴 添加application.yml配置 添加Neo4j JDBC Driver + Alibaba Druid依賴 添加application.yml配置 添加Neo4j JDBC Driver、Alibaba Druid、Dynamic DataSource依賴 添加application.y

    2023年04月08日
    瀏覽(19)
  • 圖數(shù)據(jù)庫Neo4j學(xué)習(xí)四Spring Data NEO

    圖數(shù)據(jù)庫Neo4j學(xué)習(xí)四Spring Data NEO

    如下所示,這是我們的一個實際的節(jié)點,我們以該節(jié)點為例,創(chuàng)建一個UserNode 如果出現(xiàn)以下錯誤 解決方法 :找到你安裝neo4j的路徑下的conf文件夾,neo4j.conf,找到 將前面的注釋#去掉,然后重啟neo4j,在重啟項目即可。 通過接口創(chuàng)建User節(jié)點,老王 ,然后再neo4j中查詢,看能否

    2024年02月11日
    瀏覽(44)
  • 圖數(shù)據(jù)庫Neo4j學(xué)習(xí)四——Spring Data NEO

    圖數(shù)據(jù)庫Neo4j學(xué)習(xí)四——Spring Data NEO

    如下所示,這是我們的一個實際的節(jié)點,我們以該節(jié)點為例,創(chuàng)建一個UserNode 如果出現(xiàn)以下錯誤 解決方法 :找到你安裝neo4j的路徑下的conf文件夾,neo4j.conf,找到 將前面的注釋#去掉,然后重啟neo4j,在重啟項目即可。 通過接口創(chuàng)建User節(jié)點,老王 ,然后再neo4j中查詢,看能否

    2024年02月13日
    瀏覽(58)
  • 圖數(shù)據(jù)庫_Neo4j和SpringBoot整合使用_創(chuàng)建節(jié)點_刪除節(jié)點_創(chuàng)建關(guān)系_使用CQL操作圖譜---Neo4j圖數(shù)據(jù)庫工作筆記0009

    圖數(shù)據(jù)庫_Neo4j和SpringBoot整合使用_創(chuàng)建節(jié)點_刪除節(jié)點_創(chuàng)建關(guān)系_使用CQL操作圖譜---Neo4j圖數(shù)據(jù)庫工作筆記0009

    首先需要引入依賴 ? springboot提供了一個spring data neo4j來操作 neo4j ? 可以看到它的架構(gòu) ? 這個是下載下來的jar包來看看 有很多cypher對吧 ? 可以看到就是通過封裝的驅(qū)動來操作graph database ? 然后開始弄一下 首先添加依賴

    2024年02月12日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包