mybaties plus 代码生成器 自定义模板

mybaties plus 代码生成器 自定义模板

源代码地址

https://gitee.com/liuerchong/news-artical

此生成的文件controller需要修改业务类类名,实体类格式等

pom 文件



    
        artical-admin-opt
        com.liu.news
        1.0-SNAPSHOT
    
    4.0.0

    artical-code-generate

    


        
            com.liu.news
            artical-common-public
        
        
            com.liu.news
            artical-common-utils
        
        
            com.liu.news
            artical-common-db
        
        
        
            velocity
            
                
                    commons-lang
                    commons-lang
                
                
                    commons-collections
                    commons-collections
                
            
            org.apache.velocity
            ${velocity.version}
        

        
            com.baomidou
            mybatis-plus-generator
            3.2.0
        

        
            org.freemarker
            freemarker
        
        
        
            org.springframework.boot
            spring-boot-starter-undertow
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            commons-collections
            commons-collections
            3.2.2
        
    


工具类 修改数据库配置及包配置

package com.liu.news.atical.codegen;



import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @author lx
 * @version 1.0
 * @description: TODO
 * @date 2021/7/23 9:14
 */
public class CodeGenerator {

    public static String packagePath  = "com.liu.news.artical";
    /**
     * 

* 读取控制台内容 *

*/ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { // 全局配置 GlobalConfig gc = new GlobalConfig(); //获取项目路径 String projectPath = System.getProperty("user.dir"); //输出目录 gc.setOutputDir(projectPath + "/src/main/java"); //覆盖文件 gc.setFileOverride(true); //打开文件夹 gc.setOpen(true); // XML ResultMap: mapper.xml生成查询映射结果 gc.setBaseResultMap(true); // XML ColumnList: mapper.xml生成查询结果列 gc.setBaseColumnList(true); //ID自增长 //type = IdType.ASSIGN_ID gc.setIdType(IdType.INPUT); //作者 gc.setAuthor("Liuerchong"); //实体属性 Swagger2 注解 gc.setSwagger2(true); //数据库配置 DataSourceConfig dsc = new DataSourceConfig(); //数据库路径 dsc.setUrl("jdbc:mysql://127.0.0.1:3307/artical?serverTimezone=UTC&useSSL=false"); //数据库驱动 dsc.setDriverName("com.mysql.cj.jdbc.Driver"); //账号 dsc.setUsername("root"); //密码 dsc.setPassword("root"); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("模块名")); // 自定义模板 TemplateConfig template = new TemplateConfig(); //设置模板 template.setController("templates/controller.java"); template.setService("templates/service.java"); template.setServiceImpl("templates/serviceImpl.java"); template.setMapper("templates/mapper.java"); template.setXml("templates/mapper.xml"); template.setEntity("templates/entity.java"); // 策略配置 StrategyConfig strategy = new StrategyConfig(); //命名设置,驼峰命名 strategy.setNaming(NamingStrategy.underline_to_camel); //列命名设置,驼峰命名 strategy.setColumnNaming(NamingStrategy.underline_to_camel); //实体是否是Lombok模型 strategy.setEntityLombokModel(true); //生成@RestController控制器 strategy.setRestControllerStyle(true); // 公共父类 // strategy.setSuperControllerClass("com.mybatisplus.movie.common.BaseController"); // strategy.setSuperEntityClass("com.mybatisplus.movie.common.BaseEntity"); // 写于父类中的公共字段 // strategy.setSuperEntityColumns("id"); //需要生成的表,不设置默认数据库里的全部表需要生成 strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); //设置控制器映射连字符样式 strategy.setControllerMappingHyphenStyle(true); //设置表前缀 strategy.setTablePrefix(pc.getModuleName() + "_"); //设置实体列常量 strategy.setEntityColumnConstant(true); //设置实体生成器模型 strategy.setEntityBuilderModel(true); //设置实体表字段注释启用 strategy.setEntityTableFieldAnnotationEnable(true); // 代码生成器 AutoGenerator mpg = new AutoGenerator(); //加入全局配置 mpg.setGlobalConfig(gc); //加入数据库配置 mpg.setDataSource(dsc); //加入包配置 mpg.setPackageInfo(pc); //加入自定义模板配置 mpg.setTemplate(template); //加入策略配置 mpg.setStrategy(strategy); //加入模板引擎 //Freemarker模板引擎后缀为ftl // mpg.setTemplateEngine(new FreemarkerTemplateEngine()); //Beetl模板硬气后缀为btl // mpg.setTemplateEngine(new BeetlTemplateEngine()); //Velocity模板引擎后缀为vm mpg.setTemplateEngine(new VelocityTemplateEngine()); //自定义文件生成路径,包路径 //这里调用customPackagePath方法,使用可以自己在内部灵活配置路径 //如果不调用该方法、就会使用MyBatis-Plus默认的文件生成路径和包路径生成文件、但可以使用上面的PackageConfig做一些简单的配置 customPackagePath(pc,mpg); //执行代码生成器 mpg.execute(); } /** * 自定义包路径,文件生成路径,这边配置更灵活 * 虽然也可以使用InjectionConfig设置FileOutConfig的方式设置路径 * 这里直接使用Map方式注入ConfigBuilder配置对象更加直观 * @param pc * @param mpg * @throws NoSuchFieldException * @throws IllegalAccessException */ public static void customPackagePath(PackageConfig pc,AutoGenerator mpg) throws NoSuchFieldException, IllegalAccessException { //获取文件所在路径 String projectPath = System.getProperty("user.dir"); String mavenPath = "\\src\\main\\java\\"; String srcPath = projectPath+mavenPath; String moduleName = pc.getModuleName(); /** * packageInfo配置controller、service、serviceImpl、entity、mapper等文件的包路径 * 这里包路径可以根据实际情况灵活配置 */ Map packageInfo = new HashMap<>(); packageInfo.put(ConstVal.CONTROLLER, packagePath+".controller."+moduleName); packageInfo.put(ConstVal.SERVICE, packagePath+".services."+moduleName+".service"); packageInfo.put(ConstVal.SERVICE_IMPL, packagePath+".services."+moduleName+".service.impl"); packageInfo.put(ConstVal.ENTITY, packagePath+".entity."+moduleName); packageInfo.put(ConstVal.MAPPER, packagePath+".mapper."+moduleName); /** * pathInfo配置controller、service、serviceImpl、entity、mapper、mapper.xml等文件的生成路径 * srcPath也可以更具实际情况灵活配置 * 后面部分的路径是和上面packageInfo包路径对应的源码文件夹路径 * 这里你可以选择注释其中某些路径,可忽略生成该类型的文件,例如:注释掉下面pathInfo中Controller的路径,就不会生成Controller文件 */ Map pathInfo = new HashMap<>(); pathInfo.put(ConstVal.CONTROLLER_PATH, srcPath + packageInfo.get(ConstVal.CONTROLLER).replaceAll("\\.", StringPool.BACK_SLASH + File.separator)); pathInfo.put(ConstVal.SERVICE_PATH, srcPath + packageInfo.get(ConstVal.SERVICE).replaceAll("\\.", StringPool.BACK_SLASH + File.separator)); pathInfo.put(ConstVal.SERVICE_IMPL_PATH, srcPath + packageInfo.get(ConstVal.SERVICE_IMPL).replaceAll("\\.", StringPool.BACK_SLASH + File.separator)); pathInfo.put(ConstVal.ENTITY_PATH, srcPath + packageInfo.get(ConstVal.ENTITY).replaceAll("\\.", StringPool.BACK_SLASH + File.separator)); pathInfo.put(ConstVal.MAPPER_PATH, srcPath + packageInfo.get(ConstVal.MAPPER).replaceAll("\\.", StringPool.BACK_SLASH + File.separator)); pathInfo.put(ConstVal.XML_PATH, projectPath+"\\src\\main\\resources\\mapper\\"+moduleName); pc.setPathInfo(pathInfo); /** * 创建configBuilder对象,传入必要的参数 * 将以上的定义的包路径packageInfo配置到赋值到configBuilder对象的packageInfo属性上 * 因为packageInfo是私有成员变量,也没有提交提供公共的方法,所以使用反射注入 * 为啥要这么干,看源码去吧 */ ConfigBuilder configBuilder = new ConfigBuilder(mpg.getPackageInfo(), mpg.getDataSource(), mpg.getStrategy(), mpg.getTemplate(), mpg.getGlobalConfig()); Field packageInfoField = configBuilder.getClass().getDeclaredField("packageInfo"); packageInfoField.setAccessible(true); packageInfoField.set(configBuilder,packageInfo); /** * 设置配置对象 */ mpg.setConfig(configBuilder); } }

模板

在resource 目录底下新建templates文件夹,然后新建vm文件

controller.java.vm

##package ${cfg.controllerPackage};
package ${package.Controller};

import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
##import ${package.Entity}.bo.QueryBO;
##import ${cfg.extPackagePre}.${cfg.service}.${table.serviceName};
##import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.news.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
##import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @Package: ${package.Entity}.${entity}
 * @Description: <$!{table.comment}前端控制器>
 * @Author: ${author}
 * @CreateDate: ${date}
 * @UpdateUser: ${author}
 * @UpdateDate: ${date}
 * @UpdateRemark: <>
 * @Version: 1.0
 */
#set($path=${table.name})
#set($path=$path.concat("path"))
@RestController
##@Api(tags = "$!{table.comment}-相关接口")
@Api(tags = "${entity}-相关接口")
##@RequestMapping(value = "${cfg.get($path)}", produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(value = "${entity}")
public class ${table.controllerName} {

private static final Logger logger = LoggerFactory.getLogger(${table.controllerName}.class);

@Autowired
private ${table.serviceName} i${table.serviceName};

@GetMapping("{id}")
@ApiOperation(value = "$!{table.comment}获取某一实体")
public R get${table.serviceName}Details(@PathVariable Integer id) {
        return R.ok(i${table.serviceName}.getById(id));
        }


/**
  * 列表查询(非分页)
  *
  * @return
  */
@RequestMapping("/list")
public R list() {
    ${entity} model = new  ${entity}();
        List<${entity}> list =  i${table.serviceName}.list(model);
        // todo 再包装一层
        return  R.ok(list);
        }


/**
* 列表查询(分页)
*
* @return
*/
@RequestMapping("/pageList")
public R pageList(Integer pageNum, Integer pageSize) {

    ${entity} model = new  ${entity}();
        IPage page = i${table.serviceName}.pageList(model, pageNum, pageSize);
        // todo 再包装一层
        return R.ok(page);
        }

@PostMapping
@ApiOperation(value = "$!{table.comment}新增数据")
public R save${table.serviceName}(@RequestBody ${entity} dto) {
        return R.ok(i${table.serviceName}.save(dto));
        }

@PutMapping("{id}")
@ApiOperation(value = "$!{table.comment}修改数据")
public R modify${table.serviceName}(@RequestBody ${entity} dto, @PathVariable Integer id) {
        dto.setId(id);
        return R.ok(i${table.serviceName}.updateById(dto)) ;
        }

@DeleteMapping("batch")
@ApiOperation(value = "$!{table.comment}批量删除数据")
public R batchRemove${table.serviceName}(@RequestParam(value = "ids") List ids) {
        return R.ok(i${table.serviceName}.removeByIds(ids)) ;
        }

@DeleteMapping("{id}")
@ApiOperation(value = "$!{table.comment}删除数据")
public R remove${table.serviceName}(@PathVariable Integer id) {
        return R.ok(i${table.serviceName}.removeById(id)) ;
        }
        }

entity.java.vm

package ${package.Entity};
    #foreach($pkg in ${table.importPackages})
    import ${pkg};
    #end
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @Package: ${package.Entity}
 * @Description: <$!{table.comment}实体>
 * @Author: ${author}
 * @CreateDate: ${date}
 * @UpdateUser: ${author}
 * @UpdateDate: ${date}
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Data
#if(${cfg.customerTableName})
@TableName("${table.name}")
#end
#if(${swagger2})
##@ApiModel(value = "$!{table.comment}实体")
@ApiModel(value = "${entity}实体")
#end
#if(${superEntityClass})
public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
#else
public class ${entity} implements Serializable{
#end
/**
 * 序列化时候使用
 */
private static final long serialVersionUID = 1L;

## ----------  BEGIN 字段循环遍历  ----------
#foreach($field in ${table.fields})
    #if(${field.keyFlag})
        #set($keyPropertyName=${field.propertyName})
    #end
###if("$!field.comment" != "")
##    /**
##     * ${field.comment}
##     */
###end
    #if(${field.keyFlag})
    ## 主键
        #if(${field.keyIdentityFlag})
        @TableId(value = "${field.name}", type = IdType.ASSIGN_ID)
        #elseif(${field.convert})
        @TableId("${field.name}")
        #end
    #end
## 乐观锁注解
    #if(${versionFieldName}==${field.name})
    @Version
    #end
## 逻辑删除注解
    #if(${logicDeleteFieldName}==${field.name})
    @TableLogic
    #end
##    生成swagg实体描述
    #if(${swagger2})
        #set($comment=${field.comment})
        #set($blankIndex=$comment.indexOf(" "))
        #set($hasBlank=$comment.indexOf(" ")!=-1)
        #if($hasBlank)
        @ApiModelProperty(value = "$comment.substring(0,$blankIndex)", notes = "$comment.substring($blankIndex).trim()")
        #end
        #if(!$hasBlank)
        @ApiModelProperty(value = "$comment", notes = "")
        #end
    #end
private ${field.propertyType} ${field.propertyName};

#end
}

mapper.java.vm

package ${package.Mapper};

import ${package.Entity}.${entity};
import ${superMapperClassPackage};
import org.apache.ibatis.annotations.Mapper;

/**
 * @Package: ${package.Mapper}
 * @Description: <$!{table.comment}Mapper接口>
 * @Author: ${author}
 * @CreateDate: ${date}
 * @UpdateUser: ${author}
 * @UpdateDate: ${date}
 * @UpdateRemark: <>
 * @Version: 1.0
 */
#if(${kotlin})
interface ${table.mapperName} : ${superMapperClass}<${entity}>
#else
@Mapper
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {

        }
#end


mapper.xml.vm





###if(${enableCache})
##    
##    
##
###end
###if(${baseResultMap})
##    
##    
###foreach($field in ${table.fields})
###if(${field.keyFlag})##生成主键排在第一位
##        
###end
###end
###foreach($field in ${table.commonFields})##生成公共字段
##        
###end
###foreach($field in ${table.fields})
###if(!${field.keyFlag})##生成普通字段
##        
###end
###end
##    
##
###end
###if(${baseColumnList})
##    
##    
###foreach($field in ${table.commonFields})
##        ${field.columnName},
###end
##        ${table.fieldNames}
##    
##
###end



service.java.vm

package ${package.Service};

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

/**
 * @Package: ${package.Service}
 * @Description: <$!{table.comment}服务类>
 * @Author: ${author}
 * @CreateDate: ${date}
 * @UpdateUser: ${author}
 * @UpdateDate: ${date}
 * @UpdateRemark: <>
 * @Version: 1.0
 */
#if(${kotlin})
interface ${table.serviceName} : ${superServiceClass}<${entity}>
#else
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {

        /**
       * 查询
       *
       * @param model
       * @return
       */
        List<${entity}> list(${entity} model);

        /**
        * 分页查询
        *
        * @param model
        * @param pageNum  第几页
        * @param pageSize 每页数量
        * @return
        */
        IPage<${entity}> pageList(${entity} model, Integer pageNum, Integer pageSize);

        }
#end


serviceImpl.java.vm

package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
##import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
##import org.springframework.stereotype.Service;

/**
 * @Package: ${package.ServiceImpl}
 * @Description: <$!{table.comment}服务实现类>
 * @Author: ${author}
 * @CreateDate: ${date}
 * @UpdateUser: ${author}
 * @UpdateDate: ${date}
 * @UpdateRemark: <>
 * @Version: 1.0
 */
##@Service
#if(${kotlin})
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

        }
#else
##@ConditionalOnProperty(prefix = "spring.config-service", name = "enable", havingValue = "true", matchIfMissing = false)
        @Service
        public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

            @Override
            public List< ${entity}> list(${entity} model) {
            QueryWrapper< ${entity}> wrapper = new QueryWrapper();
            return super.list(wrapper);
            }

            @Override
            public IPage< ${entity}> pageList( ${entity} model, Integer pageNum, Integer pageSize) {

            QueryWrapper< ${entity}> wrapper = new QueryWrapper(model);
            return this.page(new Page<>(pageNum, pageSize), wrapper);
            }
        }
#end


辅助

/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.liu.news.common.result;

import com.liu.news.common.consts.CommonConstants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param 
 * @author lengleng
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R implements Serializable {

	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
	private int code;

	@Getter
	@Setter
	private String msg;

	@Getter
	@Setter
	private T data;

	public static  R ok() {
		return restResult(null, CommonConstants.SUCCESS, null);
	}

	public static  R ok(T data) {
		return restResult(data, CommonConstants.SUCCESS, null);
	}

	public static  R ok(T data, String msg) {
		return restResult(data, CommonConstants.SUCCESS, msg);
	}

	public static  R failed() {
		return restResult(null, CommonConstants.FAIL, null);
	}

	public static  R failed(String msg) {
		return restResult(null, CommonConstants.FAIL, msg);
	}

	public static  R failed(T data) {
		return restResult(data, CommonConstants.FAIL, null);
	}

	public static  R failed(T data, String msg) {
		return restResult(data, CommonConstants.FAIL, msg);
	}

	private static  R restResult(T data, int code, String msg) {
		R apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}

}

/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.liu.news.common.consts;


public interface CommonConstants {

	/**
	 * 删除
	 */
	String STATUS_DEL = "1";

	/**
	 * 正常
	 */
	String STATUS_NORMAL = "0";

	/**
	 * 锁定
	 */
	String STATUS_LOCK = "9";

	

	
	/**
	 * 成功标记
	 */
	Integer SUCCESS = 0;

	/**
	 * 失败标记
	 */
	Integer FAIL = 1;


}

你可能感兴趣的:(项目实战)