目录
需要引入的依赖 application.yml.vm application-dev.yml.vm result.java.vm (统一返回集) resultCodeEnum.java.vm (统一返回集需要的枚举类) globalCorsConfig.java.vm (全局跨域处理) entity.java.vm (实体类) mapper.java.vm (Mapper接口) mapper.xml.vm (MapperXML文件) service.java.vm (service接口) serviceImpl.java.vm (Service实现类) controller.java.vm (Controller) 生成的文件结构如下:
需要引入的依赖
需要引入的依赖
< parent>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-parent</ artifactId>
< version> 2.7.12</ version>
< relativePath/>
</ parent>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> com.github.pagehelper</ groupId>
< artifactId> pagehelper-spring-boot-starter</ artifactId>
< version> 1.4.7</ version>
</ dependency>
< dependency>
< groupId> org.mybatis.spring.boot</ groupId>
< artifactId> mybatis-spring-boot-starter</ artifactId>
< version> 2.3.1</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.33</ version>
</ dependency>
application.yml.vm
$!callback.setFileName($tool.append( "application.yml"))
$!callback.setSavePath($tool.append($modulePath , "/src/main/resources"))
spring :
profiles :
active : dev
application-dev.yml.vm
$!callback.setFileName($tool.append( "application- dev.yml"))
$!callback.setSavePath($tool.append($modulePath , "/src/main/resources"))
server :
port : 8080
pagehelper :
helper-dialect : mysql
reasonable : true
support-methods-arguments : true
params : count=countSql
spring :
datasource :
driver-class-name : com.mysql.cj.jdbc.Driver
url : jdbc: mysql: //localhost: 3306/dbtest1
username : root
password : 123456
mybatis :
configuration :
log-impl : org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case : true
type-aliases-package : $! { tableInfo.savePackageName} .entity.po
mapper-locations : classpath: /mapper/*.xml
result.java.vm (统一返回集)
##设置回调
$! callback. setFileName ( $tool. append ( "Result.java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/common" ) )
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . #{ end} common;
public class Result < T > {
private String status;
private Integer code;
private String message;
private T data;
private Result ( ) {
}
public static < T > Result build ( String status, Integer code, T data, String message) {
Result result = new Result < > ( ) ;
result. setStatus ( status) ;
result. setCode ( code) ;
result. setData ( data) ;
result. setMessage ( message) ;
return result;
}
public static < T > Result ok ( T data) {
return build ( "success" , ResultCodeEnum . SUCCESS . getCode ( ) , data, ResultCodeEnum . SUCCESS . getMessage ( ) ) ;
}
public static Result ok ( ) {
return build ( "success" , ResultCodeEnum . SUCCESS . getCode ( ) , null , ResultCodeEnum . SUCCESS . getMessage ( ) ) ;
}
public static Result fail ( ResultCodeEnum resultCodeEnum) {
return build ( "error" , resultCodeEnum. getCode ( ) , null , resultCodeEnum. getMessage ( ) ) ;
}
public static Result fail ( String message) {
return build ( "error" , ResultCodeEnum . FAIL . getCode ( ) , null , message) ;
}
public String getStatus ( ) {
return status;
}
public void setStatus ( String status) {
this . status = status;
}
public Integer getCode ( ) {
return code;
}
public void setCode ( Integer code) {
this . code = code;
}
public String getMessage ( ) {
return message;
}
public void setMessage ( String message) {
this . message = message;
}
public T getData ( ) {
return data;
}
public void setData ( T data) {
this . data = data;
}
}
resultCodeEnum.java.vm (统一返回集需要的枚举类)
##设置回调
$! callback. setFileName ( $tool. append ( "ResultCodeEnum.java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/common" ) )
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . #{ end} common;
public enum ResultCodeEnum {
SUCCESS ( 200 , "成功" ) ,
FAIL ( 10000 , "失败" ) ,
NO_LOGIN ( 401 , "未登录" ) ,
NO_PERM ( 403 , "权限不足" ) ,
USERNAME_EXIST ( 10100 , "用户名重复" ) ;
private final Integer code;
private final String message;
ResultCodeEnum ( Integer code, String message) {
this . code = code;
this . message = message;
}
public Integer getCode ( ) {
return code;
}
public String getMessage ( ) {
return message;
}
}
globalCorsConfig.java.vm (全局跨域处理)
##设置回调
$! callback. setFileName ( $tool. append ( "GlobalCorsConfig.java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/config" ) )
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . #{ end} config;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. web. cors. CorsConfiguration ;
import org. springframework. web. cors. UrlBasedCorsConfigurationSource ;
import org. springframework. web. filter. CorsFilter ;
@Configuration
public class GlobalCorsConfig {
@Bean
public CorsFilter corsFilter ( ) {
CorsConfiguration config = new CorsConfiguration ( ) ;
config. addAllowedOriginPattern ( "*" ) ;
config. setAllowCredentials ( true ) ;
config. addAllowedMethod ( "*" ) ;
config. addAllowedHeader ( "*" ) ;
config. addExposedHeader ( "*" ) ;
UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource ( ) ;
corsConfigurationSource. registerCorsConfiguration ( "/**" , config) ;
return new CorsFilter ( corsConfigurationSource) ;
}
}
entity.java.vm (实体类)
##引入宏定义
$! { define. vm}
##使用宏定义设置回调(保存位置与文件后缀)
#save ( "/entity/po" , ".java" )
##使用宏定义设置包后缀
#setPackageSuffix ( "entity.po" )
##使用全局变量实现默认包导入
$! { autoImport. vm}
import java. io. Serializable ;
##使用宏定义实现类注释信息
#tableComment ( "实体类" )
public class $! { tableInfo. name} implements Serializable {
private static final long serialVersionUID = $! tool. serial ( ) ;
#foreach ( $column in $tableInfo. fullColumn)
#if ( ${ column. comment} ) #end
private $! { tool. getClsNameByFullName ( $column. type) } $! { column. name} ;
#end
#foreach ( $column in $tableInfo. fullColumn)
##使用宏定义实现get, set方法
#getSetMethod ( $column)
#end
}
mapper.java.vm (Mapper接口)
##定义初始变量
#set ( $tableName = $tool. append ( $tableInfo. name, "Mapper" ) )
##设置回调
$! callback. setFileName ( $tool. append ( $tableName, ".java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/mapper" ) )
##拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) )
#set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#end
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . #{ end} mapper;
import $! { tableInfo. savePackageName} . entity. po. $! { tableInfo. name} ;
import org. apache. ibatis. annotations. Param ;
import java. util. List ;
public interface $! { tableName} {
$! { tableInfo. name} queryById ( $! pk. shortType $! pk. name) ;
long count ( $! { tableInfo. name} $! tool. firstLowerCase ( $! { tableInfo. name} ) ) ;
int insert ( $! { tableInfo. name} $! tool. firstLowerCase ( $! { tableInfo. name} ) ) ;
int insertBatch ( @Param ( "entities" ) List < $! { tableInfo. name} > entities) ;
int insertOrUpdateBatch ( @Param ( "entities" ) List < $! { tableInfo. name} > entities) ;
int update ( $! { tableInfo. name} $! tool. firstLowerCase ( $! { tableInfo. name} ) ) ;
int deleteById ( $! pk. shortType $! pk. name) ;
}
mapper.xml.vm (MapperXML文件)
##引入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}" id = " $!{tableInfo.name}Map" >
#foreach($column in $tableInfo.fullColumn)
< result property = " $!column.name" column = " $!column.obj.name" jdbcType = " $!column.ext.jdbcType" />
#end
</ resultMap>
< select id = " queryById" resultMap = " $!{tableInfo.name}Map" >
select
#allSqlColumn()
from $!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</ select>
< select id = " queryAllByLimit" resultMap = " $!{tableInfo.name}Map" >
select
#allSqlColumn()
from $!tableInfo.obj.name
< where>
#foreach($column in $tableInfo.fullColumn)
< if test = " $!column.name != null#if($column.type.equals(" java.lang.String")) and $!column.name ! = ' ' #end" >
and $!column.obj.name = #{$!column.name}
</ if>
#end
</ where>
limit #{pageable.offset}, #{pageable.pageSize}
</ select>
< select id = " count" resultType = " java.lang.Long" >
select count(1)
from $!tableInfo.obj.name
< where>
#foreach($column in $tableInfo.fullColumn)
< if test = " $!column.name != null#if($column.type.equals(" java.lang.String")) and $!column.name ! = ' ' #end" >
and $!column.obj.name = #{$!column.name}
</ if>
#end
</ where>
</ select>
< insert id = " insert" keyProperty = " $!pk.name" useGeneratedKeys = " true" >
insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values (#foreach($column in $tableInfo.otherColumn)#{$!{column.name}}#if($velocityHasNext), #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($velocityHasNext), #end#end)
values
< foreach collection = " entities" item = " entity" separator = " ," >
(#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
</ foreach>
</ insert>
< insert id = " insertOrUpdateBatch" keyProperty = " $!pk.name" useGeneratedKeys = " true" >
insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values
< foreach collection = " entities" item = " entity" separator = " ," >
(#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
</ foreach>
on duplicate key update
#foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name)#if($velocityHasNext),
#end#end
</ insert>
< update id = " update" >
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}
</ update>
< delete id = " deleteById" >
delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}
</ delete>
</ mapper>
service.java.vm (service接口)
##定义初始变量
#set ( $tableName = $tool. append ( $tableInfo. name, "Service" ) )
##设置回调
$! callback. setFileName ( $tool. append ( $tableName, ".java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/service" ) )
##拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) )
#set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#end
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . #{ end} service;
import $! { tableInfo. savePackageName} . entity. po. $! { tableInfo. name} ;
public interface $! { tableName} {
$! { tableInfo. name} queryById ( $! pk. shortType $! pk. name) ;
$! { tableInfo. name} insert ( $! { tableInfo. name} $! tool. firstLowerCase ( $! { tableInfo. name} ) ) ;
$! { tableInfo. name} update ( $! { tableInfo. name} $! tool. firstLowerCase ( $! { tableInfo. name} ) ) ;
boolean deleteById ( $! pk. shortType $! pk. name) ;
}
serviceImpl.java.vm (Service实现类)
##定义初始变量
#set ( $tableName = $tool. append ( $tableInfo. name, "ServiceImpl" ) )
##设置回调
$! callback. setFileName ( $tool. append ( $tableName, ".java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/service/impl" ) )
##拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) )
#set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#end
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . #{ end} service. impl;
import $! { tableInfo. savePackageName} . entity. po. $! { tableInfo. name} ;
import $! { tableInfo. savePackageName} . mapper. $! { tableInfo. name} Mapper ;
import $! { tableInfo. savePackageName} . service. $! { tableInfo. name} Service ;
import org. springframework. stereotype. Service ;
import javax. annotation. Resource ;
@Service ( "$!tool.firstLowerCase($!{tableInfo.name})Service" )
public class $! { tableName} implements $! { tableInfo. name} Service {
@Resource
private $! { tableInfo. name} Mapper $! tool. firstLowerCase ( $! { tableInfo. name} ) Mapper ;
@Override
public $! { tableInfo. name} queryById ( $! pk. shortType $! pk. name) {
return this . $! { tool. firstLowerCase ( $! { tableInfo. name} ) } Mapper . queryById ( $! pk. name) ;
}
@Override
public $! { tableInfo. name} insert ( $! { tableInfo. name} $! tool. firstLowerCase ( $! { tableInfo. name} ) ) {
this . $! { tool. firstLowerCase ( $! { tableInfo. name} ) } Mapper . insert ( $! tool. firstLowerCase ( $! { tableInfo. name} ) ) ;
return $! tool. firstLowerCase ( $! { tableInfo. name} ) ;
}
@Override
public $! { tableInfo. name} update ( $! { tableInfo. name} $! tool. firstLowerCase ( $! { tableInfo. name} ) ) {
this . $! { tool. firstLowerCase ( $! { tableInfo. name} ) } Mapper . update ( $! tool. firstLowerCase ( $! { tableInfo. name} ) ) ;
return this . queryById ( $! { tool. firstLowerCase ( $! { tableInfo. name} ) } . get$! tool. firstUpperCase ( $pk. name) ( ) ) ;
}
@Override
public boolean deleteById ( $! pk. shortType $! pk. name) {
return this . $! { tool. firstLowerCase ( $! { tableInfo. name} ) } Mapper . deleteById ( $! pk. name) > 0 ;
}
}
controller.java.vm (Controller)
##定义初始变量
#set ( $tableName = $tool. append ( $tableInfo. name, "Controller" ) )
##设置回调
$! callback. setFileName ( $tool. append ( $tableName, ".java" ) )
$! callback. setSavePath ( $tool. append ( $tableInfo. savePath, "/controller" ) )
##拿到主键
#if ( ! $tableInfo. pkColumn. isEmpty ( ) )
#set ( $pk = $tableInfo. pkColumn. get ( 0 ) )
#end
#if ( $tableInfo. savePackageName) package $! { tableInfo. savePackageName} . #{ end} controller;
import $! { tableInfo. savePackageName} . entity. po. $! { tableInfo. name} ;
import $! { tableInfo. savePackageName} . service. $! { tableInfo. name} Service ;
import org. springframework. web. bind. annotation. * ;
import javax. annotation. Resource ;
@RestController
@RequestMapping ( "$!tool.firstLowerCase($tableInfo.name)" )
public class $! { tableName} {
@Resource
private $! { tableInfo. name} Service $! tool. firstLowerCase ( $tableInfo. name) Service ;
@GetMapping ( "{id}" )
public Result queryById ( @PathVariable ( "id" ) $! pk. shortType id) {
return Result . ok ( this . $! { tool. firstLowerCase ( $tableInfo. name) } Service . queryById ( id) ) ;
}
@PostMapping
public Result add ( $! { tableInfo. name} $! { tool. firstLowerCase ( $tableInfo. name) } ) {
return Result . ok ( this . $! { tool. firstLowerCase ( $tableInfo. name) } Service . insert ( $! { tool. firstLowerCase ( $tableInfo. name) } ) ) ;
}
@PutMapping
public Result edit ( $! { tableInfo. name} $! { tool. firstLowerCase ( $tableInfo. name) } ) {
return Result . ok ( this . $! { tool. firstLowerCase ( $tableInfo. name) } Service . update ( $! { tool. firstLowerCase ( $tableInfo. name) } ) ) ;
}
@DeleteMapping
public Result deleteById ( $! pk. shortType id) {
return Result . ok ( this . $! { tool. firstLowerCase ( $tableInfo. name) } Service . deleteById ( id) ) ;
}
}
生成的文件结构如下: