Database CRUD Operations
5/14/25About 1 mincommonCRUDcode generation
Generate DAO CRUD Code
Generate Code
In the sponge UI interface:
- Click the left menu bar [Public] → [Generate DAO CRUD Code];
- Select the database (e.g., mysql), fill in the
database dsn
, then click theGet Table Name
button and select the table names (multiple selections allowed); - Fill in other parameters. Hovering the mouse over the question mark
?
position allows you to view the parameter descriptions.
After filling in the parameters, click the Download Code
button to generate the DAO code, as shown in the figure below:

Equivalent Command
# Full command
sponge web dao --module-name=user --db-driver=mysql --db-dsn="root:123456@(192.168.3.37:3306)/school" --db-table=teach** <br>
# Simplified command (use --out to specify the service code directory, the generated code will be automatically merged into the specified service directory)
sponge web dao --db-driver=mysql --db-dsn="root:123456@(192.168.3.37:3306)/school" --db-table=teach --out=user
Code Directory Structure
The generated DAO CRUD code includes the following directories:
.
└─ internal
├─ cache # Cache related code
├─ dao # Data Access Object
└─ model # Data Model
Code Integration Instructions
Integration Steps
- Unzip the generated code package
- Move the
internal
directory to the root directory of the target service (web or gRPC) code
Notes
- If a conflict prompt appears, it means DAO code has already been generated for the same table and can be safely overwritten.
DAO CRUD Code Usage
Web Service Invocation Method
File Location: internal/handler/
- Services created based on SQL:
<table_name>.go
- Services created based on Protobuf:
<proto_file_name>.go
Reference Example:
type userHandler struct {
userDao dao.UserDao // Inject DAO interface
}
func NewUserHandler() UserHandler {
return &userHandler{
userDao: dao.NewUserDao(database.GetDB(), nil) // Initalize DAO interface
}
}
// Call CRUD method to manipulate database.
gRPC Service Invocation Method
File Location: internal/service/
- Services created based on SQL:
table_name.go
- Services created based on Protobuf:
proto_file_name.go
Reference Example:
type user struct {
userV1.UnimplementedUserServer
userDao dao.UserDao // Inject DAO
}
func NewUserServer() userV1.UserServer {
return &user{
userDao: dao.NewUserDao(database.GetDB(), nil) // Initalize DAO
}
}
// Call CRUD method to manipulate database.