• [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;
  • 相关阅读:
    数据结构实验2-迷宫
    离散实验4
    关系代数中的除法运算
    数据库中什么叫象集
    (转)汇编-补码
    2014022201
    20140222
    2014022101
    代码20140221
    代码20140215
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5260096.html
Copyright © 2020-2023  润新知