Java代码生成器
MyBatisPlus提供模板
Mapper接口模板
![Mapper接口模板](https://i-blog.csdnimg.cn/blog_migrate/9c97042c5c273c57a09d069822c77e97.png#pic_center)
实体对象类模板
![实体对象类模板](https://i-blog.csdnimg.cn/blog_migrate/373ce41243cd5ff0f3949adff310129d.png#pic_center)
工程搭建和基本代码编写
创建SpringBoot工程,添加代码生成器相关依赖,其他依赖自行添加
<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.2</version>
</dependency>
编写代码生成器类
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
public class Generator {
public static void main(String[] args) {
AutoGenerator autoGenerator = new AutoGenerator();
DataSourceConfig dataSource = new DataSourceConfig();
dataSource.setDriverName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("");
autoGenerator.setDataSource(dataSource);
autoGenerator.execute();
}
}
开发者自定义配置
设置全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir")+"/mybatisplus_04_generator/src/main/java");
globalConfig.setOpen(false);
globalConfig.setAuthor("BaiYang");
globalConfig.setFileOverride(true);
globalConfig.setMapperName("%sDao");
globalConfig.setIdType(IdType.ASSIGN_ID);
autoGenerator.setGlobalConfig(globalConfig);
设置包名相关配置
PackageConfig packageInfo = new PackageConfig();
packageInfo.setParent("com.aaa");
packageInfo.setEntity("domain");
packageInfo.setMapper("dao");
autoGenerator.setPackageInfo(packageInfo);
策略设置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("tbl_user");
strategyConfig.setTablePrefix("tbl_");
strategyConfig.setRestControllerStyle(true);
strategyConfig.setVersionFieldName("version");
strategyConfig.setLogicDeleteFieldName("deleted");
strategyConfig.setEntityLombokModel(true);
autoGenerator.setStrategy(strategyConfig);
完整代码实现
<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>
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class CodeGenerator {
public static void main(String[] args) {
AutoGenerator autoGenerator = new AutoGenerator();
DataSourceConfig dataSource = new DataSourceConfig();
dataSource.setDriverName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl(
"jdbc:mysql://localhost:3306/reggie?serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("");
autoGenerator.setDataSource(dataSource);
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(
System.getProperty("user.dir")+"/src/main/java");
globalConfig.setOpen(false);
globalConfig.setAuthor("BaiYang");
globalConfig.setFileOverride(true);
globalConfig.setServiceName("%sService");
globalConfig.setIdType(IdType.ASSIGN_ID);
autoGenerator.setGlobalConfig(globalConfig);
PackageConfig packageInfo = new PackageConfig();
packageInfo.setParent("");
packageInfo.setEntity("pojo");
packageInfo.setMapper("dao");
autoGenerator.setPackageInfo(packageInfo);
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
autoGenerator.setTemplate(templateConfig);
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setRestControllerStyle(true);
strategyConfig.setEntityLombokModel(true);
autoGenerator.setStrategy(strategyConfig);
autoGenerator.execute();
}
}