mybatis plus generator 根据数据库自动生成实体类的实现示例
作者:饶一一 发布时间:2024-01-25 11:48:00
标签:mybatis,plus,generator,自动生成,实体类
1、添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
2、编写代码生成器
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入" + tip + ":");
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/check-service/src/main/java");
gc.setAuthor("raoyujie");
gc.setOpen(false);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.hrbust.health.check.service");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/check-service/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.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
3、运行主程序,输入表名
来源:https://www.cnblogs.com/rao11/p/14261846.html


猜你喜欢
- 通过jsonp简单获取接口数据,对了,注意下jsonp方法会自动添加callback<template><div clas
- 本文实例讲述了python中__call__内置函数的用法。分享给大家供大家参考。具体分析如下:对象通过提供__call__(slef, [
- 一、前言B站上的漂亮的小姐姐真的好多好多,利用 you-get * 下载了一个 B 站上跳舞的小姐姐视频,利用视频中的弹幕来制作一个漂亮小姐
- 插入代码块使用sum函数:numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]print(sum(number
- 概述基于内容的图像检索技术是采用某种算法来提取图像中的特征,并将特征存储起来,组成图像特征数据库。当需要检索图像时,采用相同的特征提取技术提
- 直接看代码: 代码如下:Class GoogleTranslator sub Class_Initialize
- 安装redis服务1 下载redis cd /usr/local/ 进入安装目录 wget http://downl
- TF-IDF(term frequency–inverse document frequency)是一种用于信息检索(information
- 本文实例讲述了js字符串操作方法。分享给大家供大家参考。具体如下:var str="This is my first Script
- 实例如下:import urllib.requestimport urllib.parseimport jsonwhile True: &n
- 问题简述在 Windows 系统上,我使用 Python 3.11 的 pip 工具安装 lxml 等库时会出现以下报错:error: Mi
- 一、前言前几天在Python最强王者群有个叫【dcpeng】的粉丝问了一个关于Pandas中的问题,这里拿出来给大家分享下,一起学习。想问一
- 一、工厂模式(Factory Pattern)工厂模式(Factory Pattern),提供了一种实例化(创建)对象的最佳方式。在工厂模式
- 1. 前言当日期数据作为图表的坐标轴时通常需要特殊处理,应为日期字符串比较长,容易产生重叠现象2. 设定主/次刻度2.1 引用库from m
- 本文实例讲述了vue实现父子组件之间的通信以及兄弟组件的通信功能。分享给大家供大家参考,具体如下:<!DOCTYPE html>
- 扫描某文件夹下所有文件(包括子文件夹中的文件),使用os.walk;os.walk() 方法用于通过在目录树种游走输出在目录中的文件名,向上
- 微信(WeChat)是腾讯公司于2011年1月21日推出的一款社交软件,8年时间微信做到日活10亿,日消息量450亿。在此期间微信也推出了不
- 前言使用 Python 的时候,我们知道 list 是一个长度可变对的数组, 可以通过 insert,append 和 extend 轻易的
- 七牛云存储的 Python 语言版本 SDK(本文以下称 Python-SDK)是对七牛云存储API协议的一层封装,以提供一套对于 Pyth
- XML有很多相关的技术,将这些技术结合起来,才能充分发挥XML的强大功能。这些技术包括:Xlink与Xpointer(设置XML的超链接)、