赞
踩
前言:mybatisplus用了也挺久了,期间用过mybatis的代码生成器,那个主要生成mapper.xml文件,里面基本涵盖了绝大多数常用的方法;项目也用过一些开源框架自带的代码生成器,比如jeecg-boot框架自带的代码生成器,不过那个由于做了一些自定义封装,普适性不是很好,今天记录一下mybatis-plus代码生成器的简单使用(生成基本的controller、service、mapper、mapper.xml),如果感觉功能太少,可以在mybatis-plus自带的模板引擎demo基础上进行自定义改造,下面我们开始正篇吧,废话不多说,上代码:
maven依赖自己酌情增减:
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.3.5.RELEASE</version>
- <relativePath/>
- </parent>
-
- <properties>
- <java.version>1.8</java.version>
- <lombok.version>1.18.16</lombok.version>
- <freemarker.version>2.3.30</freemarker.version>
- <mybatis-plus.version>3.4.1</mybatis-plus.version>
- <druid.version>1.1.22</druid.version>
- <mysql.version>8.0.22</mysql.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <!--mysql数据库依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>${mysql.version}</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>${druid.version}</version>
- </dependency>
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>${mybatis-plus.version}</version>
- </dependency>
-
- <!--mybatis-plus代码生成器相关依赖-->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-generator</artifactId>
- <version>${mybatis-plus.version}</version>
- </dependency>
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>${freemarker.version}</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>${commons-lang3.version}</version>
- </dependency>
-
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>${lombok.version}</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
代码生成器使用示例:
- //执行 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目录下,可以依照此默认模板进行自定义:
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>
- <#if superControllerClassPackage??>
- import ${superControllerClassPackage};
- </#if>
-
- /**
- * <p>
- * ${table.comment!} 前端控制器
- * </p>
- *
- * @author ${author}
- * @since ${date}
- */
- <#if restControllerStyle>
- @RestController
- <#else>
- @Controller
- </#if>
- @RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
- <#if kotlin>
- class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
- <#else>
- <#if superControllerClass??>
- public class ${table.controllerName} extends ${superControllerClass} {
- <#else>
- public class ${table.controllerName} {
- </#if>
-
- }
- </#if>
entity.java.ftl:
- package ${package.Entity};
-
- <#list table.importPackages as pkg>
- import ${pkg};
- </#list>
- <#if swagger2>
- import io.swagger.annotations.ApiModel;
- import io.swagger.annotations.ApiModelProperty;
- </#if>
- <#if entityLombokModel>
- import lombok.Data;
- import lombok.EqualsAndHashCode;
- <#if chainModel>
- import lombok.experimental.Accessors;
- </#if>
- </#if>
-
- /**
- * <p>
- * ${table.comment!}
- * </p>
- *
- * @author ${author}
- * @since ${date}
- */
- <#if entityLombokModel>
- @Data
- <#if superEntityClass??>
- @EqualsAndHashCode(callSuper = true)
- <#else>
- @EqualsAndHashCode(callSuper = false)
- </#if>
- <#if chainModel>
- @Accessors(chain = true)
- </#if>
- </#if>
- <#if table.convert>
- @TableName("${table.name}")
- </#if>
- <#if swagger2>
- @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}> {
- <#else>
- public class ${entity} implements Serializable {
- </#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 swagger2>
- @ApiModelProperty(value = "${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 (versionFieldName!"") == field.name>
- @Version
- </#if>
- <#-- 逻辑删除注解 -->
- <#if (logicDeleteFieldName!"") == field.name>
- @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
- protected 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>
- }
mapper.java.ftl:
- package ${package.Mapper};
-
- import ${package.Entity}.${entity};
- import ${superMapperClassPackage};
-
- /**
- * <p>
- * ${table.comment!} Mapper 接口
- * </p>
- *
- * @author ${author}
- * @since ${date}
- */
- <#if kotlin>
- interface ${table.mapperName} : ${superMapperClass}<${entity}>
- <#else>
- public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {
-
- }
- </#if>
mapper.xml.ftl:
- <?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}">
-
- <#if enableCache>
- <!-- 开启二级缓存 -->
- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
-
- </#if>
- <#if baseResultMap>
- <!-- 通用查询映射结果 -->
- <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>
-
- </#if>
- <#if baseColumnList>
- <!-- 通用查询结果列 -->
- <sql id="Base_Column_List">
- <#list table.commonFields as field>
- ${field.columnName},
- </#list>
- ${table.fieldNames}
- </sql>
-
- </#if>
- </mapper>
service.java.ftl:
- package ${package.Service};
-
- import ${package.Entity}.${entity};
- import ${superServiceClassPackage};
-
- /**
- * <p>
- * ${table.comment!} 服务类
- * </p>
- *
- * @author ${author}
- * @since ${date}
- */
- <#if kotlin>
- interface ${table.serviceName} : ${superServiceClass}<${entity}>
- <#else>
- public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
-
- }
- </#if>
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;
-
- /**
- * <p>
- * ${table.comment!} 服务实现类
- * </p>
- *
- * @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} {
-
- }
- </#if>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。