vanilla-zh
  • 简介
  • 快速上手
    • Hello World
    • 如何调试
    • 如何新增一个Controller
    • 如何使用models/dao
    • 如何使用models/service
  • APIs
    • 配置
    • Bootstrap
    • Controllers
    • 模板引擎
    • 插件
    • 路由
    • 异常处理
    • 内建类
  • Libs
    • Cookie
  • 进阶
    • 页缓存
    • 面向对象
    • Vanilla 包开发
  • OpenResty
    • OR文档精炼
      • ngx.timer
      • ngx.config
      • coroutine
      • ngx.thread
  • ChangeLogs
    • vanilla-0.1.0.rc7
    • vanilla-0.1.0.rc6
    • vanilla-0.1.0.rc5
    • vanilla-0.1.0.rc4
    • vanilla-0.1.0.rc3
  • 杂项
    • Nginx执行阶段
    • GDB 调试 OpenResty
    • OpenResty 正则示例收集
    • 基于 OpenResty 安装 Luarocks
    • Vanilla集成的一些优秀第三方包
      • QCon 2015 Broken Performance Tools
  • Vanilla使用经验
    • 用户列表
Powered by GitBook
On this page
  • 最简单的 DAO
  • 以上代码解释

Was this helpful?

  1. 快速上手

如何使用models/dao

vanilla 的 DAO 预设为项目对数据源的封装,一切对数据源的操作都可以封装成 DAO,方便维护、管理、缓存等。 Vanilla 的 DAO 在项目的 models/dao 路径下,一般使用 LoadModel 方法进行加载

最简单的 DAO

由自动生成的 demo 中默认生成了 TableDao,可以看出 TableDao 只是一个普通的 LUA 包。

local TableDao = {}

function TableDao:set(key, value)
    self.__cache[key] = value
    return true
end

function TableDao:new()
    local instance = {
        set = self.set,
        __cache = {}
    }
    setmetatable(instance, TableDao)
    return instance
end

function TableDao:__index(key)
    local out = rawget(rawget(self, '__cache'), key)
    if out then return out else return false end
end
return TableDao

以上代码解释

DAO 可以是任何对数据层访问封装的 LUA 包,实现方式非常自由。

Previous如何新增一个ControllerNext如何使用models/service

Last updated 6 years ago

Was this helpful?