NgBatis 使用教程
2024-08-17 05:52:57作者:廉皓灿Ida
项目介绍
NgBatis 是一款针对 NebulaGraph + Springboot 的数据库 ORM 框架,借鉴于 MyBatis 的使用习惯进行开发。它包含了一些类似于 mybatis-plus 的单表操作,另外还有一些图特有的实体-关系基本操作。NgBatis 旨在简化 NebulaGraph 数据库与 Springboot 应用的集成,使得开发者能够更高效地进行图数据库的开发工作。
项目快速启动
环境准备
- Java 8 或更高版本
- Maven 3.6 或更高版本
- NebulaGraph 数据库
- Springboot 2.x
快速启动步骤
-
克隆项目
git clone https://github.com/nebula-contrib/ngbatis.git cd ngbatis -
添加依赖
在您的 Springboot 项目的
pom.xml文件中添加以下依赖:<dependency> <groupId>io.github.nebula-contrib</groupId> <artifactId>ngbatis</artifactId> <version>1.2.2</version> </dependency> -
配置数据库连接
在
application.properties文件中添加 NebulaGraph 数据库的连接配置:nebula.graph.address=127.0.0.1:9669 nebula.graph.username=root nebula.graph.password=nebula -
创建实体类和 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(); } -
使用 Mapper
在您的服务类中注入并使用 Mapper:
@Service public class PersonService { @Autowired private PersonMapper personMapper; public List<Person> getAllPersons() { return personMapper.selectAll(); } }
应用案例和最佳实践
应用案例
NgBatis 可以用于各种图数据库应用场景,例如社交网络分析、推荐系统、知识图谱等。以下是一个简单的社交网络分析案例:
-
定义社交网络实体和关系
public class User { private String id; private String name; // 省略 getter 和 setter 方法 } public class Follow { private String from; private String to; // 省略 getter 和 setter 方法 } -
定义 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); } -
使用 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 相关项目协同工作,共同构建
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0214
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0138
uni-appA cross-platform framework using Vue.jsJavaScript08
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
SwanLab⚡️SwanLab - an open-source, modern-design AI training tracking and visualization tool. Supports Cloud / Self-hosted use. Integrated with PyTorch / Transformers / LLaMA Factory / veRL/ Swift / Ultralytics / MMEngine / Keras etc.Python00
tiny-universe《大模型白盒子构建指南》:一个全手搓的Tiny-UniverseJupyter Notebook03
项目优选
收起
deepin linux kernel
C
32
16
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
469
465
暂无描述
Dockerfile
778
5.08 K
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
877
2.03 K
Ascend Extension for PyTorch
Python
758
968
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
697
1.4 K
昇腾LLM分布式训练框架
Python
185
231
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.1 K
1.14 K
本仓库是 Flutter SDK 与 Flutter Engine 的 OpenHarmony 适配版本,由 CPF-Flutter 团队维护。开发者可使用熟悉的 Flutter 技术栈开发 OpenHarmony 应用,3.35.7 及以后的适配版本可基于本仓库源码构建支持 OpenHarmony 的 Flutter Engine。
Dart
1.04 K
271
JiuwenSwarm 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。
Python
2.25 K
677