作用:自動生成模板代碼和實(shí)體類,簡化開發(fā)人員工作,降低錯誤率。
前置配置,去除指定前綴(“sys_”改成你要去掉的前綴)
##初始化區(qū)域
##去掉表的t_前綴
$!tableInfo.setName($tool.getClassName($tableInfo.obj.name.replaceFirst("sys_","")))
##參考阿里巴巴開發(fā)手冊,POJO 類中布爾類型的變量,都不要加 is 前綴,否則部分框架解析會引起序列化錯誤
#foreach($column in $tableInfo.fullColumn)
#if($column.name.startsWith("is") && $column.type.equals("java.lang.Boolean"))
$!column.setName($tool.firstLowerCase($column.name.substring(2)))
#end
#end
##實(shí)現(xiàn)動態(tài)排除列
#set($temp = $tool.newHashSet("testCreateTime", "otherColumn"))
#foreach($item in $temp)
#set($newList = $tool.newArrayList())
#foreach($column in $tableInfo.fullColumn)
#if($column.name!=$item)
##帶有反回值的方法調(diào)用時使用$tool.call來消除返回值
$tool.call($newList.add($column))
#end
#end
##重新保存
$tableInfo.setFullColumn($newList)
#end
##對importList進(jìn)行篡改
#set($temp = $tool.newHashSet())
#foreach($column in $tableInfo.fullColumn)
#if(!$column.type.startsWith("java.lang."))
##帶有反回值的方法調(diào)用時使用$tool.call來消除返回值
$tool.call($temp.add($column.type))
#end
#end
##覆蓋
#set($importList = $temp)
1.entity-注意if下面留空格,不然影響下一行對齊,造成下一行錯位
?
##導(dǎo)入宏定義
$!init
$!define
##保存文件(宏定義)
#save("/entity", ".java")
##包路徑(宏定義)
#setPackageSuffix("entity")
##自動導(dǎo)入包(全局變量)
$!autoImport
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
##表注釋(宏定義)
#tableComment("表實(shí)體類")
@Data
@TableName("$tableInfo.obj.name")
@ApiModel(value="$!{tableInfo.name}",description="$tableInfo.comment實(shí)體類")
public class $!{tableInfo.name} extends Model<$!{tableInfo.name}> implements Serializable {
#set($newList = $tool.newArrayList("createBy","updateBy","createTime","updateTime"))
#foreach($column in $tableInfo.fullColumn)
##if(${column.comment})/**
##* ${column.comment}
##*/#end
#if($newList.contains($column.name))
@ApiModelProperty(name = "$!{column.name}", value = "$column.comment",hidden=true)
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#elseif($tableInfo.pkColumn[0].name.equals($column.name))
@TableId(type=IdType.ID_WORKER)
@ApiModelProperty(name = "$!{column.name}", value = "$column.comment")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#else
@ApiModelProperty(name = "$!{column.name}", value = "$column.comment")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#end
private static final long serialVersionUID = $!tool.serial();
}
?
2.dao
?
##導(dǎo)入宏定義
$!init
$!define
##設(shè)置表后綴(宏定義)
#setTableSuffix("Dao")
##保存文件(宏定義)
#save("/dao", "Dao.java")
##包路徑(宏定義)
#setPackageSuffix("dao")
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import org.apache.ibatis.annotations.Mapper;
##表注釋(宏定義)
#tableComment("表數(shù)據(jù)庫訪問層")
@Mapper
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {
}
?
3.impl
?
##導(dǎo)入宏定義
$!init
$!define
##設(shè)置表后綴(宏定義)
#setTableSuffix("ServiceImpl")
##保存文件(宏定義)
#save("/service/impl", "ServiceImpl.java")
##包路徑(宏定義)
#setPackageSuffix("service.impl")
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
##表注釋(宏定義)
#tableComment("表服務(wù)實(shí)現(xiàn)類")
@Service
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
}
4.service
?
##導(dǎo)入宏定義
$!init
$!define
##設(shè)置表后綴(宏定義)
#setTableSuffix("Service")
##保存文件(宏定義)
#save("/service", "Service.java")
##包路徑(宏定義)
#setPackageSuffix("service")
import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
##表注釋(宏定義)
#tableComment("表服務(wù)接口")
public interface $!{tableName} extends IService<$!tableInfo.name> {
}
?
5.controller
##導(dǎo)入宏定義
$!init
$!define
##設(shè)置表后綴(宏定義)
#setTableSuffix("Controller")
##保存文件(宏定義)
#save("/controller", "Controller.java")
##包路徑(宏定義)
#setPackageSuffix("controller")
##定義服務(wù)名
#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))
##定義實(shí)體對象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))
#set($PkType = $!{tool.getClsNameByFullName($!tableInfo.pkColumn[0].type)})
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
##表注釋(宏定義)
#tableComment("表控制層")
@RestController
@Api(tags = "$!{tableInfo.comment}($!{tableInfo.name})")
@RequestMapping("$!tool.firstLowerCase($!tableInfo.name)")
public class $!{tableName} extends ApiController {
/**
* 服務(wù)對象
*/
@Resource
private $!{tableInfo.name}Service $!{serviceName};
/**
* 分頁查詢所有數(shù)據(jù)
*
* @param page 分頁對象
* @param $!entityName 查詢實(shí)體
* @return 所有數(shù)據(jù)
*/
@ApiOperation(value = "分頁查全部")
@GetMapping("/selectAll")
public R selectAll(Page<$!tableInfo.name> page,$!tableInfo.name $!entityName) {
return success(this.$!{serviceName}.page(page, new QueryWrapper<>($!entityName)));
}
/**
* 通過主鍵查詢單條數(shù)據(jù)
*
* @param id 主鍵
* @return 單條數(shù)據(jù)
*/
@ApiOperation(value = "根據(jù)id查")
@GetMapping("{id}")
public R<$!tableInfo.name> selectOne(@PathVariable("id") $!{PkType} id) {
return success(this.$!{serviceName}.getById(id));
}
/**
* 新增數(shù)據(jù)
*
* @param $!entityName 實(shí)體對象
* @return 新增結(jié)果
*/
@ApiOperationSupport(ignoreParameters = {"$tool.append($!entityName,".id")"})
@ApiOperation(value = "添加")
@PostMapping("/add")
public R<Boolean> insert(@RequestBody $!tableInfo.name $!entityName) {
return success(this.$!{serviceName}.save($!entityName));
}
/**
* 修改數(shù)據(jù)
*
* @param $!entityName 實(shí)體對象
* @return 修改結(jié)果
*/
@ApiOperation(value = "更新")
@PutMapping("/update")
public R<Boolean> update(@RequestBody $!tableInfo.name $!entityName) {
return success(this.$!{serviceName}.updateById($!entityName));
}
/**
* 刪除數(shù)據(jù)
*
* @param idList 主鍵結(jié)合
* @return 刪除結(jié)果
*/
@ApiOperation(value = "刪除")
@DeleteMapping("/del")
public R<Boolean> delete(@RequestParam("idList") List<$!{PkType}> idList) {
return success(this.$!{serviceName}.removeByIds(idList));
}
}
?
本文摘自 :https://blog.51cto.com/u