首页
/ NgBatis 使用教程

NgBatis 使用教程

2024-08-15 09:13:29作者:廉皓灿Ida
ngbatis
NGBATIS is a database ORM framework base NebulaGraph + spring-boot, which takes advantage of the mybatis’ fashion development, including some de-factor operations in single table and vertex-edge, like mybatis-plus. NGBATIS 是一款针对 NebulaGraph + Springboot 的数据库 ORM 框架。借鉴于 MyBatis 的使用习惯进行开发。https://graph-cn.github.io/ngbatis-docs/

项目介绍

NgBatis 是一款针对 NebulaGraph + Springboot 的数据库 ORM 框架,借鉴于 MyBatis 的使用习惯进行开发。它包含了一些类似于 mybatis-plus 的单表操作,另外还有一些图特有的实体-关系基本操作。NgBatis 旨在简化 NebulaGraph 数据库与 Springboot 应用的集成,使得开发者能够更高效地进行图数据库的开发工作。

项目快速启动

环境准备

  • Java 8 或更高版本
  • Maven 3.6 或更高版本
  • NebulaGraph 数据库
  • Springboot 2.x

快速启动步骤

  1. 克隆项目

    git clone https://github.com/nebula-contrib/ngbatis.git
    cd ngbatis
    
  2. 添加依赖

    在您的 Springboot 项目的 pom.xml 文件中添加以下依赖:

    <dependency>
        <groupId>io.github.nebula-contrib</groupId>
        <artifactId>ngbatis</artifactId>
        <version>1.2.2</version>
    </dependency>
    
  3. 配置数据库连接

    application.properties 文件中添加 NebulaGraph 数据库的连接配置:

    nebula.graph.address=127.0.0.1:9669
    nebula.graph.username=root
    nebula.graph.password=nebula
    
  4. 创建实体类和 Mapper

    创建一个简单的实体类:

    public class Person {
        private String id;
        private String name;
        // 省略 getter 和 setter 方法
    }
    

    创建一个 Mapper 接口:

    @Mapper
    public interface PersonMapper {
        @Select("MATCH (p:Person) RETURN p")
        List<Person> selectAll();
    }
    
  5. 使用 Mapper

    在您的服务类中注入并使用 Mapper:

    @Service
    public class PersonService {
        @Autowired
        private PersonMapper personMapper;
    
        public List<Person> getAllPersons() {
            return personMapper.selectAll();
        }
    }
    

应用案例和最佳实践

应用案例

NgBatis 可以用于各种图数据库应用场景,例如社交网络分析、推荐系统、知识图谱等。以下是一个简单的社交网络分析案例:

  1. 定义社交网络实体和关系

    public class User {
        private String id;
        private String name;
        // 省略 getter 和 setter 方法
    }
    
    public class Follow {
        private String from;
        private String to;
        // 省略 getter 和 setter 方法
    }
    
  2. 定义 Mapper

    @Mapper
    public interface UserMapper {
        @Select("MATCH (u:User) RETURN u")
        List<User> selectAllUsers();
    
        @Select("MATCH (u:User)-[:FOLLOWS]->(f:User) WHERE id(u) = #{userId} RETURN f")
        List<User> selectFollowers(String userId);
    }
    
  3. 使用 Mapper

    @Service
    public class SocialNetworkService {
        @Autowired
        private UserMapper userMapper;
    
        public List<User> getAllUsers() {
            return userMapper.selectAllUsers();
        }
    
        public List<User> getFollowers(String userId) {
            return userMapper.selectFollowers(userId);
        }
    }
    

最佳实践

  • 合理设计实体和关系:确保实体和关系的设计符合业务需求,避免过度设计。
  • 使用缓存:对于频繁查询的数据,可以考虑使用缓存机制提高性能。
  • 优化查询语句:合理编写 NebulaGraph 查询语句,避免全表扫描,提高查询效率。

典型生态项目

NgBatis 作为 NebulaGraph 开源生态项目之一,与其他 NebulaGraph 相关项目协同工作,共同构建

ngbatis
NGBATIS is a database ORM framework base NebulaGraph + spring-boot, which takes advantage of the mybatis’ fashion development, including some de-factor operations in single table and vertex-edge, like mybatis-plus. NGBATIS 是一款针对 NebulaGraph + Springboot 的数据库 ORM 框架。借鉴于 MyBatis 的使用习惯进行开发。https://graph-cn.github.io/ngbatis-docs/
热门项目推荐
相关项目推荐

热门内容推荐

展开

最新内容推荐

展开

项目优选

收起
CangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
669
0
RuoYi-Vue
🎉 基于SpringBoot,Spring Security,JWT,Vue & Element 的前后端分离权限管理系统,同时提供了 Vue3 的版本
Java
136
18
openHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
12
7
redis-sdk
仓颉语言实现的Redis客户端SDK。已适配仓颉0.53.4 Beta版本。接口设计兼容jedis接口语义,支持RESP2和RESP3协议,支持发布订阅模式,支持哨兵模式和集群模式。
Cangjie
322
26
advanced-java
Advanced-Java是一个Java进阶教程,适合用于学习Java高级特性和编程技巧。特点:内容深入、实例丰富、适合进阶学习。
JavaScript
75.83 K
19.04 K
qwerty-learner
为键盘工作者设计的单词记忆与英语肌肉记忆锻炼软件 / Words learning and English muscle memory training software designed for keyboard workers
TSX
15.56 K
1.44 K
Jpom
🚀简而轻的低侵入式在线构建、自动部署、日常运维、项目监控软件
Java
1.41 K
292
Yi-Coder
Yi Coder 编程模型,小而强大的编程助手
HTML
30
5
easy-es
Elasticsearch 国内Top1 elasticsearch搜索引擎框架es ORM框架,索引全自动智能托管,如丝般顺滑,与Mybatis-plus一致的API,屏蔽语言差异,开发者只需要会MySQL语法即可完成对Es的相关操作,零额外学习成本.底层采用RestHighLevelClient,兼具低码,易用,易拓展等特性,支持es独有的高亮,权重,分词,Geo,嵌套,父子类型等功能...
Java
1.42 K
231
taro
开放式跨端跨框架解决方案,支持使用 React/Vue/Nerv 等框架来开发微信/京东/百度/支付宝/字节跳动/ QQ 小程序/H5/React Native 等应用。 https://taro.zone/
TypeScript
35.34 K
4.77 K