MybatisPlus 自定义.vm模板的生成
更新时间:2024年03月18日 14:54:25 作者:王卷卷.
为更加快捷方便的开发代码,使用MybatisPlus的代码自动生成功能,将一些繁琐的操作自动生成,本文主要介绍了MybatisPlus 自定义.vm模板的生成,感兴趣的可以了解一下
官方手册
Mybatis-Plus:
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!--引入MybatisPlus--> < dependency > < groupId >com.baomidou</ groupId > < artifactId >mybatis-plus-boot-starter</ artifactId > < version >3.5.3.1</ version > </ dependency > <!--代码生成器--> < dependency > < groupId >com.baomidou</ groupId > < artifactId >mybatis-plus-generator</ artifactId > < version >3.5.3.1</ version > </ dependency > <!--代码生成器:模板引擎依赖 Velocity--> < dependency > < groupId >org.apache.velocity</ groupId > < artifactId >velocity</ artifactId > < version >1.7</ version > </ dependency > |
代码生成器配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package com.xin.sbvms.utils; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.OutputFile; import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; import java.sql.Types; import java.util.Collections; /** * @author WangXin * @description MyBatis-Plus代码生成器 * @date 2024/03/17 22:02 **/ public class CodeGenerator { public static void main(String[] args) { mpGenerator(); } private static void mpGenerator() { FastAutoGenerator.create( "jdbc:mysql://localhost:3306/sbvms?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai" , "root" , "123456" ) .globalConfig(builder -> { builder.author( "WangXin" ) // 设置作者 // .enableSwagger() // 开启 swagger 模式 .outputDir( "D:\\Code\\Java\\SBV-MS\\src\\main\\java\\" ); // 指定输出目录 }) .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> { int typeCode = metaInfo.getJdbcType().TYPE_CODE; if (typeCode == Types.SMALLINT) { // 自定义类型转换 return DbColumnType.INTEGER; } return typeRegistry.getColumnType(metaInfo); })) .packageConfig(builder -> { builder.parent( "com.xin.sbvms" ) // 设置父包名 .moduleName( null ) // 设置父包模块名 .pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\Code\\Java\\SBV-MS\\src\\main\\resources\\mapper\\" )); // 设置mapperXml生成路径 }) .strategyConfig(builder -> { builder.entityBuilder().enableLombok(); // 开启lombok功能 builder.entityBuilder().enableFileOverride(); // 覆盖已生成文件 builder.controllerBuilder().enableFileOverride(). enableRestStyle(); // 开启Rest风格 builder.serviceBuilder().enableFileOverride(); builder.mapperBuilder().enableFileOverride(); builder.addInclude( "sys_user" ) // 设置需要生成的表名 .addTablePrefix( "t_" , "sys_" ); // 设置过滤表前缀 }) .templateConfig(builder -> { builder.controller( "/templates/myController.java" ) // 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 .build(); // 指定模板 }) // .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); } } |
controller模板
- 模板存放路径:resources/templates/myController.java.vm
- 包名:${package.Entity}
- 类名:${entity} 如 User
- 表面:${table.entityPath} 如 user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | package ${ package .Controller}; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import ${ package .Service}.${table.serviceName}; import ${ package .Entity}.${entity}; # if (${restControllerStyle}) import org.springframework.web.bind.annotation.RestController; # else import org.springframework.stereotype.Controller; #end # if (${superControllerClassPackage}) import ${superControllerClassPackage}; #end /** * $!{table.comment} 前端控制器 * * @author ${author} * @since ${date} */ # if (${restControllerStyle}) @RestController # else @Controller #end @RequestMapping ( "#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end" ) # if (${kotlin}) class ${table.controllerName}# if (${superControllerClass}) : ${superControllerClass}()#end # else # if (${superControllerClass}) public class ${table.controllerName} extends ${superControllerClass} { # else public class ${table.controllerName} { #end @Resource private ${table.serviceName} ${table.entityPath}Service; // 新增或者更新 @PostMapping ( "/add" ) public boolean save( @RequestBody ${entity} ${table.entityPath}){ return ${table.entityPath}Service.saveOrUpdate(${table.entityPath}); } @DeleteMapping ( "/{id}" ) public Boolean delete( @PathVariable Integer id){ return ${table.entityPath}Service.removeById(id); } @PostMapping ( "/del/batch" ) public boolean deleteBatch( @RequestBody List<Integer> ids){ return ${table.entityPath}Service.removeByIds(ids); } @GetMapping public List<${entity}> findAll(){ return ${table.entityPath}Service.list(); } @GetMapping ( "/{id}" ) public ${entity} findOne( @PathVariable Integer id){ return ${table.entityPath}Service.getById(id); } @GetMapping ( "/page" ) public Page<${entity}> findPage( @RequestParam Integer pageNum, @RequestParam Integer pageSize){ QueryWrapper<User> queryWrapper= new QueryWrapper<>(); queryWrapper.orderByDesc( "id" ); return ${table.entityPath}Service.page( new Page<>(pageNum,pageSize),queryWrapper); } } #end |
Bug
自定义controller.java.vm 不生效
在生成器代码【CodeGenerator】中添加下面代码,引入自定义模板【myController.java.vm】
1 2 3 4 | .templateConfig(builder -> { builder.controller( "/templates/myController.java" ) // 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 .build(); // 指定模板 }) |
到此这篇关于MybatisPlus 自定义.vm模板的生成的文章就介绍到这了,更多相关MybatisPlus 自定义.vm模板内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
Spring事务控制策略及@Transactional失效问题解决避坑
这篇文章主要为大家介绍了Spring事务控制策略及@Transactional失效问题解决避坑,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-06-06深入Spring Boot实现对Fat Jar jsp的支持
这篇文章主要介绍了深入Spring Boot实现对Fat Jar jsp的支持,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-06-06
最新评论