代码生成器:Mybatis-plus代码生成自定义模版
简述
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
添加依赖
MyBatis-Plus 从 3.0.3 之后移除了代码生成器与模板引擎的默认依赖,需要手动添加相关依赖:
<!-- mybatis代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.0</version>
</dependency>
<!-- mybatis核心代码 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.4.0</version>
</dependency>
<!-- 数据库连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- 自定义模版引擎 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<!-- java模版引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
复制maven包内的模版
maven仓库内搜索mybatis-plus-generator,打开,复制其中的templates目录至resources
由于我们后续使用的是FreemarkerTemplateEngine,仅用到.ftl文件,其他后缀文件可自行删除。
自定义模版内容
controller.java.ftl
package ${
package.Controller};
import ${
package.Entity}.${
entity};
import ${
package.Service}.${
table.serviceName};
import com.自己的工具包.RetJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
<#if restControllerStyle>
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${
superControllerClassPackage};
</#if>
import javax.validation.Valid;
import java.util.List;
/**
* <p>
* ${table.comment} 前端控制器
* </p>
*
* @author ${author}
* @since ${date}
*/
@Api(tags = "${table.comment}")
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if 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>
@Autowired
private ${
table.serviceName} ${
table.serviceName?uncap_first};
@ApiOperation(value = "${table.comment}分页列表", response = ${
entity}.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "页面", dataType = "Long"),
@ApiImplicitParam(name = "size", value = "页面数据量", dataType = "Long"),
@ApiImplicitParam(name = "sort", value = "排序方式排序[true:正序; false:倒序]", dataType = "Boolean"),
@ApiImplicitParam(name = "sortName", value = "排序字段,参照返回字段", dataType = "String")})
@PostMapping(value = "/page")
public Object list(@Valid @RequestBody ${
entity} param) {
Object data = ${
table.serviceName?uncap_first}.page(param);
return RetJson.ok(data);
}
@ApiOperation(value = "${table.comment}详情", response = ${
entity}.class)
@GetMapping(value = "/info/{id}")
public Object info(@PathVariable Long id) {
Object data = ${
table.serviceName?uncap_first}.info(id);
return RetJson.ok(data);
}
@ApiOperation(value = "${table.comment}新增")
@PostMapping(value = "/add")
public Object add(@Valid @RequestBody ${
entity} param) {
${
table.serviceName?uncap_first}.add(param);
return RetJson.ok();
}
@ApiOperation(value = "${table.comment}修改")
@PostMapping(value = "/modify")
public Object