From 4d53303e8b099db10b42e509c0477c39f5b730bd Mon Sep 17 00:00:00 2001 From: qingshou <2660634657@qq.com> Date: Sun, 7 Jun 2026 21:23:21 +0800 Subject: [PATCH 1/3] 1 --- ...0260601-\351\205\215\347\275\256VSCode.md" | 24 +++ ...3-\346\216\247\345\210\266\345\231\250.md" | 157 ++++++++++++++++++ .../20260604-CRUD.md" | 126 ++++++++++++++ ...74\350\210\252\345\261\236\346\200\247.md" | 126 ++++++++++++++ 4 files changed, 433 insertions(+) create mode 100644 "\345\220\264\345\256\232\346\263\242/20260601-\351\205\215\347\275\256VSCode.md" create mode 100644 "\345\220\264\345\256\232\346\263\242/20260603-\346\216\247\345\210\266\345\231\250.md" create mode 100644 "\345\220\264\345\256\232\346\263\242/20260604-CRUD.md" create mode 100644 "\345\220\264\345\256\232\346\263\242/20260604-\345\257\274\350\210\252\345\261\236\346\200\247.md" diff --git "a/\345\220\264\345\256\232\346\263\242/20260601-\351\205\215\347\275\256VSCode.md" "b/\345\220\264\345\256\232\346\263\242/20260601-\351\205\215\347\275\256VSCode.md" new file mode 100644 index 0000000..c1e8162 --- /dev/null +++ "b/\345\220\264\345\256\232\346\263\242/20260601-\351\205\215\347\275\256VSCode.md" @@ -0,0 +1,24 @@ +第一步:注册 MiniMax 账号 + +第二步:设置系统环境变量 +```bash +1.右键"此电脑" → 属性 → 高级系统设置 → 环境变量 +2.在"用户变量"中点击"新建",依次添加以下两个变量: +变量名 变量值 +ANTHROPIC_BASE_URL https://api.minimaxi.com/anthropic +ANTHROPIC_API_KEY 你在 MiniMax 控制台生成的 API Key + +3.点击确定保存,重启终端和 VS Code 使变量生效 +``` + +第三步:在 Claude 扩展中选择模型 +```bash +打开 VS Code,点击侧边栏 Claude 图标 +在模型选择下拉框中选择 MiniMax M2.7 +发送一条测试消息,收到回复即配置成功 +验证环境变量是否生效: + +# 终端中输入,确认变量已设置 +echo $ANTHROPIC_BASE_URL +echo $ANTHROPIC_API_KEY +``` \ No newline at end of file diff --git "a/\345\220\264\345\256\232\346\263\242/20260603-\346\216\247\345\210\266\345\231\250.md" "b/\345\220\264\345\256\232\346\263\242/20260603-\346\216\247\345\210\266\345\231\250.md" new file mode 100644 index 0000000..ea0a08e --- /dev/null +++ "b/\345\220\264\345\256\232\346\263\242/20260603-\346\216\247\345\210\266\345\231\250.md" @@ -0,0 +1,157 @@ +### 笔记 +``` +路由 (Router):负责接收请求、匹配地址 +控制器 (Controller):负责业务逻辑、参数处理、返回结果 +作用:把接口逻辑从路由文件抽离,代码解耦、便于维护,项目变大后必备。 + +1. 入口文件 app.js(不变 + 引入路由) +js +运行 +const express = require('express') +const cors = require('cors') +// 引入路由 +const userRouter = require('./routes/user') + +const app = express() +const port = 3000 + +// 全局中间件 +app.use(cors()) +app.use(express.json()) // 解析post json参数 + +// 挂载路由 +app.use('/api', userRouter) + +app.listen(port, () => { + console.log(`服务启动:http://localhost:${port}`) +}) +2. 编写控制器 controllers/user.js +只写业务逻辑,不处理路由 +js +运行 +// 控制器:处理具体逻辑 +const getUserInfo = (req, res) => { + // 获取路由参数 + const id = req.query.id + // 模拟数据库查询 + const user = { + id: id, + name: "张三", + age: 20 + } + // 返回接口数据 + res.json({ + code: 200, + msg: "查询成功", + data: user + }) +} + +const addUser = (req, res) => { + // 获取post请求体参数 + const { name, age } = req.body + res.json({ + code: 200, + msg: "添加用户成功", + data: { name, age } + }) +} + +// 导出方法,供路由调用 +module.exports = { + getUserInfo, + addUser +} +3. 编写路由 routes/user.js +只做路由匹配,不写业务 +js +运行 +const express = require('express') +const router = express.Router() +// 引入控制器 +const userCtrl = require('../controllers/user') + +// 路由 -> 关联控制器方法 +router.get('/user', userCtrl.getUserInfo) +router.post('/user', userCtrl.addUser) + +module.exports = router +```### 笔记 +``` +路由 (Router):负责接收请求、匹配地址 +控制器 (Controller):负责业务逻辑、参数处理、返回结果 +作用:把接口逻辑从路由文件抽离,代码解耦、便于维护,项目变大后必备。 + +1. 入口文件 app.js(不变 + 引入路由) +js +运行 +const express = require('express') +const cors = require('cors') +// 引入路由 +const userRouter = require('./routes/user') + +const app = express() +const port = 3000 + +// 全局中间件 +app.use(cors()) +app.use(express.json()) // 解析post json参数 + +// 挂载路由 +app.use('/api', userRouter) + +app.listen(port, () => { + console.log(`服务启动:http://localhost:${port}`) +}) +2. 编写控制器 controllers/user.js +只写业务逻辑,不处理路由 +js +运行 +// 控制器:处理具体逻辑 +const getUserInfo = (req, res) => { + // 获取路由参数 + const id = req.query.id + // 模拟数据库查询 + const user = { + id: id, + name: "张三", + age: 20 + } + // 返回接口数据 + res.json({ + code: 200, + msg: "查询成功", + data: user + }) +} + +const addUser = (req, res) => { + // 获取post请求体参数 + const { name, age } = req.body + res.json({ + code: 200, + msg: "添加用户成功", + data: { name, age } + }) +} + +// 导出方法,供路由调用 +module.exports = { + getUserInfo, + addUser +} +3. 编写路由 routes/user.js +只做路由匹配,不写业务 +js +运行 +const express = require('express') +const router = express.Router() +// 引入控制器 +const userCtrl = require('../controllers/user') + +// 路由 -> 关联控制器方法 +router.get('/user', userCtrl.getUserInfo) +router.post('/user', userCtrl.addUser) + +module.exports = router +``` \ No newline at end of file diff --git "a/\345\220\264\345\256\232\346\263\242/20260604-CRUD.md" "b/\345\220\264\345\256\232\346\263\242/20260604-CRUD.md" new file mode 100644 index 0000000..f27019c --- /dev/null +++ "b/\345\220\264\345\256\232\346\263\242/20260604-CRUD.md" @@ -0,0 +1,126 @@ +一.使用数据库 + +1.安装依赖包和工具包 +```bash +依赖包:dotnet add package Microsoft.EntityFrameworkCore.Sqlite -v 8 +依赖包:dotnet add package Microsoft.EntityFrameworkCore.Design -v 8 +工具包dotnet tool install -g dotnet-ef +``` + +2.修改配置文件,添加数据链接字符串 +appsettings.json +```json +"ConnectionStrings": { "sqlite":"data source=./db.db;" } +``` + +3.在入口注册数据库上下文实例到容器 +program.cs +```cs +builder.Services.AddDbContext(opts=>{opts.UseSqlite("数据库连接字符串")}) +``` + + +4.生成迁移文件 +```bash +dotnet ef migrations add Xxxx +dotnet ef migrations add Init +dotnet ef migrations add AddProduct +dotnet ef migrations add RemovePrdocutColumn +``` + +5.更新迁移到数据库 +```bash +dotnet ef database update +``` + +二.CRUD + +- Model + -Category.cs +```cs +namespace ShoppingMall.Model; + +public class Category +{ + public int Id { get; set; } + + public string CateName { get; set; } = null!; + + public string? Description { get; set; } +} + +``` + + +-Controllers + -CategoriesController.cs + +```cs +using Microsoft.AspNetCore.Mvc; +using ShoppingMall.Data; +using ShoppingMall.Model; + +namespace ShoppingMall.Controllers; + +[ApiController] +[Route("[controller]")] +public class CategoriesController : ControllerBase +{ + private readonly AppDbContext _db; + + public CategoriesController(AppDbContext db) + { + _db = db; + } + + [HttpGet] + public IEnumerable Index() + { + return _db.Categories.ToList(); + } + + [HttpGet("{id}")] + public Category GetById(int id) + { + // 查不到时回退到空对象,避免上游出现 NullReferenceException + return _db.Categories.Find(id) ?? new Category(); + } + + [HttpPost] + public Category Create(Category obj) + { + _db.Categories.Add(obj); + _db.SaveChanges(); + return obj; + } + + [HttpPut("{id}")] + public Category Update(int id, Category obj) + { + var tmp = _db.Categories.Find(id); + if (tmp is null) + { + return new Category(); + } + tmp.CateName = obj.CateName; + tmp.Description = obj.Description; + _db.Categories.Update(tmp); + _db.SaveChanges(); + return tmp; + } + + [HttpDelete("{id}")] + public Category Del(int id) + { + var tmp = _db.Categories.Find(id); + if (tmp is null) + { + return new Category(); + } + _db.Categories.Remove(tmp); + _db.SaveChanges(); + return tmp; + } +} + +``` diff --git "a/\345\220\264\345\256\232\346\263\242/20260604-\345\257\274\350\210\252\345\261\236\346\200\247.md" "b/\345\220\264\345\256\232\346\263\242/20260604-\345\257\274\350\210\252\345\261\236\346\200\247.md" new file mode 100644 index 0000000..f27019c --- /dev/null +++ "b/\345\220\264\345\256\232\346\263\242/20260604-\345\257\274\350\210\252\345\261\236\346\200\247.md" @@ -0,0 +1,126 @@ +一.使用数据库 + +1.安装依赖包和工具包 +```bash +依赖包:dotnet add package Microsoft.EntityFrameworkCore.Sqlite -v 8 +依赖包:dotnet add package Microsoft.EntityFrameworkCore.Design -v 8 +工具包dotnet tool install -g dotnet-ef +``` + +2.修改配置文件,添加数据链接字符串 +appsettings.json +```json +"ConnectionStrings": { "sqlite":"data source=./db.db;" } +``` + +3.在入口注册数据库上下文实例到容器 +program.cs +```cs +builder.Services.AddDbContext(opts=>{opts.UseSqlite("数据库连接字符串")}) +``` + + +4.生成迁移文件 +```bash +dotnet ef migrations add Xxxx +dotnet ef migrations add Init +dotnet ef migrations add AddProduct +dotnet ef migrations add RemovePrdocutColumn +``` + +5.更新迁移到数据库 +```bash +dotnet ef database update +``` + +二.CRUD + +- Model + -Category.cs +```cs +namespace ShoppingMall.Model; + +public class Category +{ + public int Id { get; set; } + + public string CateName { get; set; } = null!; + + public string? Description { get; set; } +} + +``` + + +-Controllers + -CategoriesController.cs + +```cs +using Microsoft.AspNetCore.Mvc; +using ShoppingMall.Data; +using ShoppingMall.Model; + +namespace ShoppingMall.Controllers; + +[ApiController] +[Route("[controller]")] +public class CategoriesController : ControllerBase +{ + private readonly AppDbContext _db; + + public CategoriesController(AppDbContext db) + { + _db = db; + } + + [HttpGet] + public IEnumerable Index() + { + return _db.Categories.ToList(); + } + + [HttpGet("{id}")] + public Category GetById(int id) + { + // 查不到时回退到空对象,避免上游出现 NullReferenceException + return _db.Categories.Find(id) ?? new Category(); + } + + [HttpPost] + public Category Create(Category obj) + { + _db.Categories.Add(obj); + _db.SaveChanges(); + return obj; + } + + [HttpPut("{id}")] + public Category Update(int id, Category obj) + { + var tmp = _db.Categories.Find(id); + if (tmp is null) + { + return new Category(); + } + tmp.CateName = obj.CateName; + tmp.Description = obj.Description; + _db.Categories.Update(tmp); + _db.SaveChanges(); + return tmp; + } + + [HttpDelete("{id}")] + public Category Del(int id) + { + var tmp = _db.Categories.Find(id); + if (tmp is null) + { + return new Category(); + } + _db.Categories.Remove(tmp); + _db.SaveChanges(); + return tmp; + } +} + +``` -- Gitee From e4f74101a9fe728c91ca8e17b8321dadf2127939 Mon Sep 17 00:00:00 2001 From: qingshou <2660634657@qq.com> Date: Sun, 7 Jun 2026 23:47:04 +0800 Subject: [PATCH 2/3] 1 --- .../20260605-\345\257\274\350\210\252\345\261\236\346\200\247.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\345\220\264\345\256\232\346\263\242/20260604-\345\257\274\350\210\252\345\261\236\346\200\247.md" => "\345\220\264\345\256\232\346\263\242/20260605-\345\257\274\350\210\252\345\261\236\346\200\247.md" (100%) diff --git "a/\345\220\264\345\256\232\346\263\242/20260604-\345\257\274\350\210\252\345\261\236\346\200\247.md" "b/\345\220\264\345\256\232\346\263\242/20260605-\345\257\274\350\210\252\345\261\236\346\200\247.md" similarity index 100% rename from "\345\220\264\345\256\232\346\263\242/20260604-\345\257\274\350\210\252\345\261\236\346\200\247.md" rename to "\345\220\264\345\256\232\346\263\242/20260605-\345\257\274\350\210\252\345\261\236\346\200\247.md" -- Gitee From 4438da60fcd93e4524b930d32eb5350fccd98bbc Mon Sep 17 00:00:00 2001 From: qingshou <2660634657@qq.com> Date: Sun, 14 Jun 2026 18:29:57 +0800 Subject: [PATCH 3/3] 1 --- .../20260608-\345\210\206\351\241\265.md" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "\345\220\264\345\256\232\346\263\242/20260608-\345\210\206\351\241\265.md" diff --git "a/\345\220\264\345\256\232\346\263\242/20260608-\345\210\206\351\241\265.md" "b/\345\220\264\345\256\232\346\263\242/20260608-\345\210\206\351\241\265.md" new file mode 100644 index 0000000..ae11aa0 --- /dev/null +++ "b/\345\220\264\345\256\232\346\263\242/20260608-\345\210\206\351\241\265.md" @@ -0,0 +1,87 @@ +1. 分页请求参数 PageQuery.cs +```csharp +public class PageQuery +{ + private int _page = 1; + private int _pageSize = 20; + + public int Page + { + get => _page; + set => _page = value < 1 ? 1 : value; + } + + public int PageSize + { + get => _pageSize; + set => _pageSize = value switch + { + < 1 => 20, + > 100 => 100, + _ => value + }; + } + + public string? Sort { get; set; } + public string? Order { get; set; } = "asc"; +} +``` +2. 分页返回结果 PagedResult.cs +```csharp +public class PagedResult +{ + public IReadOnlyList Items { get; set; } = Array.Empty(); + public int Page { get; set; } + public int PageSize { get; set; } + public int TotalCount { get; set; } + public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize); + public bool HasNext => Page < TotalPages; + public bool HasPrev => Page > 1; +} +``` +3. 控制器核心:分页 + 排序 +```csharp +[HttpGet] +public async Task>> GetAll( + [FromQuery] PageQuery query) +{ + // 1. 基础查询 + var q = _db.Products.AsNoTracking(); + + // 2. 排序 + q = (query.Sort?.ToLower(), query.Order?.ToLower()) switch + { + ("price", "desc") => q.OrderByDescending(p => p.Price), + ("price", _) => q.OrderBy(p => p.Price), + ("name", "desc") => q.OrderByDescending(p => p.Name), + ("name", _) => q.OrderBy(p => p.Name), + _ => q.OrderBy(p => p.Id) + }; + + // 3. 查总数 + var totalCount = await q.CountAsync(); + + // 4. 分页 + 投影 + var items = await q + .Skip((query.Page - 1) * query.PageSize) + .Take(query.PageSize) + .Select(p => new ProductDto + { + Id = p.Id, + Name = p.Name, + Price = p.Price, + Stock = p.Stock, + CategoryName = p.Category!.Name + }) + .ToListAsync(); + + // 5. 返回结果 + return new PagedResult + { + Items = items, + Page = query.Page, + PageSize = query.PageSize, + TotalCount = totalCount + }; +} +``` \ No newline at end of file -- Gitee