怎样优化网站 优帮云,推几个学习网站,网上黑赌网站如何做代理,网络营销常见的工具背景
上一章 Go语言开发基于SQLite数据库实现用户表新增接口(二) 这一章我们实现用户表的查询详情接口
代码实现
mapper层
type UserMapper interface {GetById(id uint64) (*model.User, error)}type userMapper struct {
}func (m *userMapper) GetById(id uint64) (*mod…背景
上一章 Go语言开发基于SQLite数据库实现用户表新增接口(二) 这一章我们实现用户表的查询详情接口
代码实现
mapper层
type UserMapper interface {GetById(id uint64) (*model.User, error)}type userMapper struct {
}func (m *userMapper) GetById(id uint64) (*model.User, error) {// 创建 model.User 的指针实例user : model.User{}result, err : model.GetById(user.TableName(), id, user)if err ! nil {return nil, err}// 确保返回的结果是 *model.User 类型user, ok : result.(*model.User)if !ok {return nil, fmt.Errorf(expected *model.User, but got %T, result)}return user, nil
}
ModelTable实现
func GetById(tableName string, id uint64, dest interface{}) (interface{}, error) {// 确保 dest 是指针类型val : reflect.ValueOf(dest)if val.Kind() ! reflect.Ptr || val.IsNil() {return nil, fmt.Errorf(dest must be a non-nil pointer)}// 构造 SQL 查询query : fmt.Sprintf(SELECT * FROM %s WHERE id ? LIMIT 1, tableName)// 执行查询row : config.Db.QueryRow(query, id)// 获取结构体的字段fieldPointers, err : getFieldPointers(reflect.TypeOf(dest).Elem(), reflect.ValueOf(dest).Elem())if err ! nil {return nil, err}// 使用 Scan 将查询结果映射到结构体err row.Scan(fieldPointers...)if err ! nil {if err sql.ErrNoRows {return nil, fmt.Errorf(No rows found with id %d, id)}return nil, err}return dest, nil
}// 递归地获取结构体字段的指针支持嵌套结构体
func getFieldPointers(t reflect.Type, v reflect.Value) ([]interface{}, error) {var fieldPointers []interface{}for i : 0; i t.NumField(); i {field : t.Field(i)fieldValue : v.Field(i)// 如果字段是结构体类型递归进入if fieldValue.Kind() reflect.Struct {// 递归处理嵌套的结构体字段nestedPointers, err : getFieldPointers(field.Type, fieldValue)if err ! nil {return nil, err}fieldPointers append(fieldPointers, nestedPointers...)} else {// 否则将字段的指针添加到字段指针数组fieldPointers append(fieldPointers, fieldValue.Addr().Interface())}}return fieldPointers, nil
}这里我们映射的结构体中的对应db字段 这样的好处是我们这个方法是基础方法其他结构体中只要有ModelTable结构体我们就可以直接继续使用了。
service层
type UserService interface {GetById(id uint64) (*model.User, error)}service实现层
type UserServiceImpl struct {m mapper.UserMapper
}func (u UserServiceImpl) GetById(id uint64) (*model.User, error) {return u.m.GetById(id)
}
controller层
type UserController struct {UserService service.UserService
}func (u *UserController) GetById(rc *req.Ctx) {id : uint64(req.PathParamInt(rc.GinCtx, id))user, err : u.UserService.GetById(id)assert.IsNil(err)rc.ResData user
}
这里有个PathParamInt方法 获取路径上面的参数
func PathParamInt(g *gin.Context, pm string) int {value, _ : strconv.Atoi(g.Param(pm))return value
}接口 测试 源码地址
使用Go语言开发基于SQLite数据库实现用户表相关接口项目示例可进行扩展拿来即用
参考
代码风格参考的mayfly-go SQLite这块是自己实现的 文章转载自: http://www.morning.sqqpb.cn.gov.cn.sqqpb.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.qyglt.cn.gov.cn.qyglt.cn http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.txzmy.cn.gov.cn.txzmy.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.pskjm.cn.gov.cn.pskjm.cn http://www.morning.pwwdp.cn.gov.cn.pwwdp.cn http://www.morning.yrms.cn.gov.cn.yrms.cn http://www.morning.fqqlq.cn.gov.cn.fqqlq.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.gwxsk.cn.gov.cn.gwxsk.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.tsynj.cn.gov.cn.tsynj.cn http://www.morning.qtryb.cn.gov.cn.qtryb.cn http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.kkhf.cn.gov.cn.kkhf.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.lhhdy.cn.gov.cn.lhhdy.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.bnmrp.cn.gov.cn.bnmrp.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.rccbt.cn.gov.cn.rccbt.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.kpzbf.cn.gov.cn.kpzbf.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.pzlhq.cn.gov.cn.pzlhq.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.sbwr.cn.gov.cn.sbwr.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.xnpj.cn.gov.cn.xnpj.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.cmcjp.cn.gov.cn.cmcjp.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.ypbp.cn.gov.cn.ypbp.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.xyjlh.cn.gov.cn.xyjlh.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.knpmj.cn.gov.cn.knpmj.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.cwtrl.cn.gov.cn.cwtrl.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.ncrk.cn.gov.cn.ncrk.cn http://www.morning.kpcky.cn.gov.cn.kpcky.cn http://www.morning.nqgff.cn.gov.cn.nqgff.cn http://www.morning.rnmyw.cn.gov.cn.rnmyw.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.mlntx.cn.gov.cn.mlntx.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.nccyc.cn.gov.cn.nccyc.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn