[!TIP] This document is machine-translated by Google. If you find grammatical and semantic errors, and the document description is not clear, please PR
First, after downloading the demo project, we will use the user's model to demonstrate the code generation.
Model is a bridge for services to access the persistent data layer. The persistent data of the business often exists in databases such as mysql and mongo. We all know that the operation of a database is nothing more than CURD. And these tasks will also take up part of the time for development. I once wrote 40 model files when writing a business. According to the complexity of different business requirements, on average, each model file is almost required. 10 minutes, for 40 files, 400 minutes of working time, almost a day's workload, and the goctl tool can complete the 400 minutes of work in 10 seconds.
Enter the demo project
book, find the
user.sql file under
user/model, and execute the table creation in your own database.
Code generation (with cache)
The way one(ddl)
service/user/model directory and execute the command
$ cd service/user/model $ goctl model mysql ddl -src user.sql -dir . -c
The way two(datasource)
$ goctl model mysql datasource -url="$datasource" -table="user" -c -dir .
$datasourceis the database connection address
The way three(intellij plugin)
In Goland, right-click
user.sql, enter and click
Model Code to generate it, or open the
Enter the editing area, use the shortcut key
Command+N (for macOS) or
alt+insert (for windows), select
[!TIP] The intellij plug-in generation needs to install the goctl plug-in, see intellij plugin for details
Verify the generated model file
. ├── user.sql ├── usermodel.go └── vars.go