Automate Java CRUD Code Generation: One-Click Tool for Fast Backend Development

The article introduces a self‑built utility that automatically generates Java backend code—including entity classes, DAO interfaces, service implementations, and controllers—based on database table definitions, dramatically reducing the manual effort of creating dozens of tables and their associated CRUD logic.

Architect's Guide
Architect's Guide
Architect's Guide
Automate Java CRUD Code Generation: One-Click Tool for Fast Backend Development

Why the Tool Was Created

Developing a project that requires more than 20 database tables can be tedious because each table normally demands a corresponding entity class, CRUD interfaces, and SQL statements, consuming about two hours per table.

To avoid spending over 40 hours on such repetitive work, the author built an automatic code‑generation tool after five nights of intensive development.

Demo of the Tool

1. Enter the table name and its Chinese description. 2. Add the fields for the table, similar to using a regular database UI. 3. Click “One‑Click Generate Code”.

The tool then produces the SQL for table creation, CRUD SQL, the entity class, DB layer interface, service layer interface and implementation, and the controller layer code.

After copying the generated files into a project and adding the necessary imports, the basic CRUD functionality is ready for front‑end consumption.

General‑Purpose Considerations

The author acknowledges that the default templates may not fit every project's architecture, so a template‑configuration feature was added, allowing users to add, delete, or edit code templates for different layers.

How the Code Generation Works

The engine combines user‑defined code templates with dynamic parameters. Dynamic parameters represent values such as table name, field names, and timestamps. During generation, the tool replaces placeholders (e.g., $table_name$, $table_name_hump_A$) with the actual values supplied by the user.

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

/**
 * $table_desc$Model模型
 * Created by 创建人 on $current_time$.
 */
public class $table_name_hump_A$Model extends ToString {
}

The template uses placeholders like $table_desc$ (table description), $current_time$ (generation time), and $table_name_hump_A$ (table name in PascalCase).

Default Code Templates

Below are some of the default templates provided by the tool.

SQL for Table Creation

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

DAO Interface (MyBatis)

@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 Mapper

<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 $while_field_list$
        ORDER BY $primary_key$ DESC
        <if test="pageIndex != null and pageSize != null">
            LIMIT #{offset},#{rows}
        </if>
    </select>
    ...
</mapper>

Model Converter Template

public class $table_name_hump_A$Converter {
    public static $table_name_hump_A$DO toDo($table_name_hump_A$DTO source) {
        $table_name_hump_A$DO target = new $table_name_hump_A$DO();
        $converter_source_to_target_params_list$
        return target;
    }
    public static $table_name_hump_A$DTO toDto($table_name_hump_A$DO source) {
        $table_name_hump_A$DTO target = new $table_name_hump_A$DTO();
        $converter_source_to_target_params_list$
        return target;
    }
    public static List<$table_name_hump_A$DTO> toDtoList(List<$table_name_hump_A$DO> data) {
        if (CollectionUtils.isEmpty(data)) return null;
        List<$table_name_hump_A$DTO> list = new ArrayList<>();
        for ($table_name_hump_A$DO item : data) {
            list.add($table_name_hump_A$Converter.toDto(item));
        }
        return list;
    }
}

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, DAO call, return result
    }
    // modify, pageQuery, queryById methods omitted for brevity
}

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})
    public Object create(@RequestBody JSONObject request) {
        return $table_name_hump$Service.create(request);
    }
    // other endpoints omitted for brevity
}

Dynamic Parameters

The tool defines a set of placeholders (e.g., $table_name$, $table_name_hump$, $field_name$, $field_type_java$, $primary_key$, $current_time$) that are automatically replaced with user‑provided values during generation.

Dynamic Code Blocks

Users can define reusable code blocks such as member variable lists, getter/setter methods, converter logic, and required‑field validation. The tool expands these blocks for each field in the table.

Open‑Source Repository

The utility is open source and can be found at https://github.com/GooseCoding/utilsbox .

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.

JavaMyBatisCRUDTemplate EngineBackend automation
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

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.