Automatic Business Code Generation Tool and Its Usage
This article introduces a self‑built tool that automatically generates database, service, DAO, and controller code for CRUD operations based on table definitions, explains its underlying template‑and‑parameter mechanism, demonstrates usage with a sample product table, and provides customizable templates and dynamic parameters for extensibility.
Background
The author needed to create more than 20 database tables for a project and found the manual process of writing entity classes, DAO interfaces, and CRUD SQL extremely time‑consuming, estimating about two hours per table.
Tool Overview
After five nights of development, a web‑based utility (https://utilsbox.cn/) was created that can generate all required code with a single click. Users input the table name, its Chinese description, and field definitions, then the tool produces the SQL, entity classes, DAO, service, and controller code.
Demo
A demonstration shows creating a "goods_order" table, filling in fields, and clicking “One‑click generate code”. The generated output includes:
/**
* 商品订单Model模型
* Created by 创建人 on 2023-02-05 17:12:32.
*/
public class GoodsOrderModel extends ToString {
}Similar snippets are generated for the DTO, DAO interface, MyBatis XML mapper, service implementation, and REST controller.
Generic Considerations
Because project structures differ, the tool allows users to configure code templates. Templates can be added, edited, or removed through a “Template Configuration” dialog, making the generator adaptable to various architectures.
Generation Principle
The engine works by matching dynamic parameters (e.g., $table_name$ , $table_desc$ , $table_name_hump_A$ ) with user‑provided values and replacing placeholders in user‑defined templates. This approach works for any programming language.
Default Templates (excerpt)
CREATE TABLE `$table_name$` (
$create_table_field_list$
PRIMARY KEY (`$primary_key$`)
) ENGINE=$db_engine$ DEFAULT CHARSET=$db_encoded$; public class $table_name_hump_A$DTO {
$member_param_list$
$get_set_method_list$
} public interface $table_name_hump_A$DAO {
int insert($table_name_hump_A$DO data);
int update($table_name_hump_A$DO data);
List<$table_name_hump_A$DO> pageQuery(Map param);
// ... other methods
}Dynamic Parameters
The documentation lists all supported placeholders, such as table name, camel‑cased variants, field names, data types, current time, and helper parameters for building field lists and WHERE clauses.
Dynamic Code Blocks
Users can define reusable code blocks for member variables, getter/setter methods, model converters, and required‑field validation. The tool expands these blocks automatically for each field.
Open Source
The utility is open‑source on GitHub: https://github.com/GooseCoding/utilsbox .
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.