赞
踩
一、项目结构搭建&main.go的编写
项目搭建:
main.go的代码:
package main import ( "github.com/kataras/iris" "github.com/kataras/iris/mvc" "imooc-iris/web/controllers" ) func main() { app:=iris.New() app.Logger().SetLevel("debug") app.RegisterView(iris.HTML("./web/views",".html")) //注册控制器 mvc.New(app.Party("/hello")).Handle(new(controllers.MovieController)) app.Run( iris.Addr("localhost:8080"), ) }
二、商品模型开发
商品属性有id,商品名称,商品数量,商品图片
package datamodels
type Product struct {
ID int64 `json:"id" sql:"ID" imooc:"ID"`
ProductName string `json:"ProductName" sql:"productName" imooc:"ProductName"`
ProductNum int64 `json:"ProductNum" sql:"productNum" imooc:"ProductNum"`
ProductImage string `json:"ProductImage" sql:"productImage" imooc:"ProductImage"`
ProductUrl string `json:"ProductUrl" sql:"productUrl" imooc:"ProductUrl"`
}
后台商品管理开发
1.商品模型设计开发
2.商品增删改查功能开发
3.后台商品管理页面
定义一个接口:
package repositories import ( "imooc-product/datamodels" "database/sql" "imooc-product/common" "strconv" ) //第一步,先开发对应的接口 //第二步,实现定义的接口 type IProduct interface { //连接数据 Conn()(error) Insert(*datamodels.Product)(int64,error) Delete(int64) bool Update(*datamodels.Product) error SelectByKey(int64)(*datamodels.Product,error) SelectAll()([]*datamodels.Product,error) }
再定义一个结构体实现接口:
type ProductManager struct {
table string
mysqlConn *sql.DB
}
创建一个构造函数:
func NewProductManager(table string,db *sql.DB) IProduct {
return &ProductManager{table:table,mysqlConn:db}
}
实现数据库的连接:
//数据连接
func (p *ProductManager) Conn()(err error) {
if p.mysqlConn == nil {
mysql,err := common.NewMysqlConn()
if err !=nil {
return err
}
p.mysqlConn = mysql
}
if p.table == "" {
p.table = "product"
}
return
}
插入到数据库:
//插入 func (p *ProductManager) Insert(product *datamodels.Product) (productId int64,err error) { //1.判断连接是否存在 if err=p.Conn();err != nil{ return } //2.准备sql sql :="INSERT product SET productName=?,productNum=?,productImage=?,productUrl=?" stmt,errSql := p.mysqlConn.Prepare(sql) if errSql !=nil { return 0,errSql } //3.传入参数 result,errStmt:=stmt.Exec(product.ProductName,product.ProductNum,product.ProductImage,product.ProductUrl) if errStmt !=nil { return 0,errStmt } return result.LastInsertId() }
删除商品:
//商品的删除 func (p *ProductManager)Delete(productID int64) bool { //1.判断连接是否存在 if err:=p.Conn();err != nil{ return false } sql := "delete from product where ID=?" stmt,err := p.mysqlConn.Prepare(sql) if err!= nil { return false } _,err = stmt.Exec(strconv.FormatInt(productID,10)) if err !=nil { return false } return true }
更新商品:
//商品的更新 func (p *ProductManager)Update(product *datamodels.Product) error { //1.判断连接是否存在 if err:=p.Conn();err != nil{ return err } sql := "Update product set productName=?,productNum=?,productImage=?,productUrl=? where ID="+strconv.FormatInt(product.ID,10) stmt,err := p.mysqlConn.Prepare(sql) if err !=nil { return err } _,err = stmt.Exec(product.ProductName,product.ProductNum,product.ProductImage,product.ProductUrl) if err !=nil { return err } return nil }
查询商品:
//根据商品ID查询商品 func (p *ProductManager) SelectByKey(productID int64) (productResult *datamodels.Product,err error) { //1.判断连接是否存在 if err=p.Conn();err != nil{ return &datamodels.Product{},err } sql := "Select * from "+p.table+" where ID ="+strconv.FormatInt(productID,10) row,errRow :=p.mysqlConn.Query(sql) defer row.Close() if errRow !=nil { return &datamodels.Product{},errRow } result := common.GetResultRow(row) if len(result)==0{ return &datamodels.Product{},nil } productResult = &datamodels.Product{} common.DataToStructByTagSql(result,productResult) return
本篇内容学自慕课网
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。