5分钟轻松搞定:网易云QQ音乐歌词批量提取与格式转换全攻略
2026/6/8 4:20:14
domain目录是 goweb3 项目的业务领域层,包含业务逻辑、数据访问、Web 控制器等核心组件,采用分层架构设计。
plainText
domain/ ├── Init.go # 领域层初始化入口 ├── apiutils/ # API工具函数 │ └── apiutils.go ├── apiconsts/ # API常量定义 │ └── apiconsts.go ├── apifacade/ # API门面(对外统一入口) │ └── 1.go ├── baseweb/ # Web基础组件 │ ├── BaseController.go # 控制器基类 │ ├── BaseWeb.go # Web基础类 │ └── base_controller_init.go ├── baseservice/ # 服务基础组件 │ └── BaseService.go # 服务基类 ├── db/ # 数据库领域 │ ├── InitDb.go # 数据库初始化 │ ├── dbentity/ # 数据库实体 │ │ ├── learnentity/ # 学习实体 │ │ ├── sys_menu.go │ │ ├── t_sys_user.go │ │ └── ... │ ├── dbdao/ # 数据访问对象 │ │ ├── users_dao.go │ │ ├── t_sys_user_dao.go │ │ └── ... │ ├── dbdto/ # 数据传输对象 │ │ ├── ui_student.go │ │ ├── TrialReservation.go │ │ └── ... │ ├── dbfacade/ # 数据库门面 │ │ ├── userfacade.go │ │ └── ... │ ├── dbiface/ # 数据库接口 │ │ └── migrate.go │ └── uipage/ # UI分页查询 │ ├── user_request.go │ ├── uiquery_student.go │ └── ... ├── es/ # Elasticsearch领域 │ ├── InitEs.go # ES初始化 │ ├── esentity/ # ES实体 │ │ ├── service_website.go │ │ ├── sys_dept_es.go │ │ └── ... │ └── esservice/ # ES服务 │ ├── service_perform_month_es.go │ └── ... ├── internal/ # 内部模块 │ ├── db/ # 内部数据库模块 │ │ ├── web/ # Web控制器 │ │ ├── gorpc/ # gRPC服务 │ │ └── test/ # 测试用例 │ └── es/ # 内部ES模块 │ └── basedao/ # ES基础DAO └── pageui/ # UI页面模块 ├── ui dto/ # UI数据传输对象 ├── uirequest/ # UI请求定义 ├── uiexample/ # UI示例 ├── uiclickhouse/ # ClickHouse UI查询 └── ...domain/Init.go- 领域层统一初始化
go
func Init() error { golog.Info("init db & es") // 初始化数据库 var err = db.InitDb() if err != nil { return err } // 初始化Elasticsearch return es.InitEs().Result2Error() }db/InitDb.go- 数据库初始化
go
func InitDb() error { golog.Info("init db") ichubconfig.FindBeanIchubConfig().CheckSoftwareEnv() initCommon() return dbiface.AutoMigrate() }es/InitEs.go- ES初始化
go
func InitEs() *basedto.IchubResult { var ret = esentity.NewWebFacadeServiceWebsite().CreateIndexIfNotExist() if ret != nil { return ret } es.NewWebFacadeServicePerformMonthEs().CreateIndexIfNotExist() return esservice.NewWebFacadeServicePerformMonthEs().CreateIndexesIfNotExist() }baseweb/BaseController.go- Web控制器基类
go
type BaseController struct { basedto.BaseEntitySingle BaseWeb } // 参数获取方法 func (self *BaseController) GetParam2Int64(c *gin.Context, key string) int64 func (self *BaseController) GetParam2Str(c *gin.Context, key string) string func (self *BaseController) GetQuery2Int64(c *gin.Context, key string) int64 func (self *BaseController) GetQuery2Bool(c *gin.Context, key string) boolbaseweb/BaseWeb.go- Web基础类
go
type BaseWeb struct { baseservice.BaseService }职责:
BaseService获得业务能力baseservice/BaseService.go- 服务基类
go
type BaseService struct {} // 请求解析 func (self *BaseService) ParseBody(ctx *gin.Context, req any) error func (self *BaseService) ParseBodyOnly(ctx *gin.Context, dto any) error // 缓存操作 func (self *BaseService) GetCache(key string) (any, bool) func (self *BaseService) SetCache(key string, v any) func (self *BaseService) SetCache5M(key string, v any) func (self *BaseService) SetCache2Hour(key string, v any) // 日期处理 func (self *BaseService) TimeBetween2DateOnly(start, end time.Time) (time.Time, time.Time) func (self *BaseService) TimeMonthFirstLastDay(day time.Time) (time.Time, time.Time) func (self *BaseService) Datetime2DateStrTodayYesterday(day time.Time) (start, end string) // 响应处理 func (self *BaseService) ResFailUserMsg(c *gin.Context, msg string) func (self *BaseService) ResFailSystemMsg(c *gin.Context, msg string) // SQL执行 func (self *BaseService) ExecSql(sql string, params ...any) error职责:
| 文件 | 说明 |
|---|---|
sys_menu.go | 系统菜单实体 |
t_sys_user.go | 系统用户实体 |
train_group_rest.go | 培训组休息实体 |
learnentity/users.go | 学习用户实体 |
| 文件 | 说明 |
|---|---|
users_dao.go | 用户数据访问 |
t_sys_user_dao.go | 系统用户DAO |
train_group_rest_dao.go | 培训组DAO |
| 文件 | 说明 |
|---|---|
ui_student.go | 学生UI请求DTO |
TrialReservation.go | 试用预约DTO |
TrialTrain.go | 试用培训DTO |
go
// userfacade.go - 用户业务门面 type UserFacade struct { // 聚合用户相关业务 }go
// uiquery_student.go - 学生分页查询 type UiQueryStudent struct { *pagedao.PageDao[int64, *Student] }| 文件 | 说明 |
|---|---|
service_website.go | 网站服务ES实体 |
sys_dept_es.go | 系统部门ES实体 |
service_domain_es.go | 域名服务ES实体 |
es_contact_person.go | 联系人ES实体 |
| 文件 | 说明 |
|---|---|
service_perform_month_es.go | 月度绩效ES服务 |
example_service.go | 示例服务 |
plainText
internal/ ├── db/ │ ├── web/ # Web控制器 │ │ ├── service_api_list_web.go │ │ └── coo_permission_web.go │ ├── gorpc/ # gRPC服务 │ │ ├── proto/ # 协议定义 │ │ └── service/ # RPC服务实现 │ └── test/ # 测试用例 │ ├── dao/ # DAO测试 │ └── service/ # 服务测试 └── es/ ├── basedao/ # ES基础DAO └── esmodel/ # ES模型| 子目录 | 说明 |
|---|---|
uidto/ | UI数据传输对象 |
uirequest/ | UI请求定义 |
uiexample/ | UI示例代码 |
uiclickhouse/ | ClickHouse查询UI |
plainText
┌─────────────────────────────────────────────────────────────────┐ │ Domain 领域层 │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ baseweb │ │ baseservice │ │ apiconsts │ │ │ │ (控制器基类) │ │ (服务基类) │ │ (API常量) │ │ │ └──────┬───────┘ └──────┬───────┘ └──────────────┘ │ │ │ │ │ │ ▼ ▼ │ │ ┌─────────────────────────────────────┐ │ │ │ 业务服务层 │ │ │ │ db/dbdao │ es/esservice │ api │ │ │ └─────────────────────────────────────┘ │ │ │ │ │ │ ▼ ▼ │ │ ┌──────────────┐ ┌──────────────┐ │ │ │ dbentity │ │ esentity │ │ │ │ (数据库实体) │ │ (ES实体) │ │ │ └──────────────┘ └──────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────┘| 层级 | 职责 | 核心文件 |
|---|---|---|
| Controller | 处理HTTP请求,参数校验,响应封装 | BaseController.go |
| Service | 业务逻辑处理,事务管理,数据转换 | BaseService.go |
| DAO | 数据库访问,CRUD操作 | users_dao.go |
| Entity | 数据实体定义,映射数据库表 | sys_menu.go,t_sys_user.go |
| DTO | 数据传输对象,请求/响应结构 | ui_student.go |
| Facade | 业务门面,聚合多个服务 | userfacade.go |
| UIQuery | 分页查询封装 | uiquery_student.go |
plainText
main.go ↓ domain.Init() ↓ ├── db.InitDb() │ ↓ │ ├── CheckSoftwareEnv() # 检查环境配置 │ ├── initCommon() # 初始化公共实体 │ └── dbiface.AutoMigrate() # 自动迁移数据库表 │ └── es.InitEs() ↓ ├── CreateIndexIfNotExist() # 创建ES索引(如不存在) └── CreateIndexesIfNotExist() # 批量创建索引go
type UserController struct { baseweb.BaseController } func NewUserController() *UserController { return &UserController{} } func (self *UserController) RegisterRoute(api *gin.RouterGroup) { api.GET("/users/:id", self.GetUser) api.POST("/users", self.CreateUser) } func (self *UserController) GetUser(c *gin.Context) { userId := self.GetParam2Int64Check(c, "id") user := self.GetUserById(userId) c.JSON(200, user) }go
type UserService struct { baseservice.BaseService } func NewUserService() *UserService { return &UserService{} } func (self *UserService) GetUserById(userId int64) *User { // 业务逻辑处理 cacheKey := fmt.Sprintf("user:%d", userId) if user, exist := self.GetCache(cacheKey); exist { return user.(*User) } // 查询数据库 user := self.queryUser(userId) // 缓存结果 self.SetCache(cacheKey, user) return user }| 特点 | 说明 |
|---|---|
| 分层架构 | Controller → Service → DAO → Entity |
| 基类封装 | 提供通用能力,减少重复代码 |
| 依赖注入 | 通过FindBeanXxx()获取组件实例 |
| 自动迁移 | 数据库表结构自动同步 |
| 多数据源 | 支持MySQL、PostgreSQL、ClickHouse |
| ES集成 | 完整的Elasticsearch操作封装 |
| 缓存支持 | 内置Redis缓存管理 |
| 目录 | 职责 | 状态 |
|---|---|---|
baseweb/ | Web控制器基础能力 | 核心 |
baseservice/ | 服务层基础能力 | 核心 |
db/dbentity/ | 数据库实体定义 | 业务 |
db/dbdao/ | 数据访问对象 | 业务 |
db/dbdto/ | 数据传输对象 | 业务 |
db/dbfacade/ | 业务门面聚合 | 业务 |
db/uipage/ | UI分页查询 | 业务 |
es/esentity/ | ES实体定义 | 业务 |
es/esservice/ | ES服务层 | 业务 |
internal/ | 内部模块(不对外暴露) | 内部 |
pageui/ | UI页面相关 | 业务 |