手机
当前位置:查字典教程网 >电脑 >服务器_操作系统教程 >nodejs个人博客数据模型开发教程
nodejs个人博客数据模型开发教程
摘要:本文为大家分享了nodejs个人博客开发的数据模型,具体内容如下数据库模型/model/db.js数据库操作类,完成链接数据库和数据库的增删...

  本文为大家分享了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;

【nodejs个人博客数据模型开发教程】相关文章:

linux Crontab 使用基础教程

Linux系统中tr命令的基本使用教程

CentOS在不重启的情况下为其虚拟机添加新硬盘的教程

Linux系统下安装配置postfix邮件服务器教程

C#实现十五子游戏教程

MyBatis拦截器:给参数对象属性赋值的实例

Linux下用于对比文件的diff命令使用教程

nodejs个人博客载入页面开发教程

Linux下使用httpry来嗅探HTTP流量教程

JS简单验证上传文件类型的教程

精品推荐
分类导航