mybatis-plus generator自动生成代码

1. 实现代码

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.ArrayList;
import java.util.Collections;

public class CodeGenerator {

    public static void main(String[] args) {
        //设置自动填充配置
        Column gmt_create = new Column("create_time", FieldFill.INSERT_UPDATE);
        Column gmt_modified = new Column("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<IFill> tableFills=new ArrayList<>();
        tableFills.add(gmt_create);
        tableFills.add(gmt_modified);

        //自定义配置
//        CustomFile mapperXmlBuild = new CustomFile.Builder().formatNameFunction(table -> table.getEntityName())
//                .fileName("Mapper.xml")
//                .templatePath("/templates/mapper.xml.ftl")
//                .enableFileOverride()
//                .filePath("src/main/resources/mappers")
//                .build();


        // 使用 FastAutoGenerator 快速配置代码生成器
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/wotou?serverTimezone=GMT%2B8", "root", "1234")
                .globalConfig(builder -> builder.author("dwl") // 设置作者
                        .outputDir("src/main/java") // 输出目录
                        .disableOpenDir())
                .packageConfig(builder -> {
                    builder.parent("com.dwl") // 设置父包名
                            .entity("pojo.po") // 设置实体类包名
                            .mapper("mapper") // 设置 Mapper 接口包名
                            .service("service") // 设置 Service 接口包名
                            .serviceImpl("service.impl") //实现类包名
                            .xml("mapper") //mapper
                            .pathInfo(Collections.singletonMap(OutputFile.xml,"src/main/resources/mapper")) //定义mapper文件的路径
                    ;
                })
                .strategyConfig(builder -> {
                    builder.addInclude("user", "User") // 设置需要生成的表名
                            .entityBuilder()
                            .enableLombok() // 启用 Lombok
                            .enableTableFieldAnnotation() // 启用字段注解
                            .addTableFills(tableFills)
//                            .enableFileOverride() //实体文件覆盖
                            .controllerBuilder().disable() //controller禁止生成
//                            .serviceBuilder().enableFileOverride() //service文件覆盖
                            .mapperBuilder().enableFileOverride() //mapper文件覆盖

                            ;
                })
                //添加自定义配置
//                .injectionConfig(cfg->cfg.customFile(mapperXmlBuild))
                .templateEngine(new FreemarkerTemplateEngine()) // 使用 Freemarker 模板引擎
                .execute(); // 执行生成
    }


}

2.模板配置(有默认模板)

下面是mapper.xml.flt示例(Freemarker模板)

<?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="${package.Mapper}.${table.mapperName}">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
        <#list table.fields as field>
            <#if field.keyFlag><#--生成主键排在第一位-->
                <id column="${field.name}" property="${field.propertyName}" />
            </#if>
        </#list>
        <#list table.commonFields as field><#--生成公共字段 -->
            <result column="${field.name}" property="${field.propertyName}" />
        </#list>
        <#list table.fields as field>
            <#if !field.keyFlag><#--生成普通字段 -->
                <result column="${field.name}" property="${field.propertyName}" />
            </#if>
        </#list>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        <#list table.commonFields as field>
            ${field.columnName},
        </#list>
        ${table.fieldNames}
    </sql>
    <insert id="insert${entity}" parameterType="${package.Entity}.${entity}" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ${table.name}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list table.fields as field>
                <#if field.keyFlag=false><#--去掉自增主键-->
                <if test="${field.propertyName} != null">
                    ${field.name},
                </if>
                </#if>
            </#list>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <#list table.fields as field>
                <#if field.keyFlag=false><#--去掉自增主键-->
                <if test="${field.propertyName} != null">
                    ${r"#{"}${field.propertyName}${" , jdbcType = "}${field.metaInfo.jdbcType}${r"}"},
                </if>
                </#if>
            </#list>
        </trim>
    </insert>

    <insert id="batchInsert${entity}" parameterType="${package.Entity}.${entity}">
        INSERT INTO ${table.name}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list table.fields as field>
                <#if field.keyFlag=false><#--去掉自增主键-->
                ${field.name},
                </#if>
            </#list>
        </trim>
        VALUES
        <foreach collection="list" index="index" item="item" separator=",">
        <trim prefix=" (" suffix=")" suffixOverrides=",">
            <#list table.fields as field>
                <#if field.keyFlag=false><#--去掉自增主键-->
                ${r"#{item."}${field.propertyName}${" , jdbcType = "}${field.metaInfo.jdbcType}${r"}"},
                </#if>
            </#list>
        </trim>
        </foreach>
    </insert>

    <!-- 更新 -->
    <update id="update${entity}ById" parameterType="${package.Entity}.${entity}">
        UPDATE ${table.name}
        <set>
            <#list table.fields as field>
                <if test="${field.propertyName} != null">
                    ${field.name} = ${r"#{"}${field.propertyName}${" , jdbcType = "}${field.metaInfo.jdbcType}${r"}"},
                </if>
            </#list>
        </set>
        WHERE id = ${r"#{id}"}
    </update>
</mapper>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值