sails+mysql demo

来源:互联网 时间:1970-01-01

新建项目




创建实体




安装所需的npm包




测试




浏览器打开,则为正常。




导入项目到webstorm






然后finish,在新窗户打开。就可以看到项目了




基本配置

数据库配置

在config/connection.js中,配置你的mysql数据库。



model配置

在config/models.js中,



model实体

api/models/User.js中添加属性:


tableName: 'users',attributes: { id: { type: 'integer', required: true, primaryKey: true }, username: { type: 'String', required: true }, password: { type: 'String', required: true }, age: { type: 'String', defaultsTo: '0' }, tel: { type: 'String', defaultsTo: '' }, sex: { type: 'String' }},


数据库建表



测试

右键点击app.js,选择run ‘app.js’


增加



查询



根据id查



controller控制器

api/controllers/UserController.js中


module.exports = { 'new': function(req,res) { res.view(); }, create: function(req,res) { var params = _.extend(req.query || {}, req.params || {}, req.body || {}); User.create(params, function userCreated (err, createdUser) { if (err) return res.send(err,500); res.redirect('/user/show/'+ createdUser.id); }); }, show: function (req,res) { var id = req.param('id'); if (!id) return res.send("No id specified.", 500); User.find(id, function userFound(err, user) { if(err) return res.sender(err,500); if(!user) return res.send("User "+id+" not found", 404); res.view({ user:user[0] }) }); }, edit: function (req,res) { var id = req.param('id'); if (!id) return res.send("No id specified.",500); User.find(id, function userFound (err,user){ if (err) return res.send(err,500); if (!user) return res.send("User "+id+" not found.",404); res.view({ user: user[0] }) }); }, update: function (req,res) { var params = _.extend(req.query || {}, req.params || {}, req.body || {}); var id = params.id; if (!id) return res.send("No id specified.",500); User.update(id, params, function userUpdated(err, updatedUser) { if (err) { return res.redirect('/user/edit'); } if(!updatedUser) { return res.redirect('/user/edit'); } res.redirect('/user/show/'+id); }); }, destroy: function (req,res) { var id = req.param('id'); if (!id) return res.send("No id specified.",500); User.find(id, function foundUser(err, user) { if (err) return res.send(err,500); if (!user) return res.send("No user with that idid exists.",404); User.destroy(id, function userDestroyed(err) { if (err) return res.send(err,500); return res.redirect('/user'); }); }) }, find: function(req,res){ var params = _.extend(req.query || {}, req.params || {}, req.body || {}); var whereCondition = {}; var pageBean = {}; var sql = {}; var sax = ''; var username = ''; if(params.username){ whereCondition.username = { 'like': '%' + params.username + '%'}; username = params.username; } if(params.sex){ whereCondition.sex = params.sex; sax = params.sex; } if(params.pageCount){ pageBean.nowPage = params.nowPage; pageBean.pageCount = params.pageCount; pageBean.allCount = params.allCount; pageBean.NumOfPage = params.NumOfPage; if(whereCondition.username || whereCondition.sex){ sql.where = whereCondition; } sql.skip = pageBean.pageCount * (pageBean.nowPage - 1); sql.limit = pageBean.pageCount; sql.sort = 'updatedAt DESC'; //User.find({ where: { name: { 'like': '%张三%' } }, skip: 20, limit: 10, sort: 'name DESC' }); var query = User.find(sql); query.exec(function(err, users){ if (err) return res.send(err, 500); res.view( { model: users, page: pageBean, sex: sax, username: username }); }); }else{ pageBean.nowPage = 1; pageBean.pageCount = 5; if(whereCondition.username || whereCondition.sex){ sql.where = whereCondition; } sql.sort = 'updatedAt DESC'; User.count(sql).exec(function(err, count){ if (err) return res.send(err, 500); pageBean.allCount = count; sql.skip = pageBean.pageCount * (pageBean.nowPage - 1); sql.limit = pageBean.pageCount; pageBean.NumOfPage = Math.ceil(pageBean.allCount / pageBean.pageCount); var query = User.find(sql); query.exec(function(err, users){ if (err) return res.send(err, 500); res.view( { model: users, page: pageBean, sex: sax, username: username }); }); }); } }};


views视图


views/user/edit.ejs


<h1> Editing User </h1><form action="/user/update/<%= user.id %>" method="POST"> <table> <tr><td>Id:</td><td><input name="id" value="<%= user.id %>" type="text" readonly="readonly"/></td></tr> <tr><td>username: </td><td><input value="<%= user.username %>" name="username" type="text"/></td></tr> <tr><td>password:</td><td><input value="<%= user.password %>" name="password" type="text"/></td></tr> <tr><td>tel:</td><td><input value="<%= user.tel %>" name="tel" type="text"/></td></tr> <tr><td>Age: </td><td><input value="<%= user.age %>" name="age" type="text"/></td></tr> <tr><td>Sex: </td><td><input value="<%= user.sex %>" name="sex" type="text"/></td></tr> </table> <input type="submit"/></form><a href="/user/edit/<%= user.id %>">Edit</a> |<a href="/user">Back</a>


views/user/find.ejs


<h1>Find</h1><form action="/user/find" method="POST"> <table> <tr><td>Username: </td><td><input name="username" value='<%= username %>' type="text"/></td></tr> <tr><td>Sex: </td><td><input name="sex" value='<%= sex %>' type="text"/></td></tr> </table> <tr><input type="submit" value="搜索"/></tr></form><h1>Listing users</h1><form id="form1" action="/user/find" method="POST"><table> <tr> <th>Username</th> <th>password</th> <th>tel</th> <th>sex</th> <th>age</th> <th>updatedAt</th> <th></th> <th></th> </tr> <% _.each(model, function (user) { %> <tr> <td><%= user.username %></td> <td><%= user.password %></td> <td><%= user.tel %> </td> <td><%= user.sex == 1 ? '男' : '女' %> </td> <td><%= user.age %> </td> <td><%= user.updatedAt.toLocaleDateString() + " " + user.updatedAt.toLocaleTimeString() %> </td> <td><a href="/user/show/<%= user.id %>">Show</a></td> <td><a href="/user/edit/<%= user.id %>">Edit</a></td> <td><a href="/user/destroy/<%= user.id %>">Delete</a></td> <% }) %> </tr></table> <% if(page) {%> <input name="allCount" value="<%= page.allCount %>" type="hidden" /> <input name="nowPage" value="<%= page.nowPage %>" type="hidden" /> <input name="pageCount" value="<%= page.pageCount %>" type="hidden" /> <input name="NumOfPage" value="<%= page.NumOfPage %>" type="hidden" /> <% } %> <input name="sex" value="<%= sex %>" type="hidden" /> <input name="username" value="<%= username %>" type="hidden" /> <span>第 <%= page.nowPage %> 页 | </span> <span>共 <%= page.NumOfPage %> 页 | </span> <span>共 <%= page.allCount %> 条 | </span> <span><%= page.pageCount %> 条 / 页 </span> <input type="button" value="上一页" onclick="last()" <%if(page && page.nowPage <= 1) {%> disabled="disabled" <%}%>/> <input type="button" value="下一页" onclick="next()" <%if(page && page.nowPage >= page.NumOfPage) {%> disabled="disabled" <%}%>/></form><br /><a href="/user/new">New User</a><script type="application/javascript"> function last(){ var page = document.getElementsByName('nowPage')[0].value; if(page){ document.getElementsByName('nowPage')[0].value = --page; } var form = document.getElementById('form1'); form.submit(); } function next(){ var page = document.getElementsByName('nowPage')[0].value; if(page){ document.getElementsByName('nowPage')[0].value = ++page; } var form = document.getElementById('form1'); form.submit(); }</script>


views/user/new.ejs


<h1>New User</h1><form action="/user/create" method="POST"> <table> <tr><td>Id:</td><td><input name="id" type="text"/></td></tr> <tr><td>username: </td><td><input name="username" type="text"/></td></tr> <tr><td>password:</td><td><input name="password" type="text"/></td></tr> <tr><td>tel:</td><td><input name="tel" type="text"/></td></tr> <tr><td>Age: </td><td><input name="age" type="text"/></td></tr> <tr><td>Sex: </td><td><input name="sex" type="text"/></td></tr> </table> <input type="submit"/></form>


views/user/show.ejs


<h1> User </h1><table> <tr><td>Name:</td><td> <%= user.username %> </td></tr> <tr><td>password:</td><td><%= user.password %></td></tr> <tr><td>tel:</td><td><%= user.tel %></td></tr> <tr><td>Age: </td><td><%= user.age %></td></tr> <tr><td>Sex: </td><td><%= user.sex == 1 ? '男' : '女' %></td></tr></table><a href="/user/edit/<%= user.id %>">Edit</a> |<a href="/user">Back</a>


routes配置


'GET /user' : 'UserController.find'


测试





模糊查询页面




show




edit



create



总结

    基本完成使用sails的增删改查。大体上完成,具体的细节方面还有不足,以后有待改进。

















相关阅读:
Top