配置
factroy: author: leijianming@163.com Time2Int: "true" JsonCamel: "true" pkgNow: gitee.com/ichub/gowebfactroy-v4/mysql/ pkgNew: gitee.com/ichub/gowebfactroy-v4/code/mysql/db/
下载工具
go get -u gitee.com/ichub/gowebfactroy-v4
go installl gitee.com/ichub/gowebfactroy-v4/cmd/gowebfactroy-v4
命令
gowebfactroy-v4 mysql -l
Usage:
gomenu mysql [flags]
gomenu mysql [command]
Available Commands:
list mysql list
table mysql tablename
tables mysql tables
Flags:
-h, --help help for mysql
执行
gowebfactroy-v4 mysql table employee
测试用例
func (this *TestEmployeeDaoSuite) Test006_Query() { logrus.Info("start Query ...") pageRequest := page.NewPageRequest(3,1) pageResult := this.Dao.Query(pageRequest) logrus.Info(pageRequest,pageResult) this.Equal(200, pageResult.Code) }
测试结果
{
"code": 200,
"msg": "成功",
"page_size": 3,
"current": 1,
"total": 643,
"data": [
{
"id": 1,
"department_id": 1,
"name": "中山之郎",
...
"work_id": "19902238",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"deleted_at": "0001-01-01T00:00:00Z"
},
{
"id": 2,
"department_id": 91,
...
"work_id": "10000002",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"deleted_at": "0001-01-01T00:00:00Z"
},
{
"id": 3,
"depa
"email": "zhao@qq.com",
"phone": "15698887795",
"address": "陕西
"specialty": "电子工程",
...
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"deleted_at": "0001-01-01T00:00:00Z"
}
]
}
生成代码
var InstEmployeeDao = NewEmployeeDao()
type EmployeeDao struct {
basedto.BaseEntitySingle `gorm:"-"`
*generaldao.BaseDao[int32, model.Employee]
}
func NewEmployeeDao() *EmployeeDao {
var d = &EmployeeDao{
BaseDao: generaldao.NewBaseDao[int32, model.Employee](),
}
d.InitProxy(d)
return d
}
func (this *EmployeeDao) GetDB() *gorm.DB {
return ichubcontext.FindBeanIchubClientFactroy().GetDBMysql()
}
接口
type GeneralDaoIface[P int32 | int64 | string, E IBaseModel[P]] interface { Insert(entity *E) (P, error) DeleteById(pkey P) error Save(entity *E) (P, error) Update(entity *E) (P, error) UpdateNotNull(pkey P, maps map[string]any) (P, error) UpdateMap(pkey P, maps map[string]interface{}) (P, error) FindById(pkey P) (entity *E, found bool, err error) FindByIds(pks string) (*[]E, error) Query() *page.PageResult QueryModel() *pagemodel.PageResult[E] Count() (int, error) GetDB() *gorm.DB }
接口实现
type BaseDao[P int32 | int64 | string, E generaliface.IBaseModel[P]] struct { *page.PageRequest } func NewBaseDao[P int32 | int64 | string, E generaliface.IBaseModel[P]]() *BaseDao[P, E] { var dao = &BaseDao[P, E]{ PageRequest: page.FindBeanPageRequest(), } dao.FuncGetDb = dao.GetDB return dao } func (this *BaseDao[P, E]) GetDB() *gorm.DB { return ichubcontext.FindBeanIchubClientFactroy().GetDB() } func (this *BaseDao[P, E]) Insert(entity *E) (P, error) { err := this.GetDB().Model(entity).Create(entity).Error if err != nil { logrus.Error(err.Error()) return (*entity).PkeyValue(), err } return (*entity).PkeyValue(), err } func (this *BaseDao[P, E]) DeleteById(pkey P) error { var entity E err := this.GetDB().Where(entity.PkeyName()+"=?", pkey).Delete(&entity).Error return err } func (this *BaseDao[P, E]) Save(entity *E) (P, error) { err := this.GetDB().Model(entity).Where((*entity).PkeyName()+"=?", (*entity).PkeyValue()). Save(entity).Error if err != nil { logrus.Error(err.Error()) return (*entity).PkeyValue(), err } return (*entity).PkeyValue(), err } func (this *BaseDao[P, E]) Update(entity *E) (P, error) { err := this.GetDB().Model(entity).Where((*entity).PkeyName()+"=?", (*entity).PkeyValue()).Updates(entity).Error if err != nil { logrus.Error(err.Error()) return (*entity).PkeyValue(), err } return (*entity).PkeyValue(), err } func (this *BaseDao[P, E]) UpdateNotNull(pkeyValue P, maps map[string]interface{}) (P, error) { var entity = new(E) err := this.GetDB().Model(entity).Where((*entity).PkeyName()+"=?", pkeyValue).Updates(maps).Error if err != nil { logrus.Error(err.Error()) return (*entity).PkeyValue(), err } return pkeyValue, err } func (this *BaseDao[P, E]) UpdateMap(pkey P, maps map[string]interface{}) (P, error) { return this.UpdateNotNull(pkey, maps) } func (this *BaseDao[P, E]) FindById(pkey P) (*E, bool, error) { var entity = new(E) db := this.GetDB().First(entity, pkey) baseutils.IfProxy(entity) return entity, db.RecordNotFound(), db.Error } func (this *BaseDao[P, E]) FindByIds(pks string) (*[]E, error) { var entity = new(E) this.PageRequest.In((*entity).PkeyName(), []any{pks}) var ret = this.QueryModel() var err error = nil if !ret.IsSuccess() { err = errors.New(ret.Msg) } return &ret.Data, err } func (this *BaseDao[P, E]) QueryModel() *pagemodel.PageResult[E] { var pageresult = this.Query() var result = pagemodel.DefaultResult[E]() return result.ValueOfPageResult(pageresult) } func (this *BaseDao[P, E]) Query() *page.PageResult { return this.PageRequest.Query(new(E), &[]E{}) } func (this *BaseDao[P, E]) Count() (int, error) { return this.PageRequest.Count(new(E)) } func (this *BaseDao[E, P]) formatTime(sec int64) string { timeLayout := "2006-01-02 15:04:05" datetime := time.Unix(sec, 0).Format(timeLayout) return datetime } func (this *BaseDao[P, E]) formatDate(sec int64) string { timeLayout := "2006-01-02" datetime := time.Unix(sec, 0).Format(timeLayout) return datetime } func (this *BaseDao[E, P]) localTimeFormat(ptime time.Time) string { timeLayout := "2006-01-02 15:04:05" return ptime.Format(timeLayout) } func (this *BaseDao[E, P]) localTimeUTCFormat(ptime time.Time) string { timeLayout := "2006-01-02 15:04:05" return ptime.UTC().Format(timeLayout) } func (this *BaseDao[E, P]) localDateFormat(localDateInt basemodel.LocalDateInt) string { timeLayout := "2006-01-02" datetime := localDateInt.Time.Format(timeLayout) return datetime } func (this *BaseDao[E, P]) localDatetimeFormat(localTimeInt basemodel.LocalTimeInt) string { timeLayout := "2006-01-02 15:04:05" datetime := localTimeInt.Time.Format(timeLayout) return datetime } func (this *BaseDao[E, P]) GetDBMysql() *gorm.DB { return ichubcontext.FindBeanIchubClientFactroy().GetDBMysql() } func (this *BaseDao[E, P]) GetDBMeidusa() *gorm.DB { return ichubcontext.FindBeanIchubClientFactroy().GetDBMeidusa() } func (this *BaseDao[E, P]) GetDBCockdb() *gorm.DB { return ichubcontext.FindBeanIchubClientFactroy().GetDB() }