Automate CRUD Code Generation for Java Projects – Boost Development Speed
This article introduces an open‑source tool that automatically generates all Java business code—including entity classes, DAO interfaces, SQL statements, service implementations, and controllers—for new database tables, dramatically reducing the hours spent on repetitive CRUD scaffolding.
Recently I needed to create more than 20 tables for a project, and each table required an entity class, CRUD interfaces, and SQL scripts, taking about two hours per table.
To avoid spending over 40 hours a week on this repetitive work, I built an open‑source tool that automatically generates all the necessary business code.
Demo
Enter the table name and its Chinese description, add fields (similar to typical DB tools), then click “Generate Code”. The tool instantly produces the SQL for table creation, CRUD SQL statements, the entity class, DAO interface, service interface and implementation, and a controller.
Copy the generated files into your project, adjust imports, and the basic CRUD operations are ready for the frontend.
General Thoughts
The tool works well for my own use, but to be useful for others it needs more flexibility because project structures and code details vary.
Therefore I added a code‑template configuration feature, allowing users to add, delete, and edit templates for each layer.
Design
The code generator works by matching and replacing dynamic parameters in user‑defined templates.
Dynamic parameters include table name, field names, comments, types, etc. Templates can be written for any language.
Example Template
/** $table_desc$Model model
* Created by Creator on $current_time$.
*/
public class $table_name_hump_A$Model extends ToString {
}When the table name is goods_order and description is “商品订单”, the result is:
/** 商品订单Model model
* Created by Creator on 2023-02-05 17:12:32.
*/
public class GoodsOrderModel extends ToString {
}CRUD SQL Template
CREATE TABLE `$table_name$` (
$create_table_field_list$
PRIMARY KEY (`$primary_key$`)
) ENGINE=$db_engine$ DEFAULT CHARSET=$db_encoded$;Entity Class Template
/** $table_desc$DTO model
* Created by Creator on $current_time$.
*/
public class $table_name_hump_A$DTO {
$member_param_list$
$get_set_method_list$
}DAO Interface Template
@Mapper
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);
Long pageQueryCount(Map param);
$table_name_hump_A$DO queryById(@Param("$primary_key_hump$") $primary_key_type_java$ $primary_key_hump$);
$table_name_hump_A$DO queryByIdLock(@Param("$primary_key_hump$") $primary_key_type_java$ $primary_key_hump$);
}Service Implementation Template
@Service
public class $table_name_hump_A$ServiceImpl implements $table_name_hump_A$Service {
@Resource
private $table_name_hump_A$DAO $table_name_hump$DAO;
public CommonResult create(JSONObject request) {
// validation, conversion, insert, return id
}
// modify, pageQuery, queryById methods …
}Controller Template
@RestController
@RequestMapping("/$table_name_hump$/")
public class $table_name_hump_A$Controller {
@Resource
private $table_name_hump_A$Service $table_name_hump$Service;
@RequestMapping(value="create.json", method={RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public Object create(@RequestBody JSONObject request) {
return CommonTemplate.run(LOGGER, new CommonTemplate() {
protected Object business() { return $table_name_hump$Service.create(request); }
}, request);
}
// modify, pageQuery, queryById …
}Dynamic Parameters
$table_name$– raw table name entered by the user. $table_name_hump$ – camel‑case, first letter lower (e.g., goodsOrder). $table_name_hump_A$ – camel‑case, first letter upper (e.g., GoodsOrder). $table_desc$ – table description. $field_name$, $field_name_hump$, $field_name_hump_A$ – field names in raw, lower‑camel, upper‑camel forms. $field_type_db$, $field_type_java$ – database and Java types. $primary_key$, $primary_key_hump$, $primary_key_hump_A$, $primary_key_type_java$ – primary‑key information. $insert_field_name_list$, $insert_field_value_list$, $update_field_list$, $select_field_list$, $where_field_list$, $create_table_field_list$ – generated field lists for SQL. $current_time$ – current timestamp. $java_type_adapter_assert_method$ – appropriate assertion method based on Java type.
Dynamic Code Blocks
Users can define reusable code snippets that are expanded for each field, such as member declarations, getter/setter methods, model‑conversion assignments, and required‑parameter checks.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
