Mybatis-plus最新代码生成器(3.5.1+)的使用

1.引入依赖:

<!--mybatisPlus-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3.4</version>
</dependency>
<!--mybatis-plus代码生成器-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.1</version>
</dependency>
<!--velocity模板-->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.3</version>
</dependency>
<!--freemarker模板-->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
</dependency>
<!--mysql-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

解释一下为什么要引两个模板依赖:

🐰 因为plus代码生成器需要一个模板引擎,velocity和freemarker任选一个,velocity是生成器中默认使用的,根据你的选择引依赖。

2.写一个构造器类

随便创建一个类:像启动类那样有个psvm能跑就行

public class PracticeApplication {

    public static void main(String[] args) {
        代码生成器。。。。;
    }

}

接下来就是写生成逻辑了,很简单,看官网:plus–代码生成器

image-20211006150757526

这是最新版的,整体的结构就是这样的,下面把我的代码生成器拿出来,并介绍一下分别有什么用(其实官网都有),完整代码放在最后:

1.全局配置(GlobalConfig)

image-20211006152120624

  • create方法需要传入,数据库地址(如果你的MySQL版本为8,必需要在数据库地址后面加上时区, 像serverTimezone=Asia/Shanghai这个)、用户名、密码;它会在后台根据这三个参数自动构建DataSourceConfig,而不需要我们自己写了,如图:

    image-20211006152458545

  • 最新版的生成器使用了lambda表达式,反应式编程,点点点就行了。写起来非常方便

  • author指定作者

  • outputDir,指定生成的文件往哪输出

  • enableSwagger,支持swagge(非常nice,记得引swagger依赖)

  • commentDate 时间格式

  • fileOveride 覆盖之前生成的文件

    globalConfig的效果如图:

    image-20211006153239162

2. 包配置(PackageConfig)

image-20211006153717464

这个就是配置生成哪些包:

注意:配置xml的包的方法,官网是叫mapperXml,而实际代码中的方法叫xml()

image-20211006155444599

3. 策略配置(StrategyConfig)

image-20211006155301030

addInclude()就是指定为哪些表生成代码,有几个重载:

  • ​ 一个String的表名
  • 任意多个表名(可变长参数):“user”, “user1”,…这样
  • 列表list

所谓策略配置,就是配置策略,配置细节

它是将service、mapper、controller、entity都放到了策略配置里面,以前的版本是在全局配置中

还有个注入配置,貌似不常用。。。。。

代码的最后:

.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();

可以指定模板引擎,

execute()执行代码生成器,生成代码

根据实际情况配置不同选项,按照上面的来就很容易完成。建议还是看官网plus–代码生成器

效果图:

image-20211006161958575

完整代码:
2022.4.7日更新

  1. service实现类的路径规范;
  2. 代码生成器3.5.2后,xml的位置配置由OutputFile.mapperXml变为OutputFile.xml,但本文使用的3.5.1,所以下面代码并未改动
  3. 很多同学反映实体类lombok的注解是@Setter和@Getter,而不是@Data。今天更新了,这个需要我们自定义模板。代码生成器的jar下的templates下有不同引擎的模板文件,我们使用的Freemarker引擎模板,所以需要添加.ftl后缀的模板。只需要复制过来,@Setter和@Getter改为@Data即可
    resource目录下的templates目录下创建名为myentity.java.ftl的文件,内容如下:
package ${package.Entity};

<#list table.importPackages as pkg>
import ${pkg};
</#list>
<#if swagger>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
</#if>
<#if entityLombokModel>
import lombok.Data;
    <#if chainModel>
import lombok.experimental.Accessors;
    </#if>
</#if>

/**
 * <p>
 * ${table.comment!}
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if entityLombokModel>
@Data
    <#if chainModel>
@Accessors(chain = true)
    </#if>
</#if>
<#if table.convert>
@TableName("${schemaName}${table.name}")
</#if>
<#if swagger>
@ApiModel(value = "${entity}对象", description = "${table.comment!}")
</#if>
<#if superEntityClass??>
public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}></#if> {
<#elseif activeRecord>
public class ${entity} extends Model<${entity}> {
<#elseif entitySerialVersionUID>
public class ${entity} implements Serializable {
<#else>
public class ${entity} {
</#if>
<#if entitySerialVersionUID>

    private static final long serialVersionUID = 1L;
</#if>
<#-- ----------  BEGIN 字段循环遍历  ---------->
<#list table.fields as field>
    <#if field.keyFlag>
        <#assign keyPropertyName="${field.propertyName}"/>
    </#if>

    <#if field.comment!?length gt 0>
        <#if swagger>
    @ApiModelProperty("${field.comment}")
        <#else>
    /**
     * ${field.comment}
     */
        </#if>
    </#if>
    <#if field.keyFlag>
        <#-- 主键 -->
        <#if field.keyIdentityFlag>
    @TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
        <#elseif idType??>
    @TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
        <#elseif field.convert>
    @TableId("${field.annotationColumnName}")
        </#if>
        <#-- 普通字段 -->
    <#elseif field.fill??>
    <#-- -----   存在字段填充设置   ----->
        <#if field.convert>
    @TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
        <#else>
    @TableField(fill = FieldFill.${field.fill})
        </#if>
    <#elseif field.convert>
    @TableField("${field.annotationColumnName}")
    </#if>
    <#-- 乐观锁注解 -->
    <#if field.versionField>
    @Version
    </#if>
    <#-- 逻辑删除注解 -->
    <#if field.logicDeleteField>
    @TableLogic
    </#if>
    private ${field.propertyType} ${field.propertyName};
</#list>
<#------------  END 字段循环遍历  ---------->

<#if !entityLombokModel>
    <#list table.fields as field>
        <#if field.propertyType == "boolean">
            <#assign getprefix="is"/>
        <#else>
            <#assign getprefix="get"/>
        </#if>
    public ${field.propertyType} ${getprefix}${field.capitalName}() {
        return ${field.propertyName};
    }

    <#if chainModel>
    public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
    <#else>
    public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
    </#if>
        this.${field.propertyName} = ${field.propertyName};
        <#if chainModel>
        return this;
        </#if>
    }
    </#list>
</#if>

<#if entityColumnConstant>
    <#list table.fields as field>
    public static final String ${field.name?upper_case} = "${field.name}";

    </#list>
</#if>
<#if activeRecord>
    @Override
    public Serializable pkVal() {
    <#if keyPropertyName??>
        return this.${keyPropertyName};
    <#else>
        return null;
    </#if>
    }

</#if>
<#if !entityLombokModel>
    @Override
    public String toString() {
        return "${entity}{" +
    <#list table.fields as field>
        <#if field_index==0>
            "${field.propertyName}=" + ${field.propertyName} +
        <#else>
            ", ${field.propertyName}=" + ${field.propertyName} +
        </#if>
    </#list>
        "}";
    }
</#if>
}




代码生成完整代码:

package com.xp.practice.generator;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

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

public class UserGenerator {
    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();
        tables.add("p_user");
        tables.add("p_question");
        tables.add("p_answer");
        tables.add("p_correct");

        FastAutoGenerator.create("jdbc:mysql://localhost:3306/xpa","root","111111")
                .globalConfig(builder -> {
                    builder.author("向培")               //作者
                            .outputDir(System.getProperty("user.dir")+"\\src\\main\\java")    //输出路径(写到java目录)
                            .enableSwagger()           //开启swagger
                            .commentDate("yyyy-MM-dd")
                            .fileOverride();            //开启覆盖之前生成的文件

                })
                .packageConfig(builder -> {
                    builder.parent("com.xp")
                            .moduleName("practice")
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml,System.getProperty("user.dir")+"\\src\\main\\resources\\mapper"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .addTablePrefix("p_")
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .enableLombok()
                            .logicDeleteColumnName("deleted")
                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            // 映射路径使用连字符格式,而不是驼峰
                            .enableHyphenStyle()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .mapperBuilder()
                            //生成通用的resultMap
                            .enableBaseResultMap()  
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .templateConfig(new Consumer<TemplateConfig.Builder>() {
                    @Override
                    public void accept(TemplateConfig.Builder builder) {
                        // 实体类使用我们自定义模板
                        builder.entity("templates/myentity.java");
                    }
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .execute();
    }
}

  • 88
    点赞
  • 251
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 53
    评论
MyBatis-Plus 3.5.1 代码生成器MyBatis-Plus 框架提供的一款代码生成工具,可以帮助开发者快速生成 MyBatis-Plus 的 Mapper 接口及其 XML 映射文件、Service 接口、ServiceImpl 实现类、Entity 实体类等代码。 使用 MyBatis-Plus 代码生成器,可以减轻开发者的工作负担,提高开发效率,避免手写重复性的代码。以下是使用 MyBatis-Plus 代码生成器的步骤: 1. 引入 MyBatis-Plus 依赖 在项目的 pom.xml 文件中,添加 MyBatis-Plus依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.5.1</version> </dependency> ``` 2. 配置代码生成器 在项目的配置文件(如 application.yml 或 application.properties)中,添加代码生成器的配置信息,包括数据库连接信息、生成代码的包路径、作者等信息。 ``` mybatis-plus: global-config: db-config: # 数据库配置 url: jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true username: root password: 123456 driver-name: com.mysql.cj.jdbc.Driver generator: # 生成代码的包路径 package-name: com.example.mybatisplusdemo # 生成代码的作者 author: example # 开启生成器 enable: true # 开启实体类 Lombok 注解 enable-lombok: true # 开启 Swagger2 注解 enable-swagger: true # 开启 ActiveRecord 模式(生成 ActiveRecord 的实体类和接口) enable-activerecord: true ``` 3. 运行代码生成器 在项目的启动类中,添加以下代码,启动代码生成器: ``` @SpringBootApplication public class MybatisPlusDemoApplication { public static void main(String[] args) { SpringApplication.run(MybatisPlusDemoApplication.class, args); // 启动代码生成器 MybatisPlusGenerator.execute(); } } ``` 4. 查看生成的代码 代码生成器会根据配置信息,自动生成 Mapper 接口及其 XML 映射文件、Service 接口、ServiceImpl 实现类、Entity 实体类等代码,生成的代码位于指定的包路径下。开发者可以在生成的代码的基础上,进行业务代码的开发。
评论 53
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为了我的架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值