From 959dfbcd56f6b655777a57e70cf49ce9f9791cad Mon Sep 17 00:00:00 2001 From: chenjunhao <2509288351@qq.com> Date: Sun, 14 Apr 2024 21:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...16\347\253\257\344\273\243\347\240\201.md" | 118 ++++++++++++++++ ...15\346\256\265\344\273\243\347\240\201.md" | 127 ++++++++++++++++++ 2 files changed, 245 insertions(+) create mode 100644 "\351\231\210\344\277\212\350\261\252/\347\254\224\350\256\260/20244008\345\220\216\347\253\257\344\273\243\347\240\201.md" create mode 100644 "\351\231\210\344\277\212\350\261\252/\347\254\224\350\256\260/20244009\345\211\215\346\256\265\344\273\243\347\240\201.md" diff --git "a/\351\231\210\344\277\212\350\261\252/\347\254\224\350\256\260/20244008\345\220\216\347\253\257\344\273\243\347\240\201.md" "b/\351\231\210\344\277\212\350\261\252/\347\254\224\350\256\260/20244008\345\220\216\347\253\257\344\273\243\347\240\201.md" new file mode 100644 index 0000000..b74600f --- /dev/null +++ "b/\351\231\210\344\277\212\350\261\252/\347\254\224\350\256\260/20244008\345\220\216\347\253\257\344\273\243\347\240\201.md" @@ -0,0 +1,118 @@ +```js +import koa from "koa" +import Router from "koa-router" +import cors from "koa2-cors" +import bodyParser from "koa-bodyparser" +import { DataTypes, Sequelize, Op, or } from "sequelize" + +//连接数据库 +let sequelize = new Sequelize('filmALL', 'sa', '2080273017', { + host: "localhost", + dialect: "mssql" +}) + +//创建表 +const playerinfo = sequelize.define("players", { + playername: { type: DataTypes.STRING }, + age: { type: DataTypes.INTEGER }, + location: { type: DataTypes.STRING } +}) + + +//更新建表 +await playerinfo.sync(); + +let app = new koa() +let router = new Router() +app.use(cors()) +app.use(bodyParser()) + +router.get("/player/:id?", async (ctx) => { + /*思路: + 1.尝试获取id,若id为0,则说明获取的是列表数据 + 2.通过ORM工具,向数据库获取列表数据或指定行数据(可根据需要实现分页功能) + 3.通过ctx.body返回响应数据 + */ + let key = ctx.request.query.key //获取前端传来的数据 + console.log('key-------', key); + + if (key) { + + let find = await playerinfo.findAll({ + where: { + [Op.or]: [ + { playername: { [Op.like]: `%${key}%` } }, + { age:{[Op.like]:`%${key}%`}}, + { location:{[Op.like]:`%${key}%`}} + ] + } + }) + console.log('find-------', find); + if(find){ + ctx.body = { + code: 1000, + data: find, + msg: '模糊查询成功!' + } + } + } + else{ + let id = ctx.params.id||0 + if(id){ + let item = await playerinfo.findByPk(id) //查找指定id的数据方法 + ctx.body = item + } + else{ + let item = await playerinfo.findAll() + ctx.body = item + } + + } +}) +router.post("/player1", async (ctx) => { + let obj = ctx.request.body + let item = await playerinfo.create(obj) + ctx.body = item +}) + +router.put("/player/:id", async (ctx) => { + let obj = ctx.request.body + let id = ctx.params.id || 0 + let item = await playerinfo.findByPk(id) + if (item) { + let data = await playerinfo.update(obj, { + where: { + id: id + } + }) + ctx.body = data + } + else { + ctx.body = "未查询到指定选手" + } +}) + +router.delete("/player/:id", async (ctx) => { + let id = ctx.params.id || 0 + let item = await playerinfo.findByPk(id) + if (item) { + let del = await playerinfo.destroy({ + where: { + id: id + } + }) + ctx.body = "删除指定选手数据成功" + } + else { + ctx.body = "未找到指定选手" + } + +}) + + +app.use(router.routes()) + +let port = 3000 +app.listen(port) +console.log(`http://localhost:${port}`); +``` diff --git "a/\351\231\210\344\277\212\350\261\252/\347\254\224\350\256\260/20244009\345\211\215\346\256\265\344\273\243\347\240\201.md" "b/\351\231\210\344\277\212\350\261\252/\347\254\224\350\256\260/20244009\345\211\215\346\256\265\344\273\243\347\240\201.md" new file mode 100644 index 0000000..b79b91f --- /dev/null +++ "b/\351\231\210\344\277\212\350\261\252/\347\254\224\350\256\260/20244009\345\211\215\346\256\265\344\273\243\347\240\201.md" @@ -0,0 +1,127 @@ +```js +$(function gett(){ + axios.get("http://localhost:3000/player").then(res=>{ + let data = res.data + let i = 1 + data.forEach(itme => { + let tb = $(".tb") + let html = ` +