近来项目中用到了MybatisPlus,顺带研究了一下代码生成器
所以就有了这篇博客,用来记录一下
1.引入依赖
<!--代码生成--> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator --><dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.1</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <!--beetl 模板引擎--> <!-- https://mvnrepository.com/artifact/com.ibeetl/beetl --><dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl</artifactId> <version>3.2.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> <scope>provided</scope> </dependency>复制代码
2.创建父类BasePO
import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableLogic;import lombok.Getter;import lombok.Setter;import java.io.Serializable;/** * 可以放一些所有PO通用的属性或方法 * * @author dujc * @date 2020/3/7 */@Setter@Getterpublic class BasePO implements Serializable {@TableId(value = "id", type = IdType.AUTO)private Long id;/** * 逻辑删除字段,0表示未删除,1表示已删除 */@TableLogic@TableField(select = false)private Long deleted = 0L; }复制代码
3.创建CodeGenerator,书写主要逻辑
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.config.*;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;import java.util.Scanner;/** * 代码生成器 * 运行 main 方法即可 * * @author abm */public class CodeGenerator {/** * <p> * 读取控制台内容 * </p> */private static String scanner(String tip) { Scanner scanner = new Scanner(System.in); System.out.println("请输入" + tip + ":");if (scanner.hasNext()) { String ipt = scanner.next();if (ipt != null && !ipt.isEmpty()) {return ipt; } }throw new MybatisPlusException("请输入正确的" + tip + "!"); }public static void main(String[] args) {// Beetl 模板引擎BeetlTemplateEngine engine = new BeetlTemplateEngine();// 代码生成器AutoGenerator mpg = new AutoGenerator();// 全局配置GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/generate/"); gc.setAuthor("miao"); gc.setOpen(false); gc.setSwagger2(false); mpg.setGlobalConfig(gc);// 数据源配置DataSourceConfig dsc = new DataSourceConfig(); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUrl("jdbc:mysql://" + scanner("数据库 ip:port ") + "/" + scanner("数据库名") + "?useUnicode=true&useSSL=false&characterEncoding=utf8"); System.out.println("数据库 url: " + dsc.getUrl()); dsc.setUsername(scanner("数据库账户名")); dsc.setPassword(scanner("数据库密码")); mpg.setDataSource(dsc);// 包配置PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("模块名")); pc.setParent("cn.miao"); mpg.setPackageInfo(pc);// 策略配置StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setSuperEntityClass("cn.miao.code.common.po.BasePO");// 写于父类中的公共字段strategy.setSuperEntityColumns("id", "deleted"); strategy.setInclude(scanner("要生成的表,多个英文逗号分割").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); strategy.setVersionFieldName("version"); mpg.setStrategy(strategy); mpg.setTemplateEngine(engine); mpg.execute(); } }复制代码