前言
现在新建一个项目,不能快速开始开发?需要自己建entity,controller,mapper,service等包和代码?
现在来介绍一个mybatis plus的代码生成器快速生成代码,即刻开始开发,无需繁琐的准备过程!
一、AutoGenerator是什么?
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
二、使用步骤
1.引入依赖
代码如下(示例):
<!-- 代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.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.31</version>
</dependency>
<!--代码生成器 结束-->
2.详细配置
新建class,命名为CodeGenerator,可以放在任意位置,建议放到测试包下。package com.xx.xxx;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
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.NamingStrategy;
import java.util.ArrayList;
import java.util.List;
public class CodeGenerator {
public static void main(String[] a) {
//代码生成器
AutoGenerator autoGenerator = new AutoGenerator();
//数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
//指定数据库类型
dataSourceConfig.setDbType(DbType.MYSQL);
//MySQL版本为8.0以上使用com.mysql.cj.jdbc.Driver,5.7及以下版本使用com.mysql.jdbc.Driver
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
// TODO: 2021-6-21 填写mysql用户名密码
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("MySQL密码");
// TODO: 2021-6-21 填写jdbc连接
dataSourceConfig.setUrl("jdbc:mysql://IP地址/数据库名?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true");
autoGenerator.setDataSource(dataSourceConfig);
//全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOpen(false);
//开启文件覆盖
globalConfig.setFileOverride(false);
//设置控制层类名
globalConfig.setControllerName("%sController");
//设置service类名
globalConfig.setServiceName("%sService");
//设置serviceImpl类名
globalConfig.setServiceImplName("%sServiceImpl");
// mapper 命名方式
globalConfig.setMapperName("%sMapper");
// xml 命名方式
globalConfig.setXmlName("%sMapper");
// TODO: 2021-6-21 填写输出路径 选择项目目录下src/mian/java文件夹,idea右键复制绝对路径,粘贴到下面即可
globalConfig.setOutputDir("D:\\MySource\\project\\project_name\\src\\main\\java");
// 是否开启 ActiveRecord 模式
globalConfig.setActiveRecord(true);
// 是否在xml中添加二级缓存配置
globalConfig.setEnableCache(false);
// 是否开启 BaseResultMap
globalConfig.setBaseResultMap(false);
// XML columList
globalConfig.setBaseColumnList(false);
//设置作者名字
globalConfig.setAuthor("jiusi");
//开启swagger2注解 使用swagger2文档可将下面的false改为true
globalConfig.setSwagger2(false);
autoGenerator.setGlobalConfig(globalConfig);
// 包配置
PackageConfig packageConfig = new PackageConfig();
// TODO: 2021-6-21 填写包名
// 主要包名
packageConfig.setParent("com.zxy.xx");
// 实体包名
packageConfig.setEntity("entity");
// 数据库接口包名
packageConfig.setMapper("mapper");
// 控制器包名
packageConfig.setController("controller");
// 业务接口包名
packageConfig.setService("service");
// 业务实现类包名
packageConfig.setServiceImpl("service.impl");
autoGenerator.setPackageInfo(packageConfig);
// 策略配置
StrategyConfig strategyConfig = new StrategyConfig();
// 是否使用Lombok
strategyConfig.setEntityLombokModel(true);
// TODO: 2021-9-27 设置表前缀 如果有前缀的话取消下一行代码的注释填写前缀即可
// strategyConfig.setTablePrefix("t_");
// 乐观锁字段名称
strategyConfig.setVersionFieldName("version");
// 生成 @RestController 控制器
strategyConfig.setRestControllerStyle(true);
// 逻辑删除字段名称
strategyConfig.setLogicDeleteFieldName("deleted");
// 是否生成实体时,生成字段注解
strategyConfig.setEntityTableFieldAnnotationEnable(true);
// 设置逻辑删除键
strategyConfig.setLogicDeleteFieldName("deleted");
// TODO: 2021-6-21 填写表名 如果导入部分表的话,可以取消下一行代码的注释,并填入表名
// strategyConfig.setInclude("t_admin", "t_user");
//包,列的命名规则,使用驼峰规则
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
// 自动填充字段,在项目开发过程中,例如创建时间,修改时间,每次,都需要我们来指定,太麻烦了,设置为自动填充规则,就不需要我们赋值咯
List<TableFill> list = new ArrayList<>();
TableFill tableFill1 = new TableFill("create_time", FieldFill.INSERT);
TableFill tableFill2 = new TableFill("update_time", FieldFill.INSERT_UPDATE);
list.add(tableFill1);
list.add(tableFill2);
strategyConfig.setTableFillList(list);
autoGenerator.setStrategy(strategyConfig);
// 执行
autoGenerator.execute();
}
}