語法
##
是模板的註釋,不會生成#
全局的一些設置$
使用庫函數或者調用全局定義的內容
常用方法
- 數據庫表信息
${tableInfo}
comment
表備註name
表名fullColumn
所有列comment
列備註name
列名稱type
列類型
pkColumn
主鍵列otherColumn
其他列
常用功能
- 循環
#foreach($column in $tableInfo.fullColumn)
#end
- 判斷,是否包含字符串
#if($!{tool.getClsNameByFullName($column.type).contains('Date')})
#end
- 小寫轉換,把第一個字母轉換爲小寫
$!tool.firstLowerCase($!tableInfo.name)
示例
controller 自定義函數比較多,需要按照需要重寫,前端也是可以生成的,通用性很差,根據實際情況生成
entity
##導入宏定義
$!define
##保存文件(宏定義)
#save("/entity", ".java")
##包路徑(宏定義)
#setPackageSuffix("entity")
##自動導入包(全局變量)
$!autoImport
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.IdType;
##表註釋(宏定義)
#tableComment("表實體類")
@Data
@TableName
public class $!{tableInfo.name} implements Serializable {
private static final long serialVersionUID = -1;
/**
* 自增 => @TableId(type = IdType.AUTO)
*/
#foreach($column in $tableInfo.pkColumn)
#if(${column.comment})
/*${column.comment}*/#end
@TableId
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#foreach($column in $tableInfo.otherColumn)
#if(${column.comment})
/*${column.comment}*/#end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}
mapper
##導入宏定義
$!define
##設置表後綴(宏定義)
#setTableSuffix("Mapper")
##保存文件(宏定義)
#save("/mapper", "Mapper.java")
##包路徑(宏定義)
#setPackageSuffix("mapper")
$!autoImport
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
##表註釋(宏定義)
#tableComment("表數據庫訪問層")
@Mapper
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {
}
service
##導入宏定義
$!define
##設置表後綴(宏定義)
#setTableSuffix("Service")
##保存文件(宏定義)
#save("/service", "Service.java")
##包路徑(宏定義)
#setPackageSuffix("service")
$!autoImport
import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
##表註釋(宏定義)
#tableComment("表服務接口")
public interface $!{tableName} extends IService<$!tableInfo.name> {
}
serviceImpl
##導入宏定義
$!define
##設置表後綴(宏定義)
#setTableSuffix("ServiceImpl")
##保存文件(宏定義)
#save("/service/impl", "ServiceImpl.java")
##包路徑(宏定義)
#setPackageSuffix("service.impl")
$!autoImport
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
##表註釋(宏定義)
#tableComment("表服務實現類")
@Service("$!tool.firstLowerCase($tableInfo.name)Service")
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
}
controller
##導入宏定義
$!define
##設置表後綴(宏定義)
#setTableSuffix("Controller")
##保存文件(宏定義)
#save("/controller", "Controller.java")
##包路徑(宏定義)
#setPackageSuffix("controller")
##定義服務名
#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))
##定義實體對象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))
$!autoImport
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.common.core.utils.Res;
import com.test.common.core.utils.Result;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import lombok.AllArgsConstructor;
import com.test.business.entity.CommonQuery;
import com.test.business.utils.AutoQueryGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
##表註釋(宏定義)
#tableComment("表控制層")
@RestController
@RequestMapping("/$!tool.firstLowerCase($!tableInfo.name)")
@AllArgsConstructor
@Slf4j
@Api(tags = "${tableInfo.comment}(${tableInfo.name})APi")
public class $!{tableName} {
/**
* 服務對象
*/
private final $!{tableInfo.name}Service $!{serviceName};
/**
* 分頁查詢所有數據
*
* @param page 分頁對象
* @param $!entityName 查詢實體
* @return 所有數據
*/
@GetMapping("/page")
@ApiOperation(value = "分頁查詢${tableInfo.comment}")
public Result listPageAll(Page<$!tableInfo.name> page, $!tableInfo.name $!entityName) {
return Res.ok($!{serviceName}.page(page, new LambdaQueryWrapper<$!tableInfo.name>()));
}
/**
* 高級查詢數據
*
* @param page 分頁對象
* @param $!entityName 查詢實體
* @return 所有數據
*/
@GetMapping("/queryPage")
@ApiOperation(value = "高級分頁查詢${tableInfo.comment}")
public Result listPageAll(Page<$!tableInfo.name> page, $!tableInfo.name $!entityName, CommonQuery commonQuery) {
QueryWrapper<$!tableInfo.name> autoQueryWrapper = AutoQueryGenerator.initQueryWrapper($!entityName, commonQuery);
return Res.ok($!{serviceName}.page(page, autoQueryWrapper));
}
/**
* 高級查詢所有數據
*
* @param $!entityName 查詢實體
* @return 所有數據
*/
@GetMapping("/list")
@ApiOperation(value = "高級查詢全部${tableInfo.comment}")
public Result listAll($!tableInfo.name $!entityName, CommonQuery commonQuery) {
QueryWrapper<$!tableInfo.name> autoQueryWrapper = AutoQueryGenerator.initQueryWrapper($!entityName, commonQuery);
return Res.ok($!{serviceName}.list(autoQueryWrapper));
}
/**
* 通過主鍵查詢單條數據
*
* @param id 主鍵
* @return 單條數據
*/
@GetMapping("/{id}")
@ApiOperation(value = "獲取單個${tableInfo.comment}")
public Result selectOne(@PathVariable("id") Long id) {
return Res.ok($!{serviceName}.getById(id));
}
/**
* 新增數據
*
* @param $!entityName 實體對象
* @return 新增結果
*/
@PostMapping
@ApiOperation(value = "新增${tableInfo.comment}")
public Result insert(@RequestBody $!tableInfo.name $!entityName) {
return Res.ok($!{serviceName}.save($!entityName));
}
/**
* 修改數據
*
* @param $!entityName 實體對象
* @return 修改結果
*/
@PutMapping
@ApiOperation(value = "修改${tableInfo.comment}")
public Result update(@RequestBody $!tableInfo.name $!entityName) {
return Res.ok($!{serviceName}.updateById($!entityName));
}
/**
* 刪除數據
*
* @param id 主鍵結合
* @return 刪除結果
*/
@DeleteMapping("/{id}")
@ApiOperation(value = "刪除${tableInfo.comment}")
public Result delete(@PathVariable("id") Long id) {
return Res.ok($!{serviceName}.removeById(id));
}
}