pom文件
<? xml version = " 1.0 " encoding = " UTF-8 " ?>
<project
xmlns = " http://maven.apache.org/POM/4.0.0 "
xmlns : xsi = " http://www.w3.org/2001/XMLSchema-instance "
xsi : schemaLocation = " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd " >
<modelVersion> 4.0.0 </modelVersion>
<groupId> tech.foolfish </groupId>
<artifactId> mybatisplus-generator-demo </artifactId>
<version> 0.0.1-SNAPSHOT </version>
<name> mybatisplus-generator-demo </name>
<description> Demo project for Spring Boot </description>
<properties>
<java.version> 1.8 </java.version>
<project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding>
<project.reporting.outputEncoding> UTF-8 </project.reporting.outputEncoding>
<spring-boot.version> 2.3.12.RELEASE </spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-devtools </artifactId>
<scope> runtime </scope>
<optional> true </optional>
</dependency>
<dependency>
<groupId> org.projectlombok </groupId>
<artifactId> lombok </artifactId>
<optional> true </optional>
</dependency>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-test </artifactId>
<scope> test </scope>
<exclusions>
<exclusion>
<groupId> org.junit.vintage </groupId>
<artifactId> junit-vintage-engine </artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId> io.swagger </groupId>
<artifactId> swagger-models </artifactId>
<version> 1.5.21 </version>
</dependency>
<dependency>
<groupId> mysql </groupId>
<artifactId> mysql-connector-java </artifactId>
</dependency>
<dependency>
<groupId> com.baomidou </groupId>
<artifactId> mybatis-plus-annotation </artifactId>
<version> 3.4.1 </version>
</dependency>
<dependency>
<groupId> com.baomidou </groupId>
<artifactId> mybatis-plus-boot-starter </artifactId>
<version> 3.3.0 </version>
</dependency>
<dependency>
<groupId> com.baomidou </groupId>
<artifactId> mybatis-plus-generator </artifactId>
<version> 3.5.1 </version>
</dependency>
<dependency>
<groupId> org.apache.velocity </groupId>
<artifactId> velocity-engine-core </artifactId>
<version> 2.3 </version>
</dependency>
<dependency>
<groupId> org.freemarker </groupId>
<artifactId> freemarker </artifactId>
<version> 2.3.23 </version>
</dependency>
<dependency>
<groupId> javax.validation </groupId>
<artifactId> validation-api </artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-dependencies </artifactId>
<version> ${spring-boot.version} </version>
<type> pom </type>
<scope> import </scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId> org.apache.maven.plugins </groupId>
<artifactId> maven-compiler-plugin </artifactId>
<version> 3.8.1 </version>
<configuration>
<source> 1.8 </source>
<target> 1.8 </target>
<encoding> UTF-8 </encoding>
</configuration>
</plugin>
<plugin>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-maven-plugin </artifactId>
<version> ${spring-boot.version} </version>
<configuration>
<mainClass> tech.foolfish.demo.MybatisplusGeneratorDemoApplication </mainClass>
<skip> true </skip>
</configuration>
<executions>
<execution>
<id> repackage </id>
<goals>
<goal> repackage </goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
同时生成3层代码:controller、service、repository
配置生成器
package tech . foolfish . demo ;
import java . util . Collections ;
import com . baomidou . mybatisplus . annotation . FieldFill ;
import com . baomidou . mybatisplus . annotation . IdType ;
import com . baomidou . mybatisplus . core . mapper . BaseMapper ;
import com . baomidou . mybatisplus . generator . FastAutoGenerator ;
import com . baomidou . mybatisplus . generator . config . OutputFile ;
import com . baomidou . mybatisplus . generator . config . TemplateType ;
import com . baomidou . mybatisplus . generator . fill . Column ;
import com . baomidou . mybatisplus . generator . fill . Property ;
public class MybatisplusGenerator {
public static void main ( String [] args ) {
FastAutoGenerator . create ( " jdbc:mysql://******** " , " ******** " , " ******** " )
. globalConfig ( builder -> {
builder . author ( " xuej " )
. enableSwagger ()
. fileOverride ()
. outputDir ( " src/main/java " ) ;
}). packageConfig ( builder -> {
builder . parent ( " tech.foolfish.cpc.ccpbs " )
// .moduleName("charge")
. entity ( " repository.charge.model " )
. service ( " service.charge " )
. serviceImpl ( " service.charge.impl " )
. mapper ( " repository.charge " )
. xml ( " mapper.xml " )
. controller ( " controller.charge " )
. pathInfo ( Collections . singletonMap ( OutputFile . mapperXml , " src/main/resources/mapper/charge " )) ;
}). strategyConfig ( builder -> {
builder . addInclude (
" tb_operate_record "
)
. addTablePrefix ( " tb_ " )
. entityBuilder ()
. enableLombok ()
. enableTableFieldAnnotation ()
. logicDeleteColumnName ( " delete_ind " )
. logicDeletePropertyName ( " deleteInd " )
. addTableFills ( new Column ( " create_time " , FieldFill . INSERT ))
. addTableFills ( new Property ( " updateTime " , FieldFill . INSERT_UPDATE ))
. idType ( IdType . AUTO )
. formatFileName ( " %sDO " ) ;
}). strategyConfig ( builder -> {
builder . mapperBuilder ()
. superClass ( BaseMapper . class )
. enableMapperAnnotation ()
. enableBaseResultMap ()
. enableBaseColumnList ()
. formatMapperFileName ( " %sRepository " )
. build () ;
}). strategyConfig ( builder -> {
builder . serviceBuilder ()
. formatServiceFileName ( " %sBaseService " )
. formatServiceImplFileName ( " %sBaseServiceImpl " )
. build () ;
}). strategyConfig ( builder -> {
builder . controllerBuilder ()
. enableRestStyle ()
. build () ;
}). templateConfig ( builder -> {
// builder.disable(TemplateType.CONTROLLER, TemplateType.SERVICE, TemplateType.SERVICEIMPL)
// .build();
})
. execute () ;
}
}
启动生成器
16:03:00.926 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
16:03:00.955 [main] WARN org.apache.velocity.deprecation - configuration key ' file.resource.loader.unicode ' has been deprecated in favor of ' resource.loader.file.unicode '
16:03:00.957 [main] WARN org.apache.velocity.deprecation - configuration key ' file.resource.loader.class ' has been deprecated in favor of ' resource.loader.file.class '
16:03:01.568 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 执行SQL:show table status WHERE 1=1 AND NAME IN ( 'tb_operate_record' )
16:03:01.614 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 返回记录数:1,耗时 ( ms ) :44
16:03:01.816 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 执行SQL:show full fields from ` tb_operate_record `
16:03:01.859 [main] WARN com.baomidou.mybatisplus.generator.IDatabaseQuery $DefaultDatabaseQuery - 当前表 [ tb_operate_record ] 的主键为自增主键,会导致全局主键的ID类型设置失效 !
16:03:01.875 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 返回记录数:48,耗时 ( ms ) :58
16:03:01.898 [main] DEBUG org.apache.velocity - Initializing Velocity, Calling init () ...
16:03:01.898 [main] DEBUG org.apache.velocity - Starting Apache Velocity v2.3
16:03:01.903 [main] DEBUG org.apache.velocity - Default Properties resource: org/apache/velocity/runtime/defaults/velocity.properties
16:03:01.909 [main] DEBUG org.apache.velocity - ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:03:01.911 [main] DEBUG org.apache.velocity - initialized ( class org.apache.velocity.runtime.resource.ResourceCacheImpl ) with class java.util.Collections $SynchronizedMap cache map.
16:03:01.913 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Stop
16:03:01.914 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Define
16:03:01.915 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Break
16:03:01.915 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Evaluate
16:03:01.916 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Macro
16:03:01.916 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Parse
16:03:01.917 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Include
16:03:01.917 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
16:03:01.950 [main] DEBUG org.apache.velocity.parser - Created ' 20 ' parsers.
16:03:01.966 [main] DEBUG org.apache.velocity.macro - " velocimacro.library.path " is not set. Trying default library: velocimacros.vtl
16:03:01.967 [main] DEBUG org.apache.velocity.loader.file - Could not load resource ' velocimacros.vtl ' from ResourceLoader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:03:01.967 [main] DEBUG org.apache.velocity.macro - Default library velocimacros.vtl not found. Trying old default library: VM_global_library.vm
16:03:01.967 [main] DEBUG org.apache.velocity.loader.file - Could not load resource ' VM_global_library.vm ' from ResourceLoader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:03:01.967 [main] DEBUG org.apache.velocity.macro - Old default library VM_global_library.vm not found.
16:03:01.967 [main] DEBUG org.apache.velocity.macro - allowInline = true: VMs can be defined inline in templates
16:03:01.967 [main] DEBUG org.apache.velocity.macro - allowInlineToOverride = false: VMs defined inline may NOT replace previous VM definitions
16:03:01.967 [main] DEBUG org.apache.velocity.macro - allowInlineLocal = false: VMs defined inline will be global in scope if allowed.
16:03:01.967 [main] DEBUG org.apache.velocity.macro - autoload off: VM system will not automatically reload global library macros
16:03:02.003 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/entity.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:03:02.030 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/mapper.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:03:02.035 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/mapper.xml.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:03:02.042 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/service.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:03:02.045 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/serviceImpl.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:03:02.049 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/controller.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:03:02.050 [main] DEBUG com.baomidou.mybatisplus.generator.util.RuntimeUtils - 文件输出目录:src/main/java
16:03:02.050 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================
生成结果
只生成1层代码:repository
配置生成器
package tech . foolfish . demo ;
import java . util . Collections ;
import com . baomidou . mybatisplus . annotation . FieldFill ;
import com . baomidou . mybatisplus . annotation . IdType ;
import com . baomidou . mybatisplus . core . mapper . BaseMapper ;
import com . baomidou . mybatisplus . generator . FastAutoGenerator ;
import com . baomidou . mybatisplus . generator . config . OutputFile ;
import com . baomidou . mybatisplus . generator . config . TemplateType ;
import com . baomidou . mybatisplus . generator . fill . Column ;
import com . baomidou . mybatisplus . generator . fill . Property ;
public class MybatisplusGenerator {
public static void main ( String [] args ) {
FastAutoGenerator . create ( " jdbc:mysql://******** " , " ******** " , " ******** " )
. globalConfig ( builder -> {
builder . author ( " xuej " )
. enableSwagger ()
. fileOverride ()
. outputDir ( " src/main/java " ) ;
}). packageConfig ( builder -> {
builder . parent ( " tech.foolfish.cpc.ccpbs " )
// .moduleName("charge")
. entity ( " repository.charge.model " )
. service ( " service.charge " )
. serviceImpl ( " service.charge.impl " )
. mapper ( " repository.charge " )
. xml ( " mapper.xml " )
. controller ( " controller.charge " )
. pathInfo ( Collections . singletonMap ( OutputFile . mapperXml , " src/main/resources/mapper/charge " )) ;
}). strategyConfig ( builder -> {
builder . addInclude (
" tb_operate_record "
)
. addTablePrefix ( " tb_ " )
. entityBuilder ()
. enableLombok ()
. enableTableFieldAnnotation ()
. logicDeleteColumnName ( " delete_ind " )
. logicDeletePropertyName ( " deleteInd " )
. addTableFills ( new Column ( " create_time " , FieldFill . INSERT ))
. addTableFills ( new Property ( " updateTime " , FieldFill . INSERT_UPDATE ))
. idType ( IdType . AUTO )
. formatFileName ( " %sDO " ) ;
}). strategyConfig ( builder -> {
builder . mapperBuilder ()
. superClass ( BaseMapper . class )
. enableMapperAnnotation ()
. enableBaseResultMap ()
. enableBaseColumnList ()
. formatMapperFileName ( " %sRepository " )
. build () ;
}). strategyConfig ( builder -> {
builder . serviceBuilder ()
. formatServiceFileName ( " %sBaseService " )
. formatServiceImplFileName ( " %sBaseServiceImpl " )
. build () ;
}). strategyConfig ( builder -> {
builder . controllerBuilder ()
. enableRestStyle ()
. build () ;
}). templateConfig ( builder -> {
builder . disable ( TemplateType . CONTROLLER , TemplateType . SERVICE , TemplateType . SERVICEIMPL )
. build () ;
})
. execute () ;
}
}
启动生成器
16:21:17.156 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
16:21:17.180 [main] WARN org.apache.velocity.deprecation - configuration key ' file.resource.loader.unicode ' has been deprecated in favor of ' resource.loader.file.unicode '
16:21:17.181 [main] WARN org.apache.velocity.deprecation - configuration key ' file.resource.loader.class ' has been deprecated in favor of ' resource.loader.file.class '
16:21:18.008 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 执行SQL:show table status WHERE 1=1 AND NAME IN ( 'tb_operate_record' )
16:21:18.057 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 返回记录数:1,耗时 ( ms ) :49
16:21:18.236 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 执行SQL:show full fields from ` tb_operate_record `
16:21:18.260 [main] WARN com.baomidou.mybatisplus.generator.IDatabaseQuery $DefaultDatabaseQuery - 当前表 [ tb_operate_record ] 的主键为自增主键,会导致全局主键的ID类型设置失效 !
16:21:18.279 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 返回记录数:48,耗时 ( ms ) :42
16:21:18.302 [main] DEBUG org.apache.velocity - Initializing Velocity, Calling init () ...
16:21:18.302 [main] DEBUG org.apache.velocity - Starting Apache Velocity v2.3
16:21:18.306 [main] DEBUG org.apache.velocity - Default Properties resource: org/apache/velocity/runtime/defaults/velocity.properties
16:21:18.314 [main] DEBUG org.apache.velocity - ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:21:18.315 [main] DEBUG org.apache.velocity - initialized ( class org.apache.velocity.runtime.resource.ResourceCacheImpl ) with class java.util.Collections $SynchronizedMap cache map.
16:21:18.317 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Stop
16:21:18.318 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Define
16:21:18.318 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Break
16:21:18.319 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Evaluate
16:21:18.320 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Macro
16:21:18.321 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Parse
16:21:18.322 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Include
16:21:18.323 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
16:21:18.355 [main] DEBUG org.apache.velocity.parser - Created ' 20 ' parsers.
16:21:18.371 [main] DEBUG org.apache.velocity.macro - " velocimacro.library.path " is not set. Trying default library: velocimacros.vtl
16:21:18.371 [main] DEBUG org.apache.velocity.loader.file - Could not load resource ' velocimacros.vtl ' from ResourceLoader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:21:18.371 [main] DEBUG org.apache.velocity.macro - Default library velocimacros.vtl not found. Trying old default library: VM_global_library.vm
16:21:18.371 [main] DEBUG org.apache.velocity.loader.file - Could not load resource ' VM_global_library.vm ' from ResourceLoader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:21:18.371 [main] DEBUG org.apache.velocity.macro - Old default library VM_global_library.vm not found.
16:21:18.371 [main] DEBUG org.apache.velocity.macro - allowInline = true: VMs can be defined inline in templates
16:21:18.372 [main] DEBUG org.apache.velocity.macro - allowInlineToOverride = false: VMs defined inline may NOT replace previous VM definitions
16:21:18.372 [main] DEBUG org.apache.velocity.macro - allowInlineLocal = false: VMs defined inline will be global in scope if allowed.
16:21:18.372 [main] DEBUG org.apache.velocity.macro - autoload off: VM system will not automatically reload global library macros
16:21:18.403 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/entity.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:21:18.430 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/mapper.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:21:18.435 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/mapper.xml.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
16:21:18.439 [main] DEBUG com.baomidou.mybatisplus.generator.util.RuntimeUtils - 文件输出目录:src/main/java
16:21:18.439 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================
执行结果
相关链接
MyBatis-Plus 代码生成器(新)
代码生成器配置新