1.對於mybatis-plus不瞭解的同學,可以去官網看看
官網地址: https://mp.baomidou.com/
真的提高了不少開發效率
2.這裏簡單說一下如何自定義我們自己需要controller的代碼生成模板
在官方的 mybatis-plus-generator:3.0.3.jar的template文件夾下是官方提供的默認的代碼生成模板,大家可以看一下
根據模板引擎的不同,有不同的模板,.vm爲velocity引擎的,.ftl爲freemarker引擎的
關於怎麼配置代碼生成器,請參照官網的例子:https://mp.baomidou.com/guide/generator.html
自定義配置模板主要代碼如下:
// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 自定義controller的代碼模板
String templatePath = "/template/controller.java.ftl";
// 自定義輸出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定義配置會被優先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 + pc.getModuleName()
String expand = projectPath + "/cloud-api/src/main/java/com/api/" +pc.getModuleName() + "/" + "controller";
String entityFile = String.format((expand + File.separator + "%s" + ".java"), tableInfo.getControllerName());
return entityFile;
}
});
// 自定義mapper.xml的代碼模板
templatePath = "/template/mapper.xml.ftl";
// 自定義配置會被優先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 + pc.getModuleName()
return projectPath + "/cloud-api/src/main/resources/mapper/"
+ "/" + tableInfo.getEntityName() + "Mapper" +StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
templateConfig.setController(null);
mpg.setTemplate(templateConfig);
controller的模板(controller.java.ftl)內容如下:
package ${package.Controller};
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.ycj.oe.service.${table.serviceName};
import com.ycj.oe.entity.${entity};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import base.ResponseObj;
import java.util.List;
<#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}
* @version v1.0
*/
<#if restControllerStyle>
@Api(tags = {"${table.comment!}"})
@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>
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private ${table.serviceName} targetService;
/**
* 查詢分頁數據
*/
@ApiOperation(value = "查詢分頁數據")
@RequestMapping(value = "/list")
public ResponseWeb<Page> findListByPage(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,@RequestParam(name = "pageSize", defaultValue = "20") int pageSize){
return null;
}
/**
* 根據id查詢
*/
@ApiOperation(value = "根據id查詢數據")
@RequestMapping(value = "/getById")
public ResponseWeb<${entity}> getById(@RequestParam("pkid") String pkid){
return null;
}
/**
* 新增
*/
@ApiOperation(value = "新增數據")
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ResponseWeb<${entity}> add(@RequestBody ${entity} ${entity}){
return null;
}
/**
* 刪除
*/
@ApiOperation(value = "刪除數據")
@RequestMapping(value = "/del")
public ResponseWeb<String> delete(@RequestParam("ids") List<String> ids){
return null;
}
/**
* 修改
*/
@ApiOperation(value = "更新數據")
@RequestMapping(value = "/update", method = RequestMethod.POST)
public ResponseWeb<${entity}> update(@RequestBody ${entity} ${entity}){
return null;
}
}
</#if>
mapper的模板(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}" jdbcType="${field.type}"/>
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
<result column="${field.name}" property="${field.propertyName}" jdbcType="${field.type}"/>
</#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
<result column="${field.name}" property="${field.propertyName}" jdbcType="${field.type}"/>
</#if>
</#list>
</resultMap>
</#if>
<#if baseColumnList>
<!-- 通用查詢結果列 -->
<sql id="Base_Column_List">
<#list table.commonFields as field>
${field.name},
</#list>
${table.fieldNames}
</sql>
</#if>
</mapper>
相關的其他配置信息,請查看:https://mp.baomidou.com/config/#%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE