设计思想,主要目的是简化计算机程序访问数据库
1. ORM:对象关系模型(对象关系映射) Object Releastion Model,程序中的对象和数据库中的关系(表格)进行映射。可以使
开发者在程序方便的对数据库进行操作(用户在程序操作对象实际就是操作数据库的表格)
(1)程序中的模型(即为类) <———-对应———-> 表名
(2)模型的属性(类中定义的属性)<———-对应———-> 表的列
(3)由模型创建的对象(类的实例)<———-对应———-> 表中的行
//数据库中的表:bookInfo(id,isbn,name,author,press,price,publishDate),该表在程序中的映射
//js中的类
class Book{
constructor(id,isbn,name,author,press,price,publishDate){
this.id = id
this.isbn = isbn
......
}
}
/* 类名:Book
属性名:id,isbn,name,author,press,price,publishDate
*/
let b1 = new Book(1,90000123,'A','A1','abc',78,'2010-12-15')
//映射关系:
Book ------------ BookInfo
id属性 ------------ id列
b1 ---- BookInfo表中的一条记录(行)
1. Sequelize模块:支持orm映射的模块,可以访问关系型数据库,可以提高node程序访问数据库的开发效率
(1)安装MySQL模块:npm install mysql2
(2)安装Sequlize模块:npm install sequelize
(1)创建数据库的配置文件
第一步:导入
第二步:创建数据库的配置对象
//1.导入Sequelize模块
const Sequelize = require('sequelize')
//new Sequelize('数据库名','用户名','密码',{配置信息})
//2.使用sequelize模块配置和数据库的连接信息:创建连接数据库的对象
const mysql_Sequelize = new Sequelize('dbms','root','123456',{
host:'localhost', //数据库服务器的IP地址或域名
port: 3306, //数据库使用的端口号。MySQL数据库的默认端口号是3306
dialect: 'mysql',//数据库的类型
pool:{ //数据库连接池:放若干个数据库的连接对象,提高数据库的访问效率
max: 20, //数据库连接池中连接对象的最大个数
min: 3, //数据库连接池中连接对象的最少个数
idle: 20000 //等待延迟的时间,单位是毫秒
},
define:{
'charset': 'utf8' //处理Mysql中中文字符问题
}
})
//3.导出数据库的连接对象
module.exports = mysql_Sequelize;
第三步:测试连接配置
const mysql_test = require('./mysqlconfig')
mysql_test.authenticate() //用来测试数据库是否连接成功
.then(()=>{
console.log('数据库连接成功')
}).catch((err)=>{
console.log('数据库连接失败'+err)
})
(2)创建模型:实现模型和数据表的映射
模型名 = sequelize.define(‘数据表名’,’模型的属性’,{ 其他配置 } )
模型名 对应的是 数据表名
模型的属性对应的是表的列
const Sequelize = require('sequelize')
//1.导入数据库的配置对象
const mysql_Sequelize = require('../config/mysqlconfig')
//2.创建模型与数据库中的表实现映射
const Book = mysql_Sequelize.define('bookinfo',{
id:{
type: Sequelize.INTEGER, //表示id的数据类型为int型(整数型)
autoIncrement: true, //表示id的值在表中是自增的
allowNull: false, //表示id对应列的值不能为空
fields: 'id' //实现模型的属性名和表的列名之间映射关系(对应关系)
},
isbn: {
type: Sequelize.STRING,
allowNull: true,
fields:'isbn'
},
name: {
type: Sequelize.STRING,
allowNull: true,
fields: 'name'
},
author:{
type: Sequelize.STRING,
allowNull: true,
fields:'author'
},
press:{
type: Sequelize.STRING, //STRING类型对应Mysql中的varchar类型
allowNull:true,
fields:'press'
},
price: {
type: Sequelize.DECIMAL(10,2), //小数类型,共10位,其中小数点后有2位
allowNull:true,
fields:'price'
},
pubdate: {
type: Sequelize.DATE, //日期类型
allowNull: true,
fields: 'pubdate'
}
},{
freezeTableName: true, //不使用sequelize给模型自定义的表名(自定义表名的命名规则:模型名后加s)
timestamps: false //若为true,在获取数据时,会自动添加量列数据(createTime、updateTime)
})
//2.导出模型
module.exports = Book
(3)创建接口文件:使用模型操作数据表
插入记录:
模型名.create( { } ).then((result)=>{ }):then表示插入操作完成后的处理,result参数中保存有数据库返回的信息
//4.插入记录接口:http://localhost:9001/bookapi/addBook
router.post('/addBook',(req,res)=>{
try{
Book.create({
isbn: req.body.book_isbn,
name: req.body.book_name,
author: req.body.book_author,
press: req.body.book_publish,
price: req.body.book_price,
pubdate: req.body.publish_date
}).then((result)=>{
if(result){
res.json({
code: 1002
})
}
})
}catch (e) {
console.log(e)
res.json({
code: 1000
})
}
})
(4)前端页面:使用jQuery向服务器端发起ajax的请求
表单序列化:表单名.serialize() ,作用是将表单中所有控件的值序列化成字符
<script src="./jquery-3.4.1.js"></script>
<style>
div {
width: 500px;
margin: 50px auto;
}
</style>
<body>
<div>
<form id="addform">
<label>
ISBN:
<input type="text" name="book_isbn">
</label>
<br><br>
<label>
图书名称:
<input type="text" name="book_name">
</label>
<br><br>
<label>
图书作者:
<input type="text" name="book_author">
</label>
<br><br>
<label>
出版社:
<input type="text" name="book_publish">
</label>
<br><br>
<label>
图书单价:
<input type="text" name="book_price">
</label>
<br><br>
<label>
出版日期:
<input type="date" name="publish_date">
</label>
<br><br>
<button type="button" id="btn_submit">提交</button>
<button type="reset">重置</button>
</form>
</div>
<script>
$(function(){
$('#btn_submit').bind('click',function(){
$.ajax({
url:'http://localhost:8790/bookapi/addBook',
type:'post',
data:$('#addform').serialize(),//表单序列化
dataType:'json',
success:function(result){
if(result.code === 1002){
alert('添加成功')
}else{
alert('添加失败')
}
}
})
})
})
</script>
</body>
//1.导入Express模块
const express = require('express')
//2. 使用express模块创建路由器
const router = express.Router()
//导入Book模型
const Book = require('../../db/model/bookModel')
//3. 创建测试接口:http://localhost:9001/bookapi/booktest
router.get('/booktest',(req,res)=>{
res.json({
code: 1000,
msg: '接口测试'
})
})
//4.插入记录接口:http://localhost:9001/bookapi/addBook
router.post('/addBook',(req,res)=>{
try{
Book.create({
isbn: req.body.book_isbn,
name: req.body.book_name,
author: req.body.book_author,
press: req.body.book_publish,
price: req.body.book_price,
pubdate: req.body.publish_date
}).then((result)=>{
if(result){
res.json({
code: 1002
})
}
})
}catch (e) {
console.log(e)
res.json({
code: 1000
})
}
})
//4. 导出路由器
module.exports = router