mybatis-plus的使用及生成自定义模板(包含Vue,JS)
mybatis-plus的使用 ------ 入门官网mall整合Swagger-UI实现在线API文档
·
根据模板引擎的不同,有不同的模板,.vm为velocity引擎的,.ftl为freemarker引擎的。
下图是项目里 引用的mybatis-plus包的官方模板,copy一份出来,照着改就行
自定义模板放到resource下
/**
* 初始化模板配置
*/
private static TemplateConfig initTemplateConfig() {
TemplateConfig templateConfig = new TemplateConfig();
//可以对controller、service、entity模板进行配置
//mapper.xml模板需单独配置
templateConfig.setController("/mybatistemplate/controller.java.vm");
templateConfig.setXml(null);
return templateConfig;
}
贴一下我们项目改后的controller模板,
#if(${restControllerStyle})
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
/**
* <p>
* $!{table.comment} Controller
* </p>
*
* @author ${author}
* @since ${date}
*/
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@Api(tags = "${table.controllerName}", description = "$!{table.comment} Controller")
@ResourceAuthorize("${table.name}")
@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
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private ${table.serviceName} i${entity}Service;
/**
* 查询分页数据
*/
@ApiOperation(value = "查询分页数据")
@GetMapping(value = "/list")
public CommonResult<CommonPage<${entity}>> findListByPage(@ApiParam ${entity} entity,
@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "50") int pageSize) {
UmsAdmin admin = CurrentUserAdmin.instance();
Page<${entity}> list = i${entity}Service.page(new Page<>(pageNum, pageSize));
return CommonResult.success(CommonPage.restPage(list));
}
/**
* 根据id查询
*/
@ApiOperation(value = "根据id查询数据")
@GetMapping(value = "/{id}")
public CommonResult<${entity}> getById(@PathVariable Long id) {
${entity} entity = i${entity}Service.getById(id);
return CommonResult.success(entity);
}
/**
* 新增
*/
@ApiOperation(value = "新增数据")
@PostMapping(value = "/create")
public CommonResult add(@ApiParam @RequestBody ${entity} entity) {
boolean success = i${entity}Service.saveOrUpdate(entity);
return CommonResult.result(success);
}
/**
* 删除单条记录
*/
@ApiOperation(value = "删除单条记录")
@PostMapping(value = "/delete/{id}")
public CommonResult delete(@PathVariable Long id) {
boolean success = i${entity}Service.removeById(id);
return CommonResult.result(success);
}
}
#end
Velocity 模板如何显示 $! , ${} 等特殊符号
//如果$foo未定义过
$foo 输出:$foo
\$foo 输出:\$foo
\\$foo 输出:\\$foo
\\\$foo 输出:\\\$foo
//如果$foo已经定义过
#set($foo="bar")
$foo 输出:bar
\$foo 输出:$foo
\\$foo 输出:\bar
\\\$foo 输出:\$foo
不过用起来不是很准确,改为
#set($Axios="$Axios") //在文件顶部定义
//使用
this.${message}({message: '请求异常,请重试', type: 'error', duration: 1000});
Velocity 首字母都自动转大写
#set ($validName = $NAME.substring(0, 1).toUpperCase() + $NAME.substring(1))
public class ${validName} implements Serializable {
}
如果用户填写类名不是以”DTO“、“VO”结尾的,那就缺省“DTO”结尾
## 确保以NAME以DTO、Input、PO或VO结尾,缺省为以DTO结尾
#if (!$validName.endsWith("DTO")
&& !$validName.endsWith("Input")
&& !$validName.endsWith("PO")
&& !$validName.endsWith("VO"))
#set ($validName = $validName + "DTO")
#else
#set ($validName = $validName)
#end
驼峰字符串转蛇形字符串
## Model名称转化为snake分割(将所有大写字符前面加下划线,然后字符串转小写,最后删除第一个下划线)
#set( $regex = "([A-Z])")
#set( $replacement = "_$1")
#set( $classMapping = $modelName.replaceAll($regex, $replacement).toLowerCase().substring(1))
以上直接使用mybatis-plus的包就行,如果要生成VUE和JS,需要checkout源码,进行开发
IntelliJ IDEA文件模板与Velocity语法 _
更多推荐
已为社区贡献6条内容
所有评论(0)