根据数据表自动生成实体类、mapper、Service、ServiceImpl、Controller
一个整体的框架
注意
1 )不要重复生成xxxMapper.xml,如果你有一个字段写错了,请把生成的Mapper.xml删除,别的会自动覆盖,这个Mapper.xml是个坑
2)自动生成的代码要自己测试一下,自己测试一下
1.导入MybatisPlus的依赖
<!--自动生成-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<!--模板-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
其中的模板会有三个选用 velocity、Freemarker、Beetl,默认是velocity
2.编辑执行类
package com.mybatisplusstudty;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
/**
* @Program: mybatisplus
* @Description
* @Author: 涛涛 * ^ *
* @Create: 2021-01-08 17:29
**/
public class Mian {
public static void main(String[] args) {
//创建Generator对象
AutoGenerator autoGenerator = new AutoGenerator();
//数据源
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/mp?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("123456");
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
autoGenerator.setDataSource(dataSourceConfig);
//全局配置
GlobalConfig globalConfig = new GlobalConfig();
//自动生成到哪一个包里边
// globalConfig.setOutputDir(System.getProperty("user.dir" + "src/main/java"));
globalConfig.setOutputDir("C:\\Users\\lenovo\\Desktop\\day30\\mybatisplus\\"+"src\\main\\java");
System.out.println("OutputDir:"+globalConfig.getOutputDir());
//创建成功之后不打开文件夹
globalConfig.setOpen(false);
//设置作者(不设置的就是当前计算机的名称)
globalConfig.setAuthor("taotao");
//第二次生成会把第一次生成的覆盖掉
globalConfig.setFileOverride(true);
//去除生成的Service前缀I
globalConfig.setServiceName("%sService");
autoGenerator.setGlobalConfig(globalConfig);
//包配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.mybatisplusstudty");
packageConfig.setModuleName("userMoney");
System.out.println("ModuleName:"+packageConfig.getModuleName());
packageConfig.setController("controller");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setMapper("mapper");
packageConfig.setEntity("entity");
autoGenerator.setPackageInfo(packageConfig);
//配置策略
StrategyConfig strategyConfig = new StrategyConfig();
//使用Lombok
strategyConfig.setEntityLombokModel(true);
//转驼峰命名
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
autoGenerator.setStrategy(strategyConfig);
//执行
autoGenerator.execute();
}
}
这里会有一个小问题 ,在设置生成的包的路径的时候,会找不到之前设置好的user.dir" + "src/main/java
的路径,所以我跟换成了这个项目所在的绝对路径
3.生成效果图
这个Service默认生成的话是有I为前缀的,如果不需要的话也可以在策略中添加去除的
globalConfig.setServiceName("%sService");
// 4、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("t_blog");//指定要映射的数据库表,可以写多个
strategy.setNaming(NamingStrategy.underline_to_camel);//设置命名规则下划线转驼峰
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//列名规则
strategy.setEntityLombokModel(true);//是否生成lombok注解
// strategy.setLogicDeleteFieldName("deleted");//逻辑删除字段配置
//自动填充的配置
TableFill create_time = new TableFill("create_time", FieldFill.INSERT);//设置时的生成策略
TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);//设置更新时间的生成策略
ArrayList<TableFill> list = new ArrayList<>();
list.add(create_time);
list.add(update_time);
strategy.setTableFillList(list);
//乐观锁
// strategy.setVersionFieldName("version");
strategy.setRestControllerStyle(true);//开启驼峰命名
// strategy.setControllerMappingHyphenStyle(true);//开启链接地址的下划线命名 localhost:8080/hello_id_2
4.附注
执行的控制台输出
13:49:22.116 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================�������ļ�...==========================
13:49:22.506 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ����Ŀ¼�� [C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\entity]
13:49:22.507 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ����Ŀ¼�� [C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\controller]
13:49:22.509 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ����Ŀ¼�� [C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\mapper\xml]
13:49:22.510 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ����Ŀ¼�� [C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\service]
13:49:22.510 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ����Ŀ¼�� [C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\service\impl]
13:49:22.523 [main] DEBUG org.apache.velocity - CommonsLogLogChute name is 'org.apache.velocity'
13:49:22.523 [main] DEBUG org.apache.velocity - Initializing Velocity, Calling init()...
13:49:22.523 [main] DEBUG org.apache.velocity - Starting Apache Velocity v1.7 (compiled: 2010-11-19 12:14:37)
13:49:22.523 [main] DEBUG org.apache.velocity - Default Properties File: org\apache\velocity\runtime\defaults\velocity.properties
13:49:22.524 [main] DEBUG org.apache.velocity - Trying to use logger class org.apache.velocity.runtime.log.AvalonLogChute
13:49:22.524 [main] DEBUG org.apache.velocity - Target log system for org.apache.velocity.runtime.log.AvalonLogChute is not available (java.lang.NoClassDefFoundError: org/apache/log/format/Formatter). Falling back to next log system...
13:49:22.524 [main] DEBUG org.apache.velocity - Trying to use logger class org.apache.velocity.runtime.log.Log4JLogChute
13:49:22.524 [main] DEBUG org.apache.velocity - Target log system for org.apache.velocity.runtime.log.Log4JLogChute is not available (java.lang.NoClassDefFoundError: org/apache/log4j/Priority). Falling back to next log system...
13:49:22.524 [main] DEBUG org.apache.velocity - Trying to use logger class org.apache.velocity.runtime.log.CommonsLogLogChute
13:49:22.524 [main] DEBUG org.apache.velocity - Using logger class org.apache.velocity.runtime.log.CommonsLogLogChute
13:49:22.528 [main] DEBUG org.apache.velocity - ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.536 [main] DEBUG org.apache.velocity - ResourceCache: initialized (class org.apache.velocity.runtime.resource.ResourceCacheImpl) with class java.util.Collections$SynchronizedMap cache map.
13:49:22.538 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Stop
13:49:22.539 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Define
13:49:22.539 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Break
13:49:22.540 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Evaluate
13:49:22.540 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Literal
13:49:22.541 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Macro
13:49:22.542 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Parse
13:49:22.543 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Include
13:49:22.544 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
13:49:22.566 [main] DEBUG org.apache.velocity - Created '20' parsers.
13:49:22.569 [main] DEBUG org.apache.velocity - Velocimacro : "velocimacro.library" is not set. Trying default library: VM_global_library.vm
13:49:22.570 [main] DEBUG org.apache.velocity - Could not load resource 'VM_global_library.vm' from ResourceLoader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader: ClasspathResourceLoader Error: cannot find resource VM_global_library.vm
13:49:22.570 [main] DEBUG org.apache.velocity - Velocimacro : Default library not found.
13:49:22.570 [main] DEBUG org.apache.velocity - Velocimacro : allowInline = true : VMs can be defined inline in templates
13:49:22.570 [main] DEBUG org.apache.velocity - Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT replace previous VM definitions
13:49:22.570 [main] DEBUG org.apache.velocity - Velocimacro : allowInlineLocal = false : VMs defined inline will be global in scope if allowed.
13:49:22.570 [main] DEBUG org.apache.velocity - Velocimacro : autoload off : VM system will not automatically reload global library macros
13:49:22.593 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/entity.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.600 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.600 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.600 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.600 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.600 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.600 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.601 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.601 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.601 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.601 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.601 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.601 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.601 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.601 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.602 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.602 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.603 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.603 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.603 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.603 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.606 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.607 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.607 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/entity.java.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\entity\User.java
13:49:22.608 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/mapper.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.609 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/mapper.java.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\mapper\UserMapper.java
13:49:22.611 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/mapper.xml.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.612 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/mapper.xml.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\mapper\xml\UserMapper.xml
13:49:22.613 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/service.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.614 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/service.java.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\service\IUserService.java
13:49:22.616 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/serviceImpl.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.617 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/serviceImpl.java.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\service\impl\UserServiceImpl.java
13:49:22.619 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/controller.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.620 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/controller.java.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\controller\UserController.java
13:49:22.624 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/entity.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.625 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.625 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.625 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.625 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.626 [main] DEBUG org.apache.velocity - Left side (${versionFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 86, column 26]
13:49:22.626 [main] DEBUG org.apache.velocity - Left side (${logicDeleteFieldName}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /templates/entity.java.vm[line 90, column 30]
13:49:22.626 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/entity.java.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\entity\Usermoney.java
13:49:22.627 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/mapper.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.628 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/mapper.java.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\mapper\UsermoneyMapper.java
13:49:22.630 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/mapper.xml.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.631 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/mapper.xml.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\mapper\xml\UsermoneyMapper.xml
13:49:22.633 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/service.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.634 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/service.java.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\service\IUsermoneyService.java
13:49:22.635 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/serviceImpl.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.636 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/serviceImpl.java.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\service\impl\UsermoneyServiceImpl.java
13:49:22.637 [main] DEBUG org.apache.velocity - ResourceManager : found /templates/controller.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
13:49:22.638 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - ģ��:/templates/controller.java.vm; �ļ�:C:\Users\lenovo\Desktop\day30\mybatisplus\src\main\java\com\mybatisplusstudty\userMoney\controller\UsermoneyController.java
13:49:22.638 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator -
这里的中文乱码不影响的哦,只是进行输出日志的作用
生成之后呢,记得跟换启动类中的MapperScan扫描的mapper的包的路径