视频演示
https://www.bilibili.com/video/BV1BT4y1E7Nh/?p=14
一起来完成以下步骤:
-
创建数据库连接
-
创建Model,也就是实体类
-
写新增、修改、查询和删除
-
运行:deno run --allow-net --allow-read --allow-write main.ts
#controllers/employeeController.ts
//引入Context
import { Context } from "https://deno.land/x/oak/mod.ts";
//引入仓库类
import employeeRepo from "../repositories/employeeRepo.ts";
//获取当前目录方法 const { cwd } = Deno; class Controller { static async hello(ctx: any) { //cwd获取当前工程目录 //注意 ' !== ` ctx.render(`${cwd()}/views/index.ejs`, { title: "Testing", data: { name: "www.deepincoding.com" } }); } //保存 static async save(ctx: Context) { const {value} = await ctx.request.body(); const result = await employeeRepo.save(value); ctx.response.body = result; } //获取所有 static async findAll(ctx: Context){ const result = await employeeRepo.findAll(); ctx.response.body = result; } //更新 static async update(ctx: Context) { const {value} = await ctx.request.body(); const result = await employeeRepo.update(value); ctx.response.body = result; } //根据ID获取 static async findById(ctx: any){ const id = ctx.params.id; const result = await employeeRepo.findById(id); ctx.response.body = result; } //根据ID删除 static async deleteById(ctx: any){ const id = ctx.params.id; const result = await employeeRepo.deleteById(id); ctx.response.body = result; } } export default Controller;
#database/database.ts
import { DataTypes, Database, Model } from 'https://deno.land/x/denodb/mod.ts';
//MySQL连接
const mysqldb = new Database('mysql', {
host: '127.0.0.1',
username: 'root', password: '123456', database: 'deno' }); //导出 export default mysqldb;
#models/employeeModel.ts
import { DataTypes, Database, Model } from 'https://deno.land/x/denodb/mod.ts';
//员工实体
class EmployeeModel extends Model {
//表名 static table = 'employee'; //字段 static fields = { id: { primaryKey: true, autoIncrement: true }, name: DataTypes.STRING, age: DataTypes.INTEGER }; } export default EmployeeModel;
#repositories/employeeRepo.ts
//引入EmployeeModel实体
import EmployeeModel from "../models/employeeModel.ts";
import mysqldb from "../database/database.ts";
class employeeRepo{ //初始化数据库 static async init(){ mysqldb.link([EmployeeModel]); await mysqldb.sync({ drop: false }); } //新增 static async save (employee: any){ return EmployeeModel.create(employee); } //获取所有 static async findAll(){ return EmployeeModel.all(); } //更新 static async update (employee: any){ return EmployeeModel.where("id",employee.id).update(employee); } //根据ID查询 static async findById(id: number){ return EmployeeModel.where("id",id).get(); } //删除 static async deleteById(id: number){ return EmployeeModel.deleteById(id); } } export default employeeRepo;
#routers/employeeRouter.ts
//引入Router
import { Router } from "https://deno.land/x/oak/mod.ts";
//引入自定义Controller
import Controller from "../controllers/employeeController.ts";
//创建router const router = new Router(); router.get("/",Controller.hello); router.post("/save",Controller.save); router.get("/findAll",Controller.findAll); router.put("/update",Controller.update); router.get("/findById/:id",Controller.findById); router.delete("/deleteById/:id",Controller.deleteById); export default router;
#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";
import employeeRepo from "./repositories/employeeRepo.ts";
//初始化数据库 await employeeRepo.init(); //创建相关页面引擎对象 const ejsEngine = engineFactory.getEjsEngine(); const oakAdapter = adapterFactory.getOakAdapter(); //创建app const app = new Application(); //应用页面引擎 app.use(viewEngine(oakAdapter,ejsEngine)); //应用路由 app.use(router.routes()); app.use(router.allowedMethods()); //log 日志 console.log("Server Port 8000"); //端口 await app.listen({port: 8000 })
来源:oschina
链接:https://my.oschina.net/u/3270570/blog/4320810