当前位置:   article > 正文

Go秒杀系统3--项目结构搭建,商品模型开发。

Go秒杀系统3--项目结构搭建,商品模型开发。

一、项目结构搭建&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"),
	)
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

二、商品模型开发
商品属性有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
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

后台商品管理开发
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)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

再定义一个结构体实现接口:

type ProductManager struct {
	table string
	mysqlConn *sql.DB
}
  • 1
  • 2
  • 3
  • 4

创建一个构造函数:

func NewProductManager(table string,db *sql.DB) IProduct  {
	return &ProductManager{table:table,mysqlConn:db}
}
  • 1
  • 2
  • 3

实现数据库的连接:

//数据连接
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
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

插入到数据库:

//插入
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()
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

删除商品:

//商品的删除
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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

更新商品:

//商品的更新
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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

查询商品:

//根据商品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



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

本篇内容学自慕课网

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/735058
推荐阅读
相关标签
  

闽ICP备14008679号