Neo4 j概覽
相關(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)容:
訪問本地地址:通過Cypher語句實現(xiàn)查詢功能
http://localhost:8088/getByTitle?title=技術(shù)
控制臺打印日志: 文章來源:http://www.zghlxwxcb.cn/news/detail-693532.html
查看Neo4j 數(shù)據(jù)庫內(nèi)容: 文章來源地址http://www.zghlxwxcb.cn/news/detail-693532.html
到了這里,關(guān)于Spring Boot整合neo4j的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!