代码生成器之Mybatis-Plus(基础版)
1. 导入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
2. 编写生成类
package com.example.generator;
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;
public class Simple {
public static void main(String[] args) {
AutoGenerator autoGenerator = new AutoGenerator();
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir("C:\\Users\\HZC\\Desktop\\layui");
globalConfig.setAuthor("hzc");
globalConfig.setMapperName("%sMapper");
globalConfig.setServiceName("%sService");
globalConfig.setServiceImplName("%sServiceImpl");
autoGenerator.setGlobalConfig(globalConfig);
autoGenerator.setGlobalConfig(globalConfig);
DataSourceConfig sourceConfig = new DataSourceConfig();
sourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
sourceConfig.setUrl("jdbc:mysql://localhost:3306/jwsystem?serverTimezone=UTC");
sourceConfig.setUsername("root");
sourceConfig.setPassword("123456");
autoGenerator.setDataSource(sourceConfig);
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.example");
autoGenerator.setPackageInfo(packageConfig);
// TODO 策略配置
StrategyConfig strategy = new StrategyConfig();
// 数据库表映射到实体的命名策略,驼峰原则
strategy.setNaming(NamingStrategy.underline_to_camel);
// 字数据库表字段映射到实体的命名策略,驼峰原则
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 实体是否生成 serialVersionUID
strategy.setEntitySerialVersionUID(false);
// 是否生成实体时,生成字段注解
strategy.setEntityTableFieldAnnotationEnable(true);
// 使用lombok
strategy.setEntityLombokModel(true);
// 设置逻辑删除键
strategy.setLogicDeleteFieldName("del_flag");
// 驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
autoGenerator.setStrategy(strategy);
autoGenerator.execute();
}
}