栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

nodejs个人博客开发第四步 数据模型

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nodejs个人博客开发第四步 数据模型

本文为大家分享了nodejs个人博客开发的数据模型,具体内容如下

数据库模型

/model/db.js 数据库操作类,完成链接数据库和数据库的增删查改

查询表

  
 select:function(tableName,callback,where,field){
  field=field ? field : '*';
  var sql="select "+field+" from "+this.C.DB_PRE+tableName;
  if(where){
   sql+=" where "+where;
  }
  this.db.query(sql,callback);
 }

添加记录

 
 add:function(tableName,tableData,callback){
   var sql="insert into "+this.C.DB_PRE+tableName;
   var clumn='';
   var value='';
   for(var key in tableData){
    clumn+=","+key;
    value+=",'"+tableData[key]+"'";
   }
   clumns="("+clumn.substr(1)+")";
  values="("+value.substr(1)+")";
  sql=sql+clumns+"values"+values;
  console.log(sql);
  this.db.query(sql,callback);  
 }

修改记录

 
 update:function(tableName,tableData,where,callback){
   var sql="update "+this.C.DB_PRE+tableName+" set ";
   var clumns="";
   for(var key in tableData){
    clumns+=","+key+"='"+tableData[key]+"'";
   }
  clumns=clumns.substr(1);

  sql+=clumns+" where "+where;
  console.log(sql);
  this.db.query(sql,callback);  
 }

删除记录

 
 delete:function(tableName,where,callback){
   var sql="delete from "+this.C.DB_PRE+tableName+" where "+where;
  console.log(sql);
  this.db.query(sql,callback);  
 }

业务模型

例如分类模型,/model/category.js


module.exports={
 getAllList:function(){
  db.select("category",function(err,list){
   console.log(list);
  });
 },
 
 addCate:function(data){
  db.add("category",data,function(err,list){
   console.log(err);
  });
 },
 
 saveCate:function(data,where){
  db.update("category",data,where,function(err,list){
   console.log(err);
  });
 },
 
 delCate:function(where){
  db.delete("category",where,function(err,list){
   //console.log(err);
  });
 }
};

控制器

先在公共函数文件增加一个调用模型的方法

 
 model:function(name){
  return require("../model/"+name);
 }

控制器调用业务模型


var router=express.Router();
router.get('/',function(req,res,next){
 F.model("category").getAllList();
 //F.model("category").addCate({"name":"测试"});
 //F.model("category").saveCate({"name":"测试1"},"id=4");
 //F.model("category").delCate("id=4");
 
 res.render("home/index");
});
module.exports=router;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/88456.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号