01. Vibe Coding
从“码农”到“代码指挥家”
你是否曾感觉,日常开发中很大一部分时间都耗费在了编写那些“理所当然”的模板代码上?比如,为了一个简单的数据模型,你需要写 CURD (增删改查) 的 API、定义数据结构、配置数据库连接…… 这些工作重要但重复,消磨着我们的创造力。
现在,想象一下这样的场景:
你打开一个 AI 代码编辑器,对它说:“嘿,帮我创建一个用户服务,需要能连接到我的 OceanBase 数据库,表结构包含用户ID、姓名和邮箱。”
几秒钟后,一个功能完整的后端服务代码框架就出现在你眼前。
这就是 Vibe Coding(或称 Vibe-Driven Development)的魅力。他代表了一种编程理念的转变:开发者从代码的繁琐执行者,转变为描述意图指挥家。我们只需提供“Vibe”,剩下的交给 AI。
AI 驱动的开发工具生态正在迅速壮大,不同的工具针对不同的开发工作流各有侧重。为了让大家对当前的主流工具有个全局的了解,我们整理了以下速览表。
下表总结了Cursor、Claude Code 这二款主流 Vibe Coding 工具的特性,以便读者快速了解其差异和优势。
特性CursorClaude Code类型IDECLI核心
优势实时补全代码
代码库上下文感知
自然语言编辑
可视化开发深度推理
多文件重构
Git集成
终端原生
处理复杂任务正如表格所示,每款工具都有其独特的优势。我们今天选择 Cursor 具体分享将自然语言指令转化为功能代码的实践。
而要让 Cursor 理解并操作专业的数据库系统,我们需要一座桥梁。这座桥梁,就是 OceanBase MCP Server。
02. 链接的桥梁
OceanBase MCP Server
OceanBase MCP Server 是一个实现了 MCP (Machine-Readable Command Protocol) 协议的服务,它为大模型和 OceanBase 数据库之间提供了一座高效沟通的桥梁。简单来说,它让 Cursor 这样的 AI 工具获得了直接与 OceanBase 数据库“对话”和执行 SQL 的超能力。
这个项目已经在 GitHub 上完全开源,欢迎大家来 Star 和贡献:
https://github.com/oceanbase/mcp-oceanbase ✨
今天,我们就将以 OceanBase 社区开发者的真实体验,带你走一遍完整的流程,看看当 Cursor 遇上分布式数据库 OceanBase,会碰撞出怎样惊艳的火花。
03. 实践出真知
用 Vibe Coding 5分钟搭个后端服务
我们将用一个简单的例子,展示如何通过自然语言对话,让 Cursor 借助 OceanBase MCP Server,从零开始创建一个连接 OceanBase 数据库的 FastAPI 后端应用。
前提条件
在开始之前,请确保你的电脑上已经安装:
- Git 可根据自己的操作系统进行下载安装
- Python 3.11 或以上版本
- Cursor 客户端,可以在 Cursor 下载页,根据自己的操作系统选择合适的版本进行安装
- Python 包管理器 uv
- curl -LsSf https://astral.sh/uv/install.sh | sh
复制代码 安装完成后,使用 uv --version 验证安装是否成功
第一步:配置 OceanBase MCP Server
首先,我们需要让 Cursor 知道如何指挥我们的 OceanBase 数据库。
克隆 OceanBase MCP Server 源码- git clone https://github.com/oceanbase/mcp-oceanbase.git
复制代码 安装 MCP Server 依赖,进入 mcp-oceanbase 目录,使用 uv 创建虚拟环境并安装依赖。- # 进入项目目录
- cd mcp-oceanbase
- # 创建并激活虚拟环境
- uv venvsource .venv/bin/activate
- # 安装依赖
- uv pip install .
复制代码 在 Cursor 中配置 OceanBase MCP Server 首先,手动创建一个新的工作目录(比如叫 cursor-fastapi-demo),并用 Cursor 打开他。
在 Cursor 中,使用快捷键 Cmd + L (macOS) 或 Ctrl + L (Windows) 唤起聊天框。
点击聊天框右上角的 齿轮(⚙️)图标,选择 MCP Tools。
点击 Add Custom MCP 填写配置文件。
⏰提示:请务必将上面配置中的 /path/to/your/mcp-oceanbase 和所有 your_ob_* 占位符替换为你的 OceanBase 数据库真实信息。
配置无误后,你会看到 OceanBase 工具显示为“可使用”状态。
验证连接,让我们用自然语言测试一下连接。在聊天框中输入:
“ test 库中有多少张表”
Cursor 会调用我们配置的 OceanBase 工具,并生成相应的 SQL 语句。
如果一切顺利,Cursor 会返回表数量。这说明,我们的 AI 助手已经成功连接到了 OceanBase 数据库。
04. Vibe Coding Time
使用 FastAPI 快速创建 RESTful API 风格的项目
用一句话创建数据库表
创建一个 customer 表,主键是 ID,包含 name,age,telephone,location 字段。
Cursor 立刻生成了标准的 CREATE TABLE 语句。确认无误,点击 Run Tool,表就建好了。
告别手写 DDL 的繁琐!
再用一句话插入测试数据
插入10条测试数据
Cursor 再次大显身手,不仅生成了 INSERT 语句,还贴心地编造了 10 条看起来很真实的测试数据。点击 Run Tool,数据填充完毕。
创建 FastAPI 项目
这是见证奇迹的时刻。我们来个高难度的:
创建一个 FastAPI 项目,生成基于 customer 表的 RESTful API
Cursor 开始“疯狂输出”,在左侧文件浏览器中,他自动创建了 main.py 和 requirements.txt 两个文件。
点击 Accept All 接受所有变更。AI 生成的代码可能每次略有不同,有时需要微调,但这已经为我们节省了至少半小时的工作量。
启动并验证
现在,我们只需在 Cursor 的终端中,按照常规流程启动这个应用:- # 创建并激活新的虚拟环境
- uv venvsource .venv/bin/activate
- # 根据 AI 生成的 a requirements.txt 安装依赖
- uv pip install -r requirements.txt
- # 启动 FastAPI 服务!
- uvicorn main:app --reload
复制代码 服务启动成功!最后,打开另一个终端窗口,用 curl 命令来访问我们刚刚由 AI 创建的 API,看看是否能从 OceanBase 数据库中取回数据:- curl http://127.0.0.1:8000/customers
复制代码 你会看到类似下面这样格式的 JSON 数据被返回:- curl http://127.0.0.1:8000/customers[{"id":1,"name":"Alice","age":28,"telephone":"1234567890","location":"Beijing"},{"id":2,"name":"Bob","age":32,"telephone":"2345678901","location":"Shanghai"},{"id":3,"name":"Charlie","age":25,"telephone":"3456789012","location":"Guangzhou"},{"id":4,"name":"David","age":40,"telephone":"4567890123","location":"Shenzhen"},{"id":5,"name":"Eve","age":22,"telephone":"5678901234","location":"Chengdu"},{"id":6,"name":"Frank","age":35,"telephone":"6789012345","location":"Wuhan"},{"id":7,"name":"Grace","age":30,"telephone":"7890123456","location":"Hangzhou"},{"id":8,"name":"Heidi","age":27,"telephone":"8901234567","location":"Nanjing"},{"id":9,"name":"Ivan","age":29,"telephone":"9012345678","location":"Tianjin"},{"id":10,"name":"Judy","age":31,"telephone":"0123456789","location":"Chongqing"}]
复制代码 成功了!从一个空目录开始,我们仅仅通过与 AI 的几次对话,就完成了一个功能齐全、连接 OceanBase 数据库、包含增删改查所有接口的 FastAPI 后端服务。
附上 AI 生成的核心代码 main.py:- from fastapi import FastAPI, HTTPException, Depends
- from pydantic import BaseModel
- from typing import List
- from sqlalchemy import create_engine, Column, Integer, String
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy.orm import sessionmaker, Session
- # OceanBase 连接配置(请根据实际情况修改)
- DATABASE_URL = "mysql://root@sun:ShengTai%402024yyds@11.124.9.21:22881/test"
- engine = create_engine(DATABASE_URL, echo=True)
- SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
- Base = declarative_base()
- class Customer(Base):
- __tablename__ = "customer"
- id = Column(Integer, primary_key=True, index=True)
- name = Column(String(100))
- age = Column(Integer)
- telephone = Column(String(20))
- location = Column(String(100))
-
- class CustomerCreate(BaseModel):
- id: int
- name: str
- age: int
- telephone: str
- location: str
-
- class CustomerUpdate(BaseModel):
- name: str = None
- age: int = None
- telephone: str = None
- location: str = None
-
- class CustomerOut(BaseModel):
- id: int
- name: str
- age: int
- telephone: str
- location: str
- class Config:
- orm_mode = True
- def get_db():
- db = SessionLocal()
- try:
- yield db
- finally:
- db.close()
-
- app = FastAPI()
- @app.post("/customers/", response_model=CustomerOut)
- def create_customer(customer: CustomerCreate, db: Session = Depends(get_db)):
- db_customer = Customer(**customer.dict())
- db.add(db_customer)
- try:
- db.commit()
- db.refresh(db_customer)
- except Exception as e:
- db.rollback()
- raise HTTPException(status_code=400, detail=str(e))
- return db_customer
-
- @app.get("/customers/", response_model=List[CustomerOut])
- def read_customers(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
- return db.query(Customer).offset(skip).limit(limit).all()
-
- @app.get("/customers/{customer_id}", response_model=CustomerOut)
- def read_customer(customer_id: int, db: Session = Depends(get_db)):
- customer = db.query(Customer).filter(Customer.id == customer_id).first()
- if customer is None:
- raise HTTPException(status_code=404, detail="Customer not found")
- return customer
-
- @app.put("/customers/{customer_id}", response_model=CustomerOut)
- def update_customer(customer_id: int, customer: CustomerUpdate, db: Session = Depends(get_db)):
- db_customer = db.query(Customer).filter(Customer.id == customer_id).first()
- if db_customer is None:
- raise HTTPException(status_code=404, detail="Customer not found")
- for var, value in vars(customer).items():
- if value is not None:
- setattr(db_customer, var, value)
- db.commit()
- db.refresh(db_customer)
- return db_customer
-
- @app.delete("/customers/{customer_id}")
- def delete_customer(customer_id: int, db: Session = Depends(get_db)):
- db_customer = db.query(Customer).filter(Customer.id == customer_id).first()
- if db_customer is None:
- raise HTTPException(status_code=404, detail="Customer not found")
- db.delete(db_customer)
- db.commit()
- return {"ok": True}
复制代码 05. 结论:拥抱新范式,释放新潜能
这次体验让我们深刻感受到,AI 驱动的 Vibe Coding 已然已经可以落地。我们不再需要为数据库的重复性工作而烦恼,而是可以将更多精力投入到业务逻辑的创新和系统架构的思考上。
这,或许就是 AI 时代,开发者最大的价值所在。
欢迎大家来体验 OceanBase MCP Server!
https://github.com/oceanbase/mcp-oceanbase
最后为大家推荐这个 OceanBase 开源负责人老纪的公众号「老纪的技术唠嗑局」,会持续更新和 #数据库、#AI、#技术架构 相关的各种技术内容。欢迎感兴趣的朋友们关注!
「老纪的技术唠嗑局」不仅希望能持续给大家带来有价值的技术分享,也希望能和大家一起为开源社区贡献一份力量。如果你对 OceanBase 开源社区认可,点亮一颗小星星✨吧!你的每一个Star,都是我们努力的动力。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |