• Deno MySQL 增删查改接口


    视频演示

    https://www.bilibili.com/video/BV1BT4y1E7Nh/?p=9

    我们一起来完成以下步骤:

    1. 使用之前工程的代码
    2. 创建数据库脚本
    3. 创建数据库连接
    4. 新增员工model
    5. 创建员工仓库类repositories
    6. 创建员工services类
    7. 添加增删查改代码

    #controllers/employeeController.ts

    
    import { Context } from "https://deno.land/x/oak/mod.ts";
    import { Employee } from "../models/employee.ts";
    import employeeService  from "../services/employeeService.ts";
    
    const { cwd } = Deno;
    
    class Controller {
        static async hello(ctx: any){
            //cwd获取当前工程目录 
            //注意 ' !== `
            ctx.render(`${cwd()}/views/index.ejs`,{
                title:"Testing",
                data:{name:" Deepincoding"}
            }); 
        }
    
        static async save(ctx: Context){
            const bodyValue = await ctx.request.body();
            let employee:Employee={
                id: 0,
                name: bodyValue.value.name,
                age: bodyValue.value.age 
            }
    
            const savedEmployee = await employeeService.save(employee);
            console.log("savedEmployee:"+savedEmployee);
            ctx.response.body = savedEmployee;
    
        }
    
        static async update(ctx: Context){
            var bodyValue = await ctx.request.body();
             let updateEmployee: Employee ={
                 id:bodyValue.value.id,
                 name:bodyValue.value.name,
                 age:bodyValue.value.age
             }
             const updatedEmployee = await employeeService.update(updateEmployee);
             ctx.response.body=updatedEmployee;
         }
    
         static async findById(ctx: any){
            const id = ctx.params.id;
            console.log("id:"+id);
            const employee = await employeeService.findById(id);
            ctx.response.body=employee;
         }
    
         static async findAll(ctx: any){
            const employees = await employeeService.findAll();
            ctx.response.body=employees;
         }
    
         static async deleteById(ctx: any){
            const id = ctx.params.id;
            const success = await employeeService.deleteById(id);
            ctx.response.body=success;
         }
    
    
    }
    
    export default Controller;
    
    

    #database/database.ts

    import { Client } from "https://deno.land/x/mysql/mod.ts"
    
    const client = await new Client().connect({
            hostname: "127.0.0.1",
            username: "root",
            db: "deno",
            poolSize: 3, 
            password: "123456"
    });
    
    export default client;
    

    #models/employee.ts

    export interface Employee{
        id: number,
        name: string,
        age: number
    }
    
    

    #router/employeeRouter.ts

    import { Router } from "https://deno.land/x/oak/mod.ts";
    import Controller from "../controllers/employeeController.ts";
    
    const router = new Router();
    
    router.get("/",Controller.hello);
    
    router.post("/save",Controller.save);
    router.put("/update",Controller.update);
    router.get("/findById/:id",Controller.findById);
    router.get("/findAll",Controller.findAll);
    router.delete("/deleteById/:id",Controller.deleteById);
    
    
    export default router;
    

    #services/employeeService.ts

    import { Employee } from  "../models/employee.ts";
    import emlpoyeeRepo from "../repositories/employeeRepo.ts";
    
    class employeeService{
        static async save(employee: Employee){
            return emlpoyeeRepo.save(employee);
        }
    
        static async update(employee: Employee){
            return  emlpoyeeRepo.update(employee);  
        }
        static async findById(id: number){
            return  emlpoyeeRepo.findById(id);
        }
        static async findAll(){
            return  emlpoyeeRepo.findAll();
        }
    
        static async deleteById(id: number){
            return  emlpoyeeRepo.deleteById(id);
        }
    
        
    }
    
    export default employeeService;
    
    
    

    #main.ts

    
    
    import { Application } from "https://deno.land/x/oak/mod.ts"
    import {viewEngine,engineFactory,adapterFactory} from "https://deno.land/x/view_engine/mod.ts";
    import router from "./routers/employeeRouter.ts";
    
    const ejsEngine = engineFactory.getEjsEngine();
    const oakAdapter = adapterFactory.getOakAdapter();
    
    const app = new Application();
    app.use(viewEngine(oakAdapter,ejsEngine));
    app.use(router.routes());
    app.use(router.allowedMethods());
    
    console.log("Server Port 8000");
    
    await app.listen({port: 8000 })
    
    
    
    

     

  • 相关阅读:
    JAVA this
    JAVA static关键字
    子类实例化 super
    TCP/IP概述
    Java多态
    植物大战僵尸:寻找阳光掉落Call调用
    JVM总结+个人整理补充--转
    已知微分方程通解求微分方程
    Redis安装过程中的问题
    对称阵和反对称阵
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/13098836.html
Copyright © 2020-2023  润新知