记录自己的Mybatis-plus之路02-mp自定义模板

mp自动生成代码工具类在请看上篇文章

传送-->记录自己的Mybatis-plus之路01-初识mp

土 豪 链 接 模板下载

平民往下走

目录

自定义模板Controller

自定义模板service

自定义模板serviceImpl

自定义模板dao

自定义模板mapper

自定义模板DO

自定义模板form

自定义模板VO


自定义模板Controller

其中ResponseBean和BaseForm是公司自己的(注意模板引入的jar路径),替换成你们公司的即可

package ${package.Controller};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.metadata.IPage;
#if(${restControllerStyle})
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
import ${package.Service}.${table.serviceName};
import com.mybatis.plus.demo.pojo.${entity}Form;
import com.mybatis.plus.demo.pojo.${entity}VO;
import xxx.ResponseBean;
import xxx.BaseForm;
import java.util.List;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 

* ${entity}前端控制器 *

* * @author ${author} * @since ${date} */ #if(${restControllerStyle}) @RestController #else @Controller #end @Api(tags = "${entity}") @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 @Autowired public ${table.serviceName} ${table.entityPath}Service; /** * 保存单条 * @param param 保存参数 * @return 是否添加成功 */ @ApiOperation(value = "保存", notes = "保存数据到${entity}") @RequestMapping(value = "/add.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) public ResponseBean add${entity}(@RequestBody BaseForm<${entity}Form> param) { Integer result = ${table.entityPath}Service.save(param.getData()); return new ResponseBean(result); } /** * 更新(根据主键id更新) * @param param 修改参数 * @return 是否更改成功 */ @ApiOperation(value = "更新数据", notes = "根据主键id更新${entity}数据") @RequestMapping(value = "/updateById.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) public ResponseBean update${entity}ById(@RequestBody BaseForm<${entity}Form> param) { Integer result = ${table.entityPath}Service.updateById(param.getData()); return new ResponseBean(result); } /** * 删除(根据主键id伪删除) * @param param 主键id * @return 是否删除成功 */ @ApiOperation(value = "删除数据", notes = "根据主键id伪删除${entity}数据") @RequestMapping(value = "/deleteById.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) public ResponseBean delete${entity}ById(@RequestBody BaseForm param) { Integer result = ${table.entityPath}Service.deleteById(param.getData()); return new ResponseBean(result); } /** * 根据主键id查询单条 * @param param 主键id * @return 查询结果 */ @ApiOperation(value = "获取单条数据", notes = "根据主键id获取${entity}数据") @RequestMapping(value = "/getById.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) public ResponseBean get${entity}ById(@RequestBody BaseForm param) { ${entity}VO result = ${table.entityPath}Service.selectById(param.getData()); return new ResponseBean(result); } /** * 查询全部 * @param param 查询条件 * @return 查询结果 */ @ApiOperation(value = "全部查询", notes = "查询${entity}全部数据") @RequestMapping(value = "/queryAll.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) public ResponseBean get${entity}All(@RequestBody BaseForm<${entity}Form> param) { List<${entity}VO> result = ${table.entityPath}Service.selectAll(param.getData()); return new ResponseBean(result); } /** * 分页查询 * @param param 查询条件 * @return 查询结果 */ @ApiOperation(value = "分页查询", notes = "分页查询${entity}全部数据") @RequestMapping(value = "/queryPage.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) public ResponseBean get${entity}Page(@RequestBody BaseForm<${entity}Form> param) { IPage<${entity}VO> result = ${table.entityPath}Service.selectPage(param.getData()); return new ResponseBean(result); } } #end

自定义模板service

package ${package.Service};

import com.baomidou.mybatisplus.core.metadata.IPage;
import ${package.Entity}.${entity};
import com.mybatis.plus.demo.pojo.${entity}Form;
import com.mybatis.plus.demo.pojo.${entity}VO;
import java.util.List;
import ${superServiceClassPackage};

/**
 * 

* ${entity}服务类 *

* * @author ${author} * @since ${date} */ #if(${kotlin}) interface ${table.serviceName} : ${superServiceClass}<${entity}> #else public interface ${table.serviceName} extends ${superServiceClass}<${entity}> { /** * 保存信息对象 * @param record 信息对象 * @return 影响记录数 */ Integer save(${entity}Form record); /** * 根据主键更新信息对象 * @param record 信息对象 * @return 影响记录数 */ Integer updateById(${entity}Form record); /** * 根据主键删除信息对象 * 逻辑删除,字段改为删除态 * @param id 主键 * @return 影响记录数 */ Integer deleteById(String id); /** * 根据主键查询信息对象 * @param id 主键 * @return 信息对象 */ ${entity}VO selectById(String id); /** * 根据主键查询信息对象 * @param record 查询请求条件 * @return 信息列表 */ List<${entity}VO> selectAll(${entity}Form record); /** * 分页查询信息对象 * @param record 查询请求条件 * @return 信息列表 */ IPage<${entity}VO> selectPage(${entity}Form record); } #end

自定义模板serviceImpl

其中一些验证工具类替换为自己公司即可

package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import com.mybatis.plus.demo.pojo.${entity}Form;
import com.mybatis.plus.demo.pojo.${entity}VO;
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import xxx.DateUtil;
import xxx.BaseResultCode;
import xxx.ValidateException;
import xxx.StringUtil;

/**
 * 

* ${entity}服务实现类 *

* * @author ${author} * @since ${date} */ @Service #if(${kotlin}) open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} { } #else public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} { @Override public Integer save(${entity}Form record) { ${entity} data = this.convertDO(record); data.setCreateTime(new Date()); return baseMapper.insert(data); } @Override public Integer updateById(${entity}Form record) { ${entity} data = this.convertDO(record); data.setUpdateTime(new Date()); return baseMapper.updateById(data); } @Override public Integer deleteById(String id) { if(StringUtil.isBlank(id)){ throw new ValidateException(BaseResultCode.PARAM_EMPTY); } return baseMapper.deleteById(id); } @Override public ${entity}VO selectById(String id) { if(StringUtil.isBlank(id)){ throw new ValidateException(BaseResultCode.PARAM_EMPTY); } ${entity} data = baseMapper.selectById(id); ${entity}VO vo = new ${entity}VO(); BeanUtils.copyProperties(data,vo); return vo; } @Override public List<${entity}VO> selectAll(${entity}Form record) { ${entity} data = this.convertDO(record); QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>(); queryWrapper.setEntity(data); List<${entity}> list = baseMapper.selectList(queryWrapper); return this.convert(list); } @Override public IPage<${entity}VO> selectPage(${entity}Form record) { // form -> do 转换 ${entity} data = this.convertDO(record); // 分页数据设置 Page<${entity}> page = new Page<>(record.getCurrent(),record.getSize()); // 查询条件 QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>(); queryWrapper.setEntity(data); IPage<${entity}> list = baseMapper.selectPage(page, queryWrapper); IPage<${entity}VO> iPage = new Page<>(); iPage.setRecords(this.convert(list.getRecords())); iPage.setCurrent(list.getCurrent()); iPage.setSize(list.getSize()); iPage.setTotal(list.getTotal()); iPage.setPages(list.getPages()); return iPage; } /** * Form -> Do * @param form 对象 * @return Do对象 */ private ${entity} convertDO(${entity}Form form){ ${entity} data = new ${entity}(); #foreach($field in ${table.fields}) #if(${field.propertyType.equals("Date")}) data.set${field.capitalName}(DateUtil.parseDateNewFormat(form.get${field.capitalName}())); #else data.set${field.capitalName}(form.get${field.capitalName}()); #end #end return data; } /** * Do -> VO * @param list 对象 * @return VO对象 */ private List<${entity}VO> convert(List<${entity}> list){ List<${entity}VO> ${table.entityPath}List = new ArrayList<>(); if (CollectionUtils.isEmpty(list)) { return ${table.entityPath}List; } for (${entity} source : list) { ${entity}VO target = new ${entity}VO(); BeanUtils.copyProperties(source, target); ${table.entityPath}List.add(target); } return ${table.entityPath}List; } } #end

自定义模板dao

package ${package.Mapper};

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

/**
 * 

* ${entity}Mapper 接口 *

* * @author ${author} * @since ${date} */ #if(${kotlin}) interface ${table.mapperName} : ${superMapperClass}<${entity}> #else public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { } #end

自定义模板mapper

这个主要是业务复杂的情况下编写手动编写sql使用





#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.name},
#end
        ${table.fieldNames}
    

#end

自定义模板DO

package ${package.Entity};

#foreach($pkg in ${table.importPackages})
import ${pkg};
#end
#if(${swagger2})
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
#if(${entityLombokModel})
import lombok.Data;
#end
import com.baomidou.mybatisplus.annotation.TableName;
/**
 * 

* ${entity}对象 *

* * @author ${author} * @since ${date} */ #if(${entityLombokModel}) @Data #end @TableName("${table.name}") #if(${swagger2}) @ApiModel(value="${entity}对象", description="$!{table.comment}") #end #if(${superEntityClass}) public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end { #elseif(${activeRecord}) public class ${entity} extends Model<${entity}> { #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" != "") #if(${swagger2}) @ApiModelProperty(value = "${field.comment}") #else /** * ${field.comment} */ #end #end #if(${field.keyFlag}) ## 主键 #if(${field.keyIdentityFlag}) @TableId(value = "${field.name}", type = IdType.AUTO) #elseif(!$null.isNull(${idType}) && "$!idType" != "") @TableId(value = "${field.name}", type = IdType.${idType}) #elseif(${field.convert}) @TableId("${field.name}") #end ## 普通字段 #elseif(${field.fill}) ## ----- 存在字段填充设置 ----- #if(${field.convert}) @TableField(value = "${field.name}", fill = FieldFill.${field.fill}) #else @TableField(fill = FieldFill.${field.fill}) #end #elseif(${field.convert}) @TableField("${field.name}") #end ## 乐观锁注解 #if(${versionFieldName}==${field.name}) @Version #end ## 逻辑删除注解 #if(${logicDeleteFieldName}==${field.name}) @TableLogic #end private ${field.propertyType} ${field.propertyName}; #end ## ---------- END 字段循环遍历 ---------- #if(!${entityLombokModel}) #foreach($field in ${table.fields}) #if(${field.propertyType.equals("boolean")}) #set($getprefix="is") #else #set($getprefix="get") #end public ${field.propertyType} ${getprefix}${field.capitalName}() { return ${field.propertyName}; } #if(${entityBuilderModel}) public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) { #else public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { #end this.${field.propertyName} = ${field.propertyName}; #if(${entityBuilderModel}) return this; #end } #end #end #if(${entityColumnConstant}) #foreach($field in ${table.fields}) public static final String ${field.name.toUpperCase()} = "${field.name}"; #end #end #if(${activeRecord}) @Override protected Serializable pkVal() { #if(${keyPropertyName}) return this.${keyPropertyName}; #else return null; #end } #end #if(!${entityLombokModel}) @Override public String toString() { return "${entity}{" + #foreach($field in ${table.fields}) #if($!{foreach.index}==0) "${field.propertyName}=" + ${field.propertyName} + #else ", ${field.propertyName}=" + ${field.propertyName} + #end #end "}"; } #end }

自定义模板form

form 中把do中date类型都转化为string类型

package com.mybatis.plus.demo.pojo;

#foreach($pkg in ${table.importPackages})
##import ${pkg};
#end
#if(${swagger2})
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
#if(${entityLombokModel})
#end
import lombok.Data;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
/**
 * 

* ${entity}Form对象 *

* * @author ${author} * @since ${date} */ #if(${entityLombokModel}) @Data #if(${superEntityClass}) #else #end #end @JsonIgnoreProperties(ignoreUnknown = true) #if(${swagger2}) @ApiModel(value="${entity}对象", description="$!{table.comment}") #end #if(${superEntityClass}) public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end { #elseif(${activeRecord}) public class ${entity} extends Model<${entity}> { #else public class ${entity}Form extends Page { #end private static final long serialVersionUID = 1L; ## ---------- BEGIN 字段循环遍历 ---------- #foreach($field in ${table.fields}) #if(${field.keyFlag}) #set($keyPropertyName=${field.propertyName}) #end #if("$!field.comment" != "") #if(${swagger2}) @ApiModelProperty(value = "${field.comment}") #else /** * ${field.comment} */ #end #end #if(${field.keyFlag}) ## 主键 #if(${field.keyIdentityFlag}) ## @TableId(value = "${field.name}", type = IdType.AUTO) #elseif(!$null.isNull(${idType}) && "$!idType" != "") ## @TableId(value = "${field.name}", type = IdType.${idType}) #elseif(${field.convert}) ## @TableId("${field.name}") #end ## 普通字段 #elseif(${field.fill}) ## ----- 存在字段填充设置 ----- #if(${field.convert}) ## @TableField(value = "${field.name}", fill = FieldFill.${field.fill}) #else ## @TableField(fill = FieldFill.${field.fill}) #end #elseif(${field.convert}) ## @TableField("${field.name}") #end ## 乐观锁注解 #if(${versionFieldName}==${field.name}) ## @Version #end ## 逻辑删除注解 #if(${logicDeleteFieldName}==${field.name}) ## @TableLogic #end #if(${field.propertyType.equals("Date")}) private String ${field.propertyName}; #else private ${field.propertyType} ${field.propertyName}; #end #end ## ---------- END 字段循环遍历 ---------- #if(!${entityLombokModel}) #foreach($field in ${table.fields}) #if(${field.propertyType.equals("boolean")}) #set($getprefix="is") #else #set($getprefix="get") #end public ${field.propertyType} ${getprefix}${field.capitalName}() { return ${field.propertyName}; } #if(${entityBuilderModel}) public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) { #else public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { #end this.${field.propertyName} = ${field.propertyName}; #if(${entityBuilderModel}) return this; #end } #end #end #if(${entityColumnConstant}) #foreach($field in ${table.fields}) public static final String ${field.name.toUpperCase()} = "${field.name}"; #end #end #if(${activeRecord}) @Override protected Serializable pkVal() { #if(${keyPropertyName}) return this.${keyPropertyName}; #else return null; #end } #end #if(!${entityLombokModel}) @Override public String toString() { return "${entity}{" + #foreach($field in ${table.fields}) #if($!{foreach.index}==0) "${field.propertyName}=" + ${field.propertyName} + #else ", ${field.propertyName}=" + ${field.propertyName} + #end #end "}"; } #end }

自定义模板VO

package com.mybatis.plus.demo.pojo;

#foreach($pkg in ${table.importPackages})
#end
#if(${swagger2})
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
#if(${entityLombokModel})
#end
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
 * 

* ${entity}VO对象 *

* * @author ${author} * @since ${date} */ #if(${entityLombokModel}) @Data #if(${superEntityClass}) #else #end #end #if(${swagger2}) @ApiModel(value="${entity}对象", description="$!{table.comment}") #end #if(${superEntityClass}) public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end { #elseif(${activeRecord}) public class ${entity} extends Model<${entity}> { #else public class ${entity}VO 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" != "") #if(${swagger2}) @ApiModelProperty(value = "${field.comment}") #else /** * ${field.comment} */ #end #end #if(${field.keyFlag}) ## 主键 #if(${field.keyIdentityFlag}) ## @TableId(value = "${field.name}", type = IdType.AUTO) #elseif(!$null.isNull(${idType}) && "$!idType" != "") ## @TableId(value = "${field.name}", type = IdType.${idType}) #elseif(${field.convert}) ## @TableId("${field.name}") #end ## 普通字段 #elseif(${field.fill}) ## ----- 存在字段填充设置 ----- #if(${field.convert}) ## @TableField(value = "${field.name}", fill = FieldFill.${field.fill}) #else ## @TableField(fill = FieldFill.${field.fill}) #end #elseif(${field.convert}) ## @TableField("${field.name}") #end ## 乐观锁注解 #if(${versionFieldName}==${field.name}) ## @Version #end ## 逻辑删除注解 #if(${logicDeleteFieldName}==${field.name}) ## @TableLogic #end ###if(${field.propertyType.equals("Date")}) ## private String ${field.propertyName}; ###else private ${field.propertyType} ${field.propertyName}; ###end #end ## ---------- END 字段循环遍历 ---------- #if(!${entityLombokModel}) #foreach($field in ${table.fields}) #if(${field.propertyType.equals("boolean")}) #set($getprefix="is") #else #set($getprefix="get") #end public ${field.propertyType} ${getprefix}${field.capitalName}() { return ${field.propertyName}; } #if(${entityBuilderModel}) public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) { #else public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { #end this.${field.propertyName} = ${field.propertyName}; #if(${entityBuilderModel}) return this; #end } #end #end #if(${entityColumnConstant}) #foreach($field in ${table.fields}) public static final String ${field.name.toUpperCase()} = "${field.name}"; #end #end #if(${activeRecord}) @Override protected Serializable pkVal() { #if(${keyPropertyName}) return this.${keyPropertyName}; #else return null; #end } #end #if(!${entityLombokModel}) @Override public String toString() { return "${entity}{" + #foreach($field in ${table.fields}) #if($!{foreach.index}==0) "${field.propertyName}=" + ${field.propertyName} + #else ", ${field.propertyName}=" + ${field.propertyName} + #end #end "}"; } #end }

记录自己的Mybatis-plus之路03-mp的CRUD分页操作

你可能感兴趣的:(后端开发)