視頻演示
https://www.bilibili.com/video/BV1BT4y1E7Nh/?p=14
一起來完成以下步驟:
- 創(chuàng)建數(shù)據(jù)庫連接
- 創(chuàng)建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";
//獲取當(dāng)前目錄方法
const { cwd } = Deno;
class Controller {
static async hello(ctx: any) {
//cwd獲取當(dāng)前工程目錄
//注意 ' !== `
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;
}
//根據(jù)ID獲取
static async findById(ctx: any){
const id = ctx.params.id;
const result = await employeeRepo.findById(id);
ctx.response.body = result;
}
//根據(jù)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'
});
//導(dǎo)出
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{
//初始化數(shù)據(jù)庫
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);
}
//根據(jù)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";
//創(chuàng)建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";
//初始化數(shù)據(jù)庫
await employeeRepo.init();
//創(chuàng)建相關(guān)頁面引擎對象
const ejsEngine = engineFactory.getEjsEngine();
const oakAdapter = adapterFactory.getOakAdapter();
//創(chuàng)建app
const app = new Application();
//應(yīng)用頁面引擎
app.use(viewEngine(oakAdapter,ejsEngine));
//應(yīng)用路由
app.use(router.routes());
app.use(router.allowedMethods());
//log 日志
console.log("Server Port 8000");
//端口
await app.listen({port: 8000 })