mybatis-plus自定义代码模板

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} ${(table.serviceName?substring(1))?uncap_first};

    /**
     * 查询分页数据
     */
    @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?uncap_first}){
        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?uncap_first}){
        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

mybatis-plus的公共字段自动填充配置,请查看我的另一篇博客:

https://blog.youkuaiyun.com/qq_26182739/article/details/90476098

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值