时间:2022-06-03 11:09:41 | 栏目:JAVA代码 | 点击:次
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.19</version>
</dependency>
更详细的代码生成器配置请查看官方文档:https://baomidou.com/pages/061573/#superentityclass


outputDir
D 盘根目录fileOverride
falseopen
trueenableCache
false开发人员
nullkotlin
falseswagger2
falseactiveRecord
falsebaseResultMap
falsebaseColumnList
falsedateType
TIME_PACKentityName
null 例如:%sEntity 生成 UserEntitymapperName
null 例如:%sDao 生成 UserDaoxmlName
null 例如:%sDao 生成 UserDao.xmlserviceName
null 例如:%sBusiness 生成 UserBusinessserviceImplName
null 例如:%sBusinessImpl 生成 UserBusinessImplcontrollerName
null 例如:%sAction 生成 UserActionidType
nulldbQuery
dbType 类型决定选择对应数据库内置实现? 实现 IDbQuery 接口自定义数据库查询 SQL 语句 定制化返回自己需要的内容
dbType
schemaName
PostgreSQL 可指定为 publictypeConvert
dbType 类型决定选择对应数据库内置实现? 实现 ITypeConvert 接口自定义数据库 字段类型 转换为自己需要的 java 类型,内置转换类型无法满足可实现 IColumnType 接口自定义
url
driverName
username
password
package com.haoming;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
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.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
public class ChengCode {
public static void main(String[] args) {
//构建代码生成器对象
AutoGenerator mpg = new AutoGenerator();
//1、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");//生成文件的输出目录
gc.setAuthor("鼠皓明");//作者
gc.setOpen(false);//是否打开输出目录
gc.setFileOverride(false);//是否覆盖已有的文件
gc.setServiceName("%sService");//去除Service的I前缀
gc.setIdType(IdType.ID_WORKER);//主键生成策略
//ONLY_DATE 只使用 java.util.date 代替,SQL_PACK 使用 java.sql 包下的,TIME_PACK 使用 java.time 包下的 java8 新的时间类型
gc.setDateType(DateType.TIME_PACK);//数据库时间类型 到 实体类时间类型 对应策略
gc.setSwagger2(true);//开启swagger2模式
mpg.setGlobalConfig(gc);
//2、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSl=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);//数据库类型
mpg.setDataSource(dsc);
//3、包的配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("blog");//父包模块名
pc.setParent("com.cheng");//父包名,如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setEntity("pojo");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);
//4、策略配置
StrategyConfig sy = new StrategyConfig();
sy.setInclude("user");//设置要映射的表,可以设置多张
sy.setNaming(NamingStrategy.underline_to_camel);//从数据库表到文件的命名策略,下划线转驼峰命名
sy.setColumnNaming(NamingStrategy.underline_to_camel);//列的命名策略
sy.setEntityLombokModel(true);//开启lombok支持
sy.setLogicDeleteFieldName("deleted");//设置逻辑删除字段
sy.setVersionFieldName("version");//设置乐观锁
sy.setRestControllerStyle(true);//开启controller的restful命名
sy.setControllerMappingHyphenStyle(true);//开启controller中请求映射的连字符样式,如:localhost:8080/hello_id_1
//设置自动填充
TableFill create_time = new TableFill("create_time", FieldFill.INSERT);
TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(create_time);
tableFills.add(update_time);
mpg.setStrategy(sy);
//执行代码生成器
mpg.execute();
}
}
执行代码生成器,查看项目结构的变化

代码生成器执行成功,自动生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码。