Skip to content

MySQL

go-zero uses goctl to generate type-safe, zero-reflection data access code from a DDL schema.

CREATE TABLE `user` (
`id` bigint NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL DEFAULT '',
`password` varchar(255) NOT NULL DEFAULT '',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_username` (`username`)
) ENGINE=InnoDB;
Terminal window
goctl model mysql ddl -src user.sql -dir ./internal/model
DataSource: "root:password@tcp(127.0.0.1:3306)/dbname?parseTime=true"
internal/svc/servicecontext.go
func NewServiceContext(c config.Config) *ServiceContext {
sqlConn := sqlx.NewMysql(c.DataSource)
return &ServiceContext{
Config: c,
UserModel: model.NewUserModel(sqlConn),
}
}
result, err := l.svcCtx.UserModel.Insert(l.ctx, &model.User{
Username: req.Username,
Password: hashPassword(req.Password),
})
err = l.svcCtx.UserModel.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
if _, err := insertUser(ctx, session, user); err != nil {
return err
}
return insertProfile(ctx, session, profile)
})