欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

gin框架学习-dao

程序员文章站 2022-06-16 08:02:34
dao层负责与数据库交流:package daoimport ("Gin_demo/CloudRestaurant/model""Gin_demo/CloudRestaurant/tool""fmt")//定义用户dao层 继承Orm 可以拥有orm的很多基本方法type MemberDao struct {*tool.Orm}//更新用户头像func (md *MemberDao)UploadAvator(userId int64, fileName string...

dao层负责与数据库交流:

package dao

import (
	"Gin_demo/CloudRestaurant/model"
	"Gin_demo/CloudRestaurant/tool"
	"fmt"
)

//定义用户dao层 继承Orm 可以拥有orm的很多基本方法
type MemberDao struct {
	*tool.Orm
}

//更新用户头像
func (md *MemberDao)UploadAvator(userId int64, fileName string) int64 {
	member := model.Member{Avatar:fileName}
	result, err := md.Where("id=?",userId).Update(&member)
	if err != nil {
		return 0
	}
	return result
}

///查询用户信息
func (md *MemberDao)Query(name string, password string) *model.Member {
	var memb model.Member
	_, err := md.Where("user_name=?",name).Get(&memb)
	if err!=nil {
		fmt.Println("查询用户信息, err:",err)
		logs.Error(err.Error())
		return nil
	}
	fmt.Println("memb:",memb)
	return &memb
}

func (md *MemberDao)ValidateSmsCode(phone string, code string) *model.SmsCode {
	var sms model.SmsCode
	_, err := md.Where("phone=? and code=?", phone, code).Get(&sms)
	if err!= nil {
		fmt.Println("ValidateSmsCode err:",err)
	}
	return &sms
}

func (md *MemberDao)QueryByPhone(phone string) *model.Member  {
	var memb model.Member
	_,err := md.Where("mobile=?",phone).Get(&memb)
	if err!=nil {
		fmt.Println("QueryByPhone err:",err)
	}
	return &memb
}

func (md *MemberDao)InsertMember(member *model.Member) int64 {
	result , err := md.InsertOne(member)
	if err != nil {
		fmt.Println("InsertMember err:",err)
		return 0
	}
	return result
}

func (md * MemberDao)InsertCode(sms model.SmsCode) int64 {
	result ,err := md.InsertOne(&sms)
	if err != nil {
		fmt.Println("")
	}
	return result
}

 

本文地址:https://blog.csdn.net/Batac_Lee/article/details/109626455

相关标签: go语言学习