mybatis-plus-generator(mybatisplus代码生成器篇)

前言:mybatisplus用了也挺久了,期间用过mybatis的代码生成器,那个主要生成mapper.xml文件,里面基本涵盖了绝大多数常用的方法;项目也用过一些开源框架自带的代码生成器,比如jeecg-boot框架自带的代码生成器,不过那个由于做了一些自定义封装,普适性不是很好,今天记录一下mybatis-plus代码生成器的简单使用(生成基本的controller、service、mapper、mapper.xml),如果感觉功能太少,可以在mybatis-plus自带的模板引擎demo基础上进行自定义改造,下面我们开始正篇吧,废话不多说,上代码:

maven依赖自己酌情增减:

    
	    org.springframework.boot
	    spring-boot-starter-parent
	    2.3.5.RELEASE
	    
	
    
    
        1.8
        1.18.16
        2.3.30
        3.4.1
        1.1.22
        8.0.22
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
            com.alibaba
            druid-spring-boot-starter
            ${druid.version}
        
        
            com.baomidou
            mybatis-plus-boot-starter
            ${mybatis-plus.version}
        

        
        
            com.baomidou
            mybatis-plus-generator
            ${mybatis-plus.version}
        
        
            org.freemarker
            freemarker
            ${freemarker.version}
        

        
            org.apache.commons
            commons-lang3
            ${commons-lang3.version}
        

        
            org.projectlombok
            lombok
            ${lombok.version}
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

代码生成器使用示例:

//执行 main 方法,控制台输入模块表名,回车自动生成对应项目目录中
public class MybatisPlusCodeGenerator {

    public static void main(String[] args) {
        //====================配置变量区域=====================//
        String author="";//生成文件的作者,可以不填
        String rootPackage="org.example.xxxx";//生成的entity、controller、service等包所在的公共上一级包路径全限定名
        String moduleName="province-sess-platfrom-xqxy";
        //数据库配置
        String url="jdbc:mysql://192.168.xxx.xxx:3306/xxx?useSSL=false&characterEncoding=utf8";
        String driverClassName="com.mysql.jdbc.Driver";//或者com.mysql.cj.jdbc.Driver
        String username="root";
        String password="xxxxxx";
        String tableNames="annualplan";//表名,多个使用,分隔
        //====================配置变量区域=====================//

        // 代码生成器
        AutoGenerator generator = new AutoGenerator();
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        globalConfig.setOutputDir(projectPath +"/"+moduleName+"/src/main/java");
        globalConfig.setFileOverride(false);//是否覆盖已有文件,默认false
        globalConfig.setOpen(false);//是否打开输出目录
        globalConfig.setAuthor(author);
        globalConfig.setServiceName("%sService");//去掉service接口的首字母I
        globalConfig.setBaseResultMap(true);//开启 BaseResultMap
        globalConfig.setDateType(DateType.ONLY_DATE);//只使用 java.util.date代替
        globalConfig.setIdType(IdType.ASSIGN_ID);//分配ID (主键类型为number或string)
        generator.setGlobalConfig(globalConfig);

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(url);
        dataSourceConfig.setDbType(DbType.MYSQL);//数据库类型
        dataSourceConfig.setDriverName(driverClassName);
        dataSourceConfig.setUsername(username);
        dataSourceConfig.setPassword(password);
        generator.setDataSource(dataSourceConfig);

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        //packageConfig.setModuleName(scanner("模块名"));
        packageConfig.setParent(rootPackage);//例:org.jeecg.modules.xqxy
        generator.setPackageInfo(packageConfig);

        //注意:模板引擎在mybatisplus依赖中的templates目录下,可以依照此默认模板进行自定义

        // 策略配置:配置根据哪张表生成代码
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude(tableNames);//表名,多个英文逗号分割(与exclude二选一配置)
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        strategy.setEntityLombokModel(true);//lombok模型,@Accessors(chain = true)setter链式操作
        strategy.setRestControllerStyle(true);//controller生成@RestController
        strategy.setEntityTableFieldAnnotationEnable(true);//是否生成实体时,生成字段注解

        generator.setStrategy(strategy);
        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        generator.execute();
    }

}

详细配置可以在idea中查看配置类注释,download source,里面的注释都是中文的,比较详细。

 模板引擎在mybatisplus依赖中的templates目录下,可以依照此默认模板进行自定义:

mybatis-plus-generator(mybatisplus代码生成器篇)_第1张图片

controller.java.ftl:

package ${package.Controller};


import org.springframework.web.bind.annotation.RequestMapping;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;

<#if superControllerClassPackage??>
import ${superControllerClassPackage};


/**
 * 

* ${table.comment!} 前端控制器 *

* * @author ${author} * @since ${date} */ <#if restControllerStyle> @RestController <#else> @Controller @RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}") <#if kotlin> class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}() <#else> <#if superControllerClass??> public class ${table.controllerName} extends ${superControllerClass} { <#else> public class ${table.controllerName} { }

entity.java.ftl:

package ${package.Entity};

<#list table.importPackages as pkg>
import ${pkg};

<#if swagger2>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

<#if entityLombokModel>
import lombok.Data;
import lombok.EqualsAndHashCode;
    <#if chainModel>
import lombok.experimental.Accessors;
    


/**
 * 

* ${table.comment!} *

* * @author ${author} * @since ${date} */ <#if entityLombokModel> @Data <#if superEntityClass??> @EqualsAndHashCode(callSuper = true) <#else> @EqualsAndHashCode(callSuper = false) <#if chainModel> @Accessors(chain = true) <#if table.convert> @TableName("${table.name}") <#if swagger2> @ApiModel(value="${entity}对象", description="${table.comment!}") <#if superEntityClass??> public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}> { <#elseif activeRecord> public class ${entity} extends Model<${entity}> { <#else> public class ${entity} implements Serializable { <#if entitySerialVersionUID> private static final long serialVersionUID = 1L; <#-- ---------- BEGIN 字段循环遍历 ----------> <#list table.fields as field> <#if field.keyFlag> <#assign keyPropertyName="${field.propertyName}"/> <#if field.comment!?length gt 0> <#if swagger2> @ApiModelProperty(value = "${field.comment}") <#else> /** * ${field.comment} */ <#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}") <#-- 普通字段 --> <#elseif field.fill??> <#-- ----- 存在字段填充设置 -----> <#if field.convert> @TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill}) <#else> @TableField(fill = FieldFill.${field.fill}) <#elseif field.convert> @TableField("${field.annotationColumnName}") <#-- 乐观锁注解 --> <#if (versionFieldName!"") == field.name> @Version <#-- 逻辑删除注解 --> <#if (logicDeleteFieldName!"") == field.name> @TableLogic private ${field.propertyType} ${field.propertyName}; <#------------ END 字段循环遍历 ----------> <#if !entityLombokModel> <#list table.fields as field> <#if field.propertyType == "boolean"> <#assign getprefix="is"/> <#else> <#assign getprefix="get"/> 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}) { this.${field.propertyName} = ${field.propertyName}; <#if chainModel> return this; } <#if entityColumnConstant> <#list table.fields as field> public static final String ${field.name?upper_case} = "${field.name}"; <#if activeRecord> @Override protected Serializable pkVal() { <#if keyPropertyName??> return this.${keyPropertyName}; <#else> return null; } <#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} + "}"; } }

mapper.java.ftl:

package ${package.Mapper};

import ${package.Entity}.${entity};
import ${superMapperClassPackage};

/**
 * 

* ${table.comment!} Mapper 接口 *

* * @author ${author} * @since ${date} */ <#if kotlin> interface ${table.mapperName} : ${superMapperClass}<${entity}> <#else> public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { }

mapper.xml.ftl:





<#if enableCache>
    
    


<#if baseResultMap>
    
    
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        


<#list table.commonFields as field><#--生成公共字段 -->
        

<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
        


    


<#if baseColumnList>
    
    
<#list table.commonFields as field>
        ${field.columnName},

        ${table.fieldNames}
    



service.java.ftl:

package ${package.Service};

import ${package.Entity}.${entity};
import ${superServiceClassPackage};

/**
 * 

* ${table.comment!} 服务类 *

* * @author ${author} * @since ${date} */ <#if kotlin> interface ${table.serviceName} : ${superServiceClass}<${entity}> <#else> public interface ${table.serviceName} extends ${superServiceClass}<${entity}> { }

serviceImpl.java.ftl:

package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;

/**
 * 

* ${table.comment!} 服务实现类 *

* * @author ${author} * @since ${date} */ @Service <#if kotlin> open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} { } <#else> public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} { }

你可能感兴趣的:(mybatis,java,spring,boot)