Automate CRUD Code Generation for Spring Boot: Build 20+ Tables in Hours

This article introduces a Spring Boot code‑generation tool that automatically creates database tables, entity classes, DAO interfaces, service layers and controllers, turning weeks of manual CRUD development for dozens of tables into a matter of minutes, and explains its design, templates, dynamic parameters and usage.

macrozheng
macrozheng
macrozheng
Automate CRUD Code Generation for Spring Boot: Build 20+ Tables in Hours

Origin

When working on a project that required more than 20 new tables, the author realized that manually creating each table, its entity class, CRUD interfaces and SQL took about two hours per table, consuming a whole week.

To avoid this tedious work, a tool was created to automatically generate all related code.

The tool is open‑source at https://github.com/GooseCoding/utilsbox.

Demo

The demo shows creating a product table: fill in the table name and Chinese description, add fields, then click “Generate Code”. The tool instantly produces the SQL for creating the table, the entity class, DAO interface, service implementation, controller and conversion classes.

Generated code can be copied into the project and used directly.

The tool also supports importing an existing CREATE TABLE statement: paste the SQL, the tool parses it, you only need to provide the Chinese name and click “Generate”.

General Thoughts

The author acknowledges that the current templates may not fit every project's structure, so a template configuration feature was added, allowing users to add, delete or edit code templates.

Design

Code generation works by combining user‑provided dynamic parameters (table name, field names, types, etc.) with user‑defined templates. During generation the tool matches placeholders like $table_name$ and replaces them with actual values.

In theory any language can be supported by defining appropriate templates.

Code Template Reference

Several default templates are provided, such as:

Table 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 创建人 on $current_time$.
 */
public class $table_name_hump_A$DO {
    $member_param_list$
    $get_set_method_list$
}

DAO Interface Template

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;

@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$);
}

CRUD XML Template

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.$table_name_hump_A$DAO">
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO $table_name$($insert_field_name_list$) VALUES ($insert_field_value_list$);
    </insert>
    <update id="update">
        UPDATE $table_name$ SET $update_field_list$ WHERE $primary_key$ = #{ $primary_key_hump$ };
    </update>
    <select id="pageQuery" resultType="com.xxx.$table_name_hump_A$DO">
        SELECT $select_field_list$ FROM $table_name$ WHERE 1=1 $where_field_list$
        ORDER BY $primary_key$ DESC
        <if test="pageIndex != null and pageSize != null">
            LIMIT #{offset},#{rows}
        </if>
    </select>
    <select id="pageQueryCount" resultType="java.lang.Long">
        SELECT COUNT(1) as total FROM $table_name$ WHERE 1=1 $where_field_list$
    </select>
    <select id="queryById" resultType="com.xxx.$table_name_hump_A$DO">
        SELECT $select_field_list$ FROM $table_name$ WHERE $primary_key$ = #{ $primary_key_hump$ };
    </select>
    <select id="queryByIdLock" resultType="com.xxx.$table_name_hump_A$DO">
        SELECT $select_field_list$ FROM $table_name$ WHERE $primary_key$ = #{ $primary_key_hump$ } FOR UPDATE;
    </select>
</mapper>

Dynamic Parameters

Various placeholders are defined, such as $table_name$ (raw table name), $table_name_hump$ (camelCase), $table_name_hump_A$ (PascalCase), $field_type_java$ (mapped Java type), $primary_key$, $current_time$, etc.

Dynamic Code Blocks

Users can define custom blocks that use the placeholders, for example a member variable list:

/** $field_comment$ */
private $field_type_java$ $field_name_hump$;

The tool expands the block for each field, generating getters, setters, converters and validation code automatically.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaCode GenerationSpring BootCRUD
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.