郭洁 發表於 2025-7-28 19:30:00

Vibe Coding 时代的开源社区开发新体验

<h3 id="01-vibe-coding"><strong>01. Vibe Coding</strong></h3>
<h3 id="从码农到代码指挥家"><strong>从“码农”到“代码指挥家”</strong></h3>
<p>你是否曾感觉,日常开发中很大一部分时间都耗费在了编写那些“理所当然”的模板代码上?比如,为了一个简单的数据模型,你需要写 CURD (增删改查) 的 API、定义数据结构、配置数据库连接…… 这些工作重要但重复,消磨着我们的创造力。</p>
<p>现在,想象一下这样的场景:</p>
<p>你打开一个 AI 代码编辑器,对它说:“嘿,帮我创建一个用户服务,需要能连接到我的 OceanBase 数据库,表结构包含用户ID、姓名和邮箱。”</p>
<p>几秒钟后,一个功能完整的后端服务代码框架就出现在你眼前。</p>
<p>这就是 <strong>Vibe Coding</strong>(或称 Vibe-Driven Development)的魅力。他代表了一种编程理念的转变:<strong>开发者从代码的繁琐执行者,转变为描述意图指挥家</strong>。我们只需提供“Vibe”,剩下的交给 AI。</p>
<p>AI 驱动的开发工具生态正在迅速壮大,不同的工具针对不同的开发工作流各有侧重。为了让大家对当前的主流工具有个全局的了解,我们整理了以下速览表。</p>
<p>下表总结了Cursor、Claude Code 这二款主流 Vibe Coding 工具的特性,以便读者快速了解其差异和优势。</p>
<table>
<thead>
<tr>
<th>特性</th>
<th><strong>Cursor</strong></th>
<th><strong>Claude Code</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>类型</td>
<td>IDE</td>
<td>CLI</td>
</tr>
<tr>
<td>核心<br>优势</td>
<td>实时补全代码<br>代码库上下文感知<br>自然语言编辑<br>可视化开发</td>
<td>深度推理<br>多文件重构<br>Git集成<br>终端原生<br>处理复杂任务</td>
</tr>
</tbody>
</table>
<p>正如表格所示,每款工具都有其独特的优势。我们今天选择 <strong>Cursor</strong> 具体分享将自然语言指令转化为功能代码的实践。</p>
<p>而要让 Cursor 理解并操作专业的数据库系统,我们需要一座桥梁。这座桥梁,就是 <strong>OceanBase MCP Server。</strong></p>
<h4 id="02-链接的桥梁"><strong>02. 链接的桥梁</strong></h4>
<h4 id="oceanbase-mcp-server"><strong>OceanBase MCP Server</strong></h4>
<p>OceanBase MCP Server 是一个实现了 MCP (Machine-Readable Command Protocol) 协议的服务,它为大模型和 OceanBase 数据库之间提供了一座高效沟通的桥梁。简单来说,它让 Cursor 这样的 AI 工具获得了直接与 OceanBase 数据库“对话”和执行 SQL 的超能力。</p>
<p>这个项目已经在 GitHub 上完全开源,欢迎大家来 Star 和贡献:</p>
<p>https://github.com/oceanbase/mcp-oceanbase ✨</p>
<p>今天,我们就将以 OceanBase 社区开发者的真实体验,带你走一遍完整的流程,看看当 Cursor 遇上分布式数据库 OceanBase,会碰撞出怎样惊艳的火花。</p>
<h4 id="03-实践出真知"><strong>03. 实践出真知</strong></h4>
<h4 id="用-vibe-coding-5分钟搭个后端服务"><strong>用 Vibe Coding 5分钟搭个后端服务</strong></h4>
<p>我们将用一个简单的例子,展示如何通过自然语言对话,让 Cursor 借助 OceanBase MCP Server,从零开始创建一个连接 OceanBase 数据库的 FastAPI 后端应用。</p>
<p><strong>前提条件</strong></p>
<p>在开始之前,请确保你的电脑上已经安装:</p>
<ul>
<li>Git 可根据自己的操作系统进行下载安装</li>
<li>Python 3.11 或以上版本</li>
<li>Cursor 客户端,可以在 Cursor 下载页,根据自己的操作系统选择合适的版本进行安装</li>
<li>Python 包管理器 uv</li>
</ul>
<pre><code class="language-plain">curl -LsSf https://astral.sh/uv/install.sh | sh
</code></pre>
<p>安装完成后,使用 uv --version 验证安装是否成功</p>
<p>第一步:配置 OceanBase MCP Server</p>
<p>首先,我们需要让 Cursor 知道如何指挥我们的 OceanBase 数据库。</p>
<p><strong>克隆 OceanBase MCP Server 源码</strong></p>
<pre><code class="language-plain">git clone https://github.com/oceanbase/mcp-oceanbase.git
</code></pre>
<p><strong>安装 MCP Server 依赖</strong>,进入 mcp-oceanbase 目录,使用 uv 创建虚拟环境并安装依赖。</p>
<pre><code class="language-plain"># 进入项目目录
cd mcp-oceanbase
# 创建并激活虚拟环境
uv venvsource .venv/bin/activate
# 安装依赖
uv pip install .
</code></pre>
<p><strong>在 Cursor 中配置 OceanBase MCP Server</strong> 首先,手动创建一个新的工作目录(比如叫 cursor-fastapi-demo),并用 Cursor 打开他。</p>
<p><img src="https://intranetproxy.alipay.com/skylark/lark/0/2025/jpeg/32756313/1753690974866-d1235265-48ed-486b-adb9-54de88327f9a.jpeg"></p>
<p>在 Cursor 中,使用快捷键 Cmd + L (macOS) 或 Ctrl + L (Windows) 唤起聊天框。</p>
<p>点击聊天框右上角的 齿轮(⚙️)图标,选择 MCP Tools。</p>
<p>点击 Add Custom MCP 填写配置文件。</p>
<p><img src="https://intranetproxy.alipay.com/skylark/lark/0/2025/jpeg/32756313/1753690974594-ab4aa691-1261-45db-8611-af30932cc4c3.jpeg"></p>
<p>⏰提示:请务必将上面配置中的 /path/to/your/mcp-oceanbase 和所有 your_ob_* 占位符替换为你的 OceanBase 数据库真实信息。</p>
<p>配置无误后,你会看到 OceanBase 工具显示为“可使用”状态。</p>
<p><img src="https://intranetproxy.alipay.com/skylark/lark/0/2025/jpeg/32756313/1753690974666-3a678014-03a8-44fe-b64b-0e3057e54488.jpeg"></p>
<p><strong>验证连接,</strong>让我们用自然语言测试一下连接。在聊天框中输入:</p>
<p>“ test 库中有多少张表”</p>
<p>Cursor 会调用我们配置的 OceanBase 工具,并生成相应的 SQL 语句。</p>
<p>如果一切顺利,Cursor 会返回表数量。这说明,我们的 AI 助手已经成功连接到了 OceanBase 数据库。</p>
<p><img src="https://intranetproxy.alipay.com/skylark/lark/0/2025/png/32756313/1753690974878-637ff1b6-49df-4c16-a10e-8cf44d7fc6c2.png"></p>
<h4 id="04-vibe-coding-time"><strong>04. Vibe Coding Time</strong></h4>
<h4 id="使用-fastapi-快速创建-restful-api-风格的项目"><strong>使用 FastAPI 快速创建 RESTful API 风格的项目</strong></h4>
<p><strong>用一句话创建数据库表</strong></p>
<p>创建一个 customer 表,主键是 ID,包含 name,age,telephone,location 字段。</p>
<p>Cursor 立刻生成了标准的 <code>CREATE TABLE</code> 语句。确认无误,点击 <strong>Run Tool</strong>,表就建好了。</p>
<p>告别手写 DDL 的繁琐!</p>
<p><img src="https://intranetproxy.alipay.com/skylark/lark/0/2025/png/32756313/1753690974645-c151d422-a6b3-4d1f-adfd-1c429bf2313e.png"></p>
<p><strong>再用一句话插入测试数据</strong></p>
<p>插入10条测试数据</p>
<p>Cursor 再次大显身手,不仅生成了 <code>INSERT</code> 语句,还贴心地编造了 10 条看起来很真实的测试数据。点击 <strong>Run Tool</strong>,数据填充完毕。</p>
<p><img src="https://intranetproxy.alipay.com/skylark/lark/0/2025/png/32756313/1753690975165-befd21d2-cbcf-4e1c-818c-2436547b715e.png"></p>
<p><strong>创建 FastAPI 项目</strong></p>
<p>这是见证奇迹的时刻。我们来个高难度的:</p>
<p>创建一个 FastAPI 项目,生成基于 customer 表的 RESTful API</p>
<p>Cursor 开始“疯狂输出”,在左侧文件浏览器中,他自动创建了 main.py 和 requirements.txt 两个文件。</p>
<p>点击 Accept All 接受所有变更。AI 生成的代码可能每次略有不同,有时需要微调,但这已经为我们节省了至少半小时的工作量。</p>
<p><img src="https://intranetproxy.alipay.com/skylark/lark/0/2025/png/32756313/1753690975229-5c7188ec-602c-41a6-b5b7-90de3ff886cc.png"></p>
<p><strong>启动并验证</strong></p>
<p>现在,我们只需在 Cursor 的终端中,按照常规流程启动这个应用:</p>
<pre><code class="language-plain"># 创建并激活新的虚拟环境
uv venvsource .venv/bin/activate
# 根据 AI 生成的 a requirements.txt 安装依赖
uv pip install -r requirements.txt
# 启动 FastAPI 服务!
uvicorn main:app --reload
</code></pre>
<p>服务启动成功!最后,打开另一个终端窗口,用 <code>curl</code> 命令来访问我们刚刚由 AI 创建的 API,看看是否能从 OceanBase 数据库中取回数据:</p>
<pre><code class="language-plain">curl http://127.0.0.1:8000/customers
</code></pre>
<p>你会看到类似下面这样格式的 JSON 数据被返回:</p>
<pre><code class="language-plain">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"}]
</code></pre>
<p>成功了!从一个空目录开始,我们仅仅通过与 AI 的几次对话,就完成了一个功能齐全、连接 OceanBase 数据库、包含增删改查所有接口的 FastAPI 后端服务。</p>
<p><strong>附上 AI 生成的核心代码 <strong><code>main.py</code></strong>:</strong></p>
<pre><code class="language-plain">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)
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}
</code></pre>
<h4 id="05-结论拥抱新范式释放新潜能"><strong>05. 结论:拥抱新范式,释放新潜能</strong></h4>
<p>这次体验让我们深刻感受到,AI 驱动的 Vibe Coding 已然已经可以落地。我们不再需要为数据库的重复性工作而烦恼,而是可以将更多精力投入到业务逻辑的创新和系统架构的思考上。</p>
<p>这,或许就是 AI 时代,开发者最大的价值所在。</p>
<p><strong>欢迎大家来体验 OceanBase MCP Server!</strong></p>
<p>https://github.com/oceanbase/mcp-oceanbase</p>
<blockquote>
<p>最后为大家推荐这个 OceanBase 开源负责人老纪的公众号「老纪的技术唠嗑局」,会持续更新和 #<strong>数据库</strong>、#<strong>AI</strong>、#<strong>技术架构</strong> 相关的各种技术内容。欢迎感兴趣的朋友们关注!</p>
<p>「老纪的技术唠嗑局」不仅希望能持续给大家带来有价值的技术分享,也希望能和大家一起为开源社区贡献一份力量。如果你对 OceanBase 开源社区认可,点亮一颗小星星✨吧!你的每一个Star,都是我们努力的动力。</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/OBCE666/p/19009514
頁: [1]
查看完整版本: Vibe Coding 时代的开源社区开发新体验