文章目录 1.生成步骤 1.定义全局逻辑删除字段名称以及删除值和未删除值 2.简单三层架构模板 1.概览 2.Req.java 3.Vo.java 4.Po.java 5.Mapper.java 6.Mapper.xml 7.Service.java 8.ServiceImpl.java 9.Controller.java 3.通用配置生成模板 1.概览 2.PageInfo.java 3.PageResult.java 4.SunPageHelper.java 5.Result.java 6.RespBeanEnum.java 2.生成效果 3.导出到本地
1.生成步骤
1.定义全局逻辑删除字段名称以及删除值和未删除值
2.简单三层架构模板
1.概览
2.Req.java
## 引入宏定义
$! { define. vm} ## 设置保存名称与保存位置
$! callback. setFileName ( $tool. append ( $! { tableInfo. name} , "Req.java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/entity/req" ) ) ## 拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) ) #set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#end## 包声明和导入
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . entity. req;
#endimport $! { tableInfo. savePackageName} . entity. page. PageInfo;
import lombok. Data ;
import lombok. experimental. Accessors ;
import java. io. Serializable ;
## 检查是否包含 Date 类型的字段
#set ( $containsDate = false )
#foreach ( $column in $tableInfo. fullColumn) #if ( $tool. getClsNameByFullName ( $column. type) == "Date" ) #set ( $containsDate = true ) #break #end
#end
## 如果包含 Date 类型的字段,导入 java. util. Date
#if ( $containsDate)
import java. util. Date ;
#end
@Data
@Accessors ( chain = true )
public class $! { tableInfo. name} Req extends PageInfo implements Serializable { private static final long serialVersionUID = 1L ; #foreach ( $column in $tableInfo. fullColumn) ## 添加字段注释#if ( $column. comment) #endprivate $! { tool. getClsNameByFullName ( $column. type) } $! { column. name} ; #end
}
3.Vo.java
## 引入宏定义
$! { define. vm} ## 设置保存名称与保存位置
$! callback. setFileName ( $tool. append ( $! { tableInfo. name} , "Vo.java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/entity/vo" ) ) ## 拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) ) #set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#end## 包声明和导入
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . entity. vo;
#endimport lombok. Data ;
import lombok. experimental. Accessors ;
import java. io. Serializable ;
## 检查是否包含 Date 类型的字段
#set ( $containsDate = false )
#foreach ( $column in $tableInfo. fullColumn) #if ( $tool. getClsNameByFullName ( $column. type) == "Date" ) #set ( $containsDate = true ) #break #end
#end
## 如果包含 Date 类型的字段,导入 java. util. Date
#if ( $containsDate)
import java. util. Date ;
#end
@Data
@Accessors ( chain = true )
public class $! { tableInfo. name} Vo implements Serializable { private static final long serialVersionUID = 1L ; #foreach ( $column in $tableInfo. fullColumn) ## 添加字段注释#if ( $column. comment) #endprivate $! { tool. getClsNameByFullName ( $column. type) } $! { column. name} ; #end
}
4.Po.java
## 引入宏定义
$! { define. vm} ## 设置保存名称与保存位置
$! callback. setFileName ( $tool. append ( $! { tableInfo. name} , "Po.java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/entity/po" ) ) ## 拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) ) #set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#end## 包声明和导入
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . entity. po;
#endimport lombok. Data ;
import lombok. experimental. Accessors ;
import java. io. Serializable ;
## 检查是否包含 Date 类型的字段
#set ( $containsDate = false )
#foreach ( $column in $tableInfo. fullColumn) #if ( $tool. getClsNameByFullName ( $column. type) == "Date" ) #set ( $containsDate = true ) #break #end
#end
## 如果包含 Date 类型的字段,导入 java. util. Date
#if ( $containsDate)
import java. util. Date ;
#end
@Data
@Accessors ( chain = true )
public class $! { tableInfo. name} Po implements Serializable { private static final long serialVersionUID = 1L ; #foreach ( $column in $tableInfo. fullColumn) ## 添加字段注释#if ( $column. comment) #endprivate $! { tool. getClsNameByFullName ( $column. type) } $! { column. name} ; #end
}
5.Mapper.java
## 设置保存名称与保存位置
$! callback. setFileName ( $tool. append ( $! { tableInfo. name} , "Mapper.java" ) )
$! callback. setSavePath ( $tool. append ( $modulePath, "/src/main/java/$!{tableInfo.savePackageName.replace('.','/')}/mapper" ) )
##拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) ) #set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#endpackage $! { tableInfo. savePackageName} . mapper; import $! { tableInfo. savePackageName} . entity. po. $! { tableInfo. name} Po ;
import org. apache. ibatis. annotations. Param ; import java. util. List ;
public interface $! { tableInfo. name} Mapper { $! { tableInfo. name} Po queryById ( @Param ( "id" ) $! { pk. shortType} id) ; List < $! { tableInfo. name} Po > queryPage ( @Param ( "po" ) $! { tableInfo. name} Po po, @Param ( "offset" ) Integer offset, @Param ( "pageSize" ) Integer pageSize) ; List < $! { tableInfo. name} Po > queryAllByLimit ( @Param ( "po" ) $! { tableInfo. name} Po po) ; Integer count ( @Param ( "po" ) $! { tableInfo. name} Po po) ; int insert ( $! { tableInfo. name} Po po) ; int insertBatch ( @Param ( "entities" ) List < $! { tableInfo. name} Po > entities) ; int insertOrUpdateBatch ( @Param ( "entities" ) List < $! { tableInfo. name} Po > entities) ; int updateById ( $! { tableInfo. name} Po po) ; int logicDeleteById ( @Param ( "id" ) $! { pk. shortType} id) ; int logicDeleteBatchById ( @Param ( "list" ) List < $! { pk. shortType} > ids) ;
}
6.Mapper.xml
##定义全局逻辑删除字段名称
#set($deleteColumnName = "is_deleted")
##定义全局逻辑删除字段的删除值和未删除值
#set($deleteColumnValue = 1)
#set($notDeletedColumnValue = 0)##引入mybatis支持
$!{mybatisSupport.vm}##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())#set($pk = $tableInfo.pkColumn.get(0))
#end<?xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper" > < resultMap type = " $!{tableInfo.savePackageName}.entity.po.$!{tableInfo.name}Po" id = " $!{tableInfo.name}Map" > < id property = " $!pk.name" column = " $!pk.obj.name" />
#foreach($column in $tableInfo.fullColumn)#if($column.name != $pk.name)< result property = " $!column.name" column = " $!column.obj.name" /> #end
#end</ resultMap> < select id = " queryById" resultMap = " $!{tableInfo.name}Map" > select #allSqlColumn()from $!{tableInfo.obj.name}where $!pk.obj.name = #{$!pk.name}and $!deleteColumnName = $notDeletedColumnValue</ select> < select id = " queryPage" resultMap = " $!{tableInfo.name}Map" > select #allSqlColumn()from $!{tableInfo.obj.name}< where>
#foreach($column in $tableInfo.fullColumn)< if test = " po.$!column.name != null#if($column.type.equals('java.lang.String')) and po.$!column.name != ''#end" > and $!column.obj.name = #{po.$!column.name}</ if>
#endand $!deleteColumnName = $notDeletedColumnValue</ where> limit #{offset}, #{pageSize}</ select> < select id = " queryAllByLimit" resultMap = " $!{tableInfo.name}Map" > select #allSqlColumn()from $!{tableInfo.obj.name}< where>
#foreach($column in $tableInfo.fullColumn)< if test = " po.$!column.name != null#if($column.type.equals('java.lang.String')) and po.$!column.name != ''#end" > and $!column.obj.name = #{po.$!column.name}</ if>
#endand $!deleteColumnName = $notDeletedColumnValue</ where> </ select> < select id = " count" resultType = " java.lang.Integer" > select count(1)from $!{tableInfo.obj.name}< where>
#foreach($column in $tableInfo.fullColumn)< if test = " po.$!column.name != null#if($column.type.equals('java.lang.String')) and po.$!column.name != ''#end" > and $!column.obj.name = #{po.$!column.name}</ if>
#endand $!deleteColumnName = $notDeletedColumnValue</ where> </ select> < insert id = " insert" keyProperty = " $!pk.name" useGeneratedKeys = " true" > insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($foreach.hasNext), #end#end)values (#foreach($column in $tableInfo.otherColumn)#{$!column.name}#if($foreach.hasNext), #end#end)</ insert> < insert id = " insertBatch" keyProperty = " $!pk.name" useGeneratedKeys = " true" > insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($foreach.hasNext), #end#end)values< foreach collection = " entities" item = " entity" separator = " ," > (#foreach($column in $tableInfo.otherColumn)#{entity.$!column.name}#if($foreach.hasNext), #end#end)</ foreach> </ insert> < insert id = " insertOrUpdateBatch" keyProperty = " $!pk.name" useGeneratedKeys = " true" > insert into $!{tableInfo.obj.name}(id, #foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($foreach.hasNext), #end#end)values< foreach collection = " entities" item = " entity" separator = " ," > (#{entity.id}, #foreach($column in $tableInfo.otherColumn)#{entity.$!column.name}#if($foreach.hasNext), #end#end)</ foreach> on duplicate key update
#foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name)#if($foreach.hasNext),#end
#end</ insert> < update id = " updateById" > update $!{tableInfo.obj.name}< set>
#foreach($column in $tableInfo.otherColumn)< if test = " $!column.name != null#if($column.type.equals('java.lang.String')) and $!column.name != ''#end" > $!column.obj.name = #{$!column.name},</ if>
#end</ set> where $!pk.obj.name = #{$!pk.name}and $!deleteColumnName = $notDeletedColumnValue</ update> < update id = " logicDeleteById" > update $!{tableInfo.obj.name}set $!deleteColumnName = $deleteColumnValuewhere $!pk.obj.name = #{$!pk.name}and $!deleteColumnName = $notDeletedColumnValue</ update> < update id = " logicDeleteBatchById" > update $!{tableInfo.obj.name}set $!deleteColumnName = $deleteColumnValuewhere $!pk.obj.name in< foreach collection = " list" item = " id" open = " (" separator = " ," close = " )" > #{id}</ foreach> and $!deleteColumnName = $notDeletedColumnValue</ update>
</ mapper>
7.Service.java
## 引入宏定义
$! { define. vm} ## 设置保存名称与保存位置
$! callback. setFileName ( $tool. append ( $! { tableInfo. name} , "Service.java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/service" ) ) ## 拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) ) #set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#end## 包声明和导入
package $! { tableInfo. savePackageName} . service; import $! { tableInfo. savePackageName} . entity. page. PageResult;
import $! { tableInfo. savePackageName} . entity. req. $! { tableInfo. name} Req ;
import $! { tableInfo. savePackageName} . entity. vo. $! { tableInfo. name} Vo ;
public interface $! { tableInfo. name} Service { PageResult < $! { tableInfo. name} Vo > queryByPage ( $! { tableInfo. name} Req req) ; }
8.ServiceImpl.java
## 引入宏定义
$! { define. vm} ## 设置保存名称与保存位置
$! callback. setFileName ( $tool. append ( $! { tableInfo. name} , "ServiceImpl.java" ) )
$! callback. setSavePath ( $tool. append ( $! { tableInfo. savePath} , "/service/impl" ) ) ## 拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) ) #set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#end## 包声明和导入
package $! { tableInfo. savePackageName} . service. impl; import $! { tableInfo. savePackageName} . entity. page. PageResult;
import $! { tableInfo. savePackageName} . entity. page. SunPageHelper;
import $! { tableInfo. savePackageName} . entity. po. $! { tableInfo. name} Po ;
import $! { tableInfo. savePackageName} . entity. req. $! { tableInfo. name} Req ;
import $! { tableInfo. savePackageName} . entity. vo. $! { tableInfo. name} Vo ;
import $! { tableInfo. savePackageName} . mapper. $! { tableInfo. name} Mapper ;
import $! { tableInfo. savePackageName} . service. $! { tableInfo. name} Service ;
import org. springframework. stereotype. Service ;
import javax. annotation. Resource ;
import java. util. List ;
@Service ( "$!tool.firstLowerCase($!{tableInfo.name})Service" )
public class $! { tableInfo. name} ServiceImpl implements $! { tableInfo. name} Service { @Resource private $! { tableInfo. name} Mapper $! tool. firstLowerCase ( $! { tableInfo. name} ) Mapper ; @Override public PageResult < $! { tableInfo. name} Vo > queryByPage ( $! { tableInfo. name} Req $! tool. firstLowerCase ( $! { tableInfo. name} ) Req ) { $! { tableInfo. name} Po $! tool. firstLowerCase ( $! { tableInfo. name} ) Po = convertReqToPo ( $! tool. firstLowerCase ( $! { tableInfo. name} ) Req ) ; PageResult < $! { tableInfo. name} Po > paginate = SunPageHelper . paginate ( $! tool. firstLowerCase ( $! { tableInfo. name} ) Req . getPageNo ( ) , $! tool. firstLowerCase ( $! { tableInfo. name} ) Req . getPageSize ( ) , ( ) -> $! tool. firstLowerCase ( $! { tableInfo. name} ) Mapper . count ( $! tool. firstLowerCase ( $! { tableInfo. name} ) Po ) , ( offset, size) -> $! tool. firstLowerCase ( $! { tableInfo. name} ) Mapper . queryPage ( $! tool. firstLowerCase ( $! { tableInfo. name} ) Po , offset, size) ) ; List < $! { tableInfo. name} Vo > $! tool. firstLowerCase ( $! { tableInfo. name} ) VoList = convertPoListToVoList ( paginate. getResult ( ) ) ; PageResult < $! { tableInfo. name} Vo > $! tool. firstLowerCase ( $! { tableInfo. name} ) VoPageResult = new PageResult. Builder < $! { tableInfo. name} Vo > ( ) . pageNo ( paginate. getPageNo ( ) ) . pageSize ( paginate. getPageSize ( ) ) . total ( paginate. getTotal ( ) ) . result ( $! tool. firstLowerCase ( $! { tableInfo. name} ) VoList ) . build ( ) ; return $! tool. firstLowerCase ( $! { tableInfo. name} ) VoPageResult ; } private List < $! { tableInfo. name} Vo > convertPoListToVoList ( List < $! { tableInfo. name} Po > result) { return null ; } private $! { tableInfo. name} Po convertReqToPo ( $! { tableInfo. name} Req $! tool. firstLowerCase ( $! { tableInfo. name} ) Req ) { return null ; } }
9.Controller.java
## 引入宏定义
$! { define. vm} ## 设置保存名称与保存位置
$! callback. setFileName ( $tool. append ( $! { tableInfo. name} , "Controller.java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/controller" ) ) ## 拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) ) #set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#end## 包声明和导入
package $! { tableInfo. savePackageName} . controller; import $! { tableInfo. savePackageName} . entity. page. PageResult;
import $! { tableInfo. savePackageName} . entity. req. $! { tableInfo. name} Req ;
import $! { tableInfo. savePackageName} . entity. vo. $! { tableInfo. name} Vo ;
import $! { tableInfo. savePackageName} . response. Result;
import $! { tableInfo. savePackageName} . service. $! { tableInfo. name} Service ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. RequestBody ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ; import javax. annotation. Resource ;
@RestController
@RequestMapping ( "/$tool.hump2Underline($tool.firstLowerCase($tableInfo.name)).replace('_', '/')" )
@Slf4j
public class $! { tableInfo. name} Controller { @Resource private $! { tableInfo. name} Service $! tool. firstLowerCase ( $tableInfo. name) Service ; @GetMapping ( "/queryPage" ) public Result < PageResult < $! { tableInfo. name} Vo >> queryByPage ( @RequestBody $! { tableInfo. name} Req req) { PageResult < $! { tableInfo. name} Vo > $! tool. firstLowerCase ( $tableInfo. name) VoPageResult = this . $! tool. firstLowerCase ( $tableInfo. name) Service . queryByPage ( req) ; return Result . ok ( $! tool. firstLowerCase ( $tableInfo. name) VoPageResult ) ; }
}
3.通用配置生成模板
1.概览
2.PageInfo.java
##定义初始变量
#set ( $className = "PageInfo" )
##设置回调
$! callback. setFileName ( $className + ".java" )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/entity/page" ) )
##包名
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . entity. page; #endimport java. util. Objects ;
public class $className { private Integer pageNo = 1 ; private Integer pageSize = 20 ; public Integer getPageNo ( ) { return ( pageNo == null || pageNo < 1 ) ? 1 : pageNo; } public Integer getPageSize ( ) { return ( pageSize == null || pageSize < 1 ) ? 20 : pageSize; } public $className setPageNo ( Integer pageNo) { this . pageNo = pageNo; return this ; } public $className setPageSize ( Integer pageSize) { this . pageSize = pageSize; return this ; } @Override public int hashCode ( ) { return Objects . hash ( pageNo, pageSize) ; } @Override public String toString ( ) { return "$className{" + "pageNo=" + pageNo + ", pageSize=" + pageSize + '}' ; }
}
3.PageResult.java
##定义初始变量
#set ( $className = "PageResult" )
##设置回调
$! callback. setFileName ( $className + ".java" )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/entity/page" ) )
##包名
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . entity. page; #endimport java. util. Collections ;
import java. util. List ; import static java. util. Objects . requireNonNull ;
public class $className< T > { private Integer pageNo = 1 ; private Integer pageSize = 20 ; private Integer total = 0 ; private Integer totalPages = 0 ; private List < T > result = Collections . emptyList ( ) ; private Integer start = 1 ; private Integer end = 0 ; private $className ( Builder < T > builder) { this . pageNo = builder. pageNo; this . pageSize = builder. pageSize; this . total = builder. total; this . result = builder. result; calculateTotalPages ( ) ; calculateStartAndEnd ( ) ; } public static class Builder < T > { private Integer pageNo = 1 ; private Integer pageSize = 20 ; private Integer total = 0 ; private List < T > result = Collections . emptyList ( ) ; public Builder < T > pageNo ( Integer pageNo) { this . pageNo = requireNonNull ( pageNo, "Page number cannot be null" ) ; return this ; } public Builder < T > pageSize ( Integer pageSize) { this . pageSize = requireNonNull ( pageSize, "Page size cannot be null" ) ; return this ; } public Builder < T > total ( Integer total) { this . total = requireNonNull ( total, "Total count cannot be null" ) ; return this ; } public Builder < T > result ( List < T > result) { this . result = requireNonNull ( result, "Result list cannot be null" ) ; return this ; } public $className< T > build ( ) { return new $className< > ( this ) ; } } private void calculateTotalPages ( ) { if ( this . pageSize > 0 ) { this . totalPages = ( this . total / this . pageSize) + ( this . total % this . pageSize == 0 ? 0 : 1 ) ; } else { this . totalPages = 0 ; } } private void calculateStartAndEnd ( ) { if ( this . pageSize > 0 ) { this . start = ( this . pageNo - 1 ) * this . pageSize + 1 ; this . end = Math . min ( this . pageNo * this . pageSize, this . total) ; } else { this . start = 1 ; this . end = this . total; } } public Integer getStart ( ) { return start; } public Integer getPageSize ( ) { return pageSize; } public Integer getPageNo ( ) { return pageNo; } public Integer getTotal ( ) { return total; } public Integer getTotalPages ( ) { return totalPages; } public List < T > getResult ( ) { return result; } public Integer getEnd ( ) { return end; } @Override public String toString ( ) { return "$className{" + "pageNo=" + pageNo + ", pageSize=" + pageSize + ", total=" + total + ", totalPages=" + totalPages + ", result=" + result + ", start=" + start + ", end=" + end + '}' ; }
}
4.SunPageHelper.java
##定义初始变量
#set ( $className = "SunPageHelper" )
##设置回调
$! callback. setFileName ( $className + ".java" )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/entity/page" ) )
##包名
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . entity. page; #endimport java. util. List ;
import java. util. function. BiFunction ;
import java. util. function. Supplier ;
public class $className { public static < T > PageResult < T > paginate ( int pageNo, int pageSize, Supplier < Integer > totalSupplier, BiFunction < Integer , Integer , List < T > > recordsSupplier) { int total; try { total = totalSupplier. get ( ) ; } catch ( Exception e) { throw new RuntimeException ( "Failed to get total count" , e) ; } if ( total == 0 ) { return new PageResult. Builder < T > ( ) . pageNo ( pageNo) . pageSize ( pageSize) . total ( total) . result ( Collections . emptyList ( ) ) . build ( ) ; } int offset = calculateOffset ( pageNo, pageSize) ; List < T > records; try { records = recordsSupplier. apply ( offset, pageSize) ; } catch ( Exception e) { throw new RuntimeException ( "Failed to get records" , e) ; } return new PageResult. Builder < T > ( ) . pageNo ( pageNo) . pageSize ( pageSize) . total ( total) . result ( records) . build ( ) ; } public static int calculateOffset ( int pageNo, int pageSize) { return ( pageNo - 1 ) * pageSize; }
}
5.Result.java
## 引入宏定义
$! { define. vm} ## 设置保存名称与保存位置
$! callback. setFileName ( "Result.java" )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/response" ) ) ## 包声明
package $! { tableInfo. savePackageName} . response; ## 包导入
import lombok. Data ;
import java. io. Serializable ;
@Data
public class Result < T > implements Serializable { private static final long serialVersionUID = 1L ; private boolean success; private int code; private String message; private T data; private Result ( ) { } public static class Builder < T > { private boolean success; private int code; private String message; private T data; public Builder < T > success ( boolean success) { this . success = success; return this ; } public Builder < T > code ( int code) { this . code = code; return this ; } public Builder < T > message ( String message) { this . message = message; return this ; } public Builder < T > data ( T data) { this . data = data; return this ; } public Result < T > build ( ) { Result < T > result = new Result < > ( ) ; result. success = this . success; result. code = this . code; result. message = this . message; result. data = this . data; return result; } } public static < T > Result < T > ok ( ) { return new Builder < T > ( ) . success ( true ) . code ( RespBeanEnum . SUCCESS . getCode ( ) ) . message ( RespBeanEnum . SUCCESS . getMessage ( ) ) . build ( ) ; } public static < T > Result < T > ok ( T data) { return new Builder < T > ( ) . success ( true ) . code ( RespBeanEnum . SUCCESS . getCode ( ) ) . message ( RespBeanEnum . SUCCESS . getMessage ( ) ) . data ( data) . build ( ) ; } public static < T > Result < T > ok ( T data, String message) { return new Builder < T > ( ) . success ( true ) . code ( RespBeanEnum . SUCCESS . getCode ( ) ) . message ( message) . data ( data) . build ( ) ; } public static < T > Result < T > fail ( ) { return new Builder < T > ( ) . success ( false ) . code ( RespBeanEnum . ERROR . getCode ( ) ) . message ( RespBeanEnum . ERROR . getMessage ( ) ) . build ( ) ; } public static < T > Result < T > fail ( String message) { return new Builder < T > ( ) . success ( false ) . code ( RespBeanEnum . ERROR . getCode ( ) ) . message ( message) . build ( ) ; } public static < T > Result < T > fail ( int code, String message) { return new Builder < T > ( ) . success ( false ) . code ( code) . message ( message) . build ( ) ; } public static < T > Result < T > fail ( RespBeanEnum respBeanEnum) { return new Builder < T > ( ) . success ( false ) . code ( respBeanEnum. getCode ( ) ) . message ( respBeanEnum. getMessage ( ) ) . build ( ) ; } }
6.RespBeanEnum.java
## 引入宏定义
$! { define. vm} ## 设置保存名称与保存位置
$! callback. setFileName ( "RespBeanEnum.java" )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/response" ) ) ## 包声明
package $! { tableInfo. savePackageName} . response; ## 包导入
import lombok. AllArgsConstructor ;
import lombok. Getter ;
import lombok. ToString ; import java. util. Map ;
import java. util. function. Function ;
import java. util. stream. Collectors ;
import java. util. stream. Stream ; @Getter
@AllArgsConstructor
@ToString
public enum RespBeanEnum { SUCCESS ( 200 , "成功" ) , ERROR ( 500 , "失败" ) , ; private final Integer code; private final String message; public static final Map < Integer , RespBeanEnum > channelEnumMap = Stream . of ( RespBeanEnum . values ( ) ) . collect ( Collectors . toMap ( RespBeanEnum :: getCode , Function . identity ( ) ) ) ; public static RespBeanEnum getByCode ( int code) { return channelEnumMap. get ( code) ; } }
2.生成效果
3.导出到本地