Mybatis-Plus简单代码生成器
AI-摘要
Tianli GPT
AI初始化中...
介绍自己
生成本文简介
推荐相关文章
前往主页
前往tianli博客
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>
2、代码生成
package com.leaflei.utils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.List;
/**
* MybatisPlus代码生成器
*/
public class GeneratorUtils {
// 数据库连接地址
private final static String DB_URL = "jdbc:mysql://43.138.100.148/springboot_study?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC";
// 用户名
private final static String USER_NAME = "root";
// 密码
private final static String PASSWORD = "root";
// 作者
private final static String AUTHOR = "leaflei";
// 数据库驱动
private final static String DRIVE = "com.mysql.cj.jdbc.Driver";
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
// 指定生成路径
gc.setOutputDir(projectPath + "/src/main/java/test");
// 作者名
gc.setAuthor(AUTHOR);
// 是否打开全局配置
gc.setOpen(false);
// controller名称
gc.setControllerName("%sController");
// service名称
gc.setServiceName("%sService");
// service实现类名称
gc.setServiceImplName("%sServiceImpl");
// mapper名称
gc.setMapperName("%sMapper");
// 设置全局配置
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
// dsc.setSchemaName("public");
// 数据源连接地址
dsc.setUrl(DB_URL);
// 驱动名称
dsc.setDriverName(DRIVE);
// 数据库用户名
dsc.setUsername(USER_NAME);
// 数据库密码
dsc.setPassword(PASSWORD);
// 设置数据源
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// 最上级包名
pc.setParent("com.leaflei");
// entity包名
pc.setEntity("entity");
// mapper包名
pc.setMapper("mapper");
// controller名称
pc.setController("controller");
// service名称
pc.setService("service");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/Mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
// 设置模板
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// 驼峰
strategy.setNaming(NamingStrategy.underline_to_camel);
// 驼峰
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 要生成的表
strategy.setInclude("tbl_user");
// 表前缀
strategy.setTablePrefix("tbl_");
// 是否使用lombok
strategy.setEntityLombokModel(true);
// restful风格
strategy.setRestControllerStyle(true);
// 逻辑删除字段
strategy.setLogicDeleteFieldName("deleted");
// 乐观锁
strategy.setVersionFieldName("version");
// controller mapping风格
strategy.setControllerMappingHyphenStyle(true);
// 表前缀
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.execute();
}
}
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 leaflei
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果