旧的代码生成

记得导包,依赖如下


<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 代码生成器 一个是新的代码生成器,一个是旧的问题-->
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus-generator</artifactId>-->
<!-- <version>3.4.1</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>

代码


/**
* @description:
* @author: HaHa
* @time: 2022/4/19 20:04
*/
public class AutoMain {
public static void main(String[] args) {
AutoGenerator generator = new AutoGenerator(); // 设置全局配置
GlobalConfig config = new GlobalConfig();
String s = System.getProperty("user.dir");
config.setOutputDir(s+"/src/main/java");
config.setAuthor("mybatis自动生成");
config.setOpen(false);
config.setFileOverride(false); //是否文件覆盖
config.setSwagger2(true);
config.setServiceName("%sService"); //去掉Service前面的I
config.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
generator.setGlobalConfig(config); //2、设置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/admin-springboot?serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456789");
dsc.setDbType(DbType.MYSQL);
generator.setDataSource(dsc); //3、包的配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("edu"); //模块名
// 包:com.lu.edu
pc.setParent("com.lu");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
generator.setPackageInfo(pc); //4、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("sys_user"); // 设置要映射的表名
//数据库表 映射到实体的命名策略
strategy.setNaming(NamingStrategy.underline_to_camel);
//数据库表字段 映射到实体的命名策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//生成实体时去掉表前缀
strategy.setTablePrefix(pc.getModuleName() + "_"); strategy.setEntityLombokModel(true); // 自动lombok;
strategy.setLogicDeleteFieldName("deleted"); //逻辑字段,数据库字段里面有deteted,然后自动配置
// 自动填充配置
TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
TableFill gmtModified = new TableFill("gmt_modified",FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(gmtCreate);
tableFills.add(gmtModified);
strategy.setTableFillList(tableFills);
// 乐观锁
strategy.setVersionFieldName("version");
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true); //下划线相当于/
generator.setStrategy(strategy); generator.execute();
}
}

部分代码详解

定义生成的实体类中日期类型

  • 默认生成日期格式为 LocalDateTimeLocalDate

  • 自定义添加类型 添加设置 gc.setDateType(DateType.ONLY_DATE); 已实现 Date 类型.

  • 可添加注解来实现前后端日期格式的转换

  • @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")

主键策略

  • 默认雪花算法
@TableId(type = IdType.INPUT)    自行输入id
@TableId(type = IdType.AUTO) 主键自增
@TableId(type = IdType.ASSIGN_ID) 雪花算法
@TableId(type = IdType.ASSIGN_UUID)

乐观锁

  • 参考即可

(91条消息) mybatis plus的乐观锁使用总结_霉男纸的博客-CSDN博客_mybatisplus乐观锁

(91条消息) mybatis-plus的乐观锁_zhuzai233的博客-CSDN博客

自定义的模板

新的代码生成器

更多详解看官网:

https://baomidou.com/pages/779a6e/#使用


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile; import java.util.Collections; /**
* @description:
* @author: HaHa
* @time: 2022/4/29 16:54
*/
public class AutoFast {
public static void main(String[] args) {
generate();
} private static void generate() {
// UTC是根据原子钟来计算时间,而GMT是根据地球的自转和公转来计算时间
String url="jdbc:mysql://localhost:3306/admin-springboot?serverTimezone=GMT%2b8";
String username="root";
String password="123456789";
String outputDir = System.getProperty("user.dir");
String parentPackage="com"; DataSourceConfig.Builder database = new DataSourceConfig.Builder(url, username, password);
FastAutoGenerator.create(database)
.globalConfig(builder -> {
builder.author("雨同我") .enableSwagger()
.fileOverride()
.outputDir(outputDir+"/src/main/java/");
})
.packageConfig(builder -> {
builder.parent(parentPackage)
.moduleName(null)
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, outputDir+"/src/main/resources/mapper/"));
})
.strategyConfig(builder -> {
builder.entityBuilder().enableLombok();
// builder.mapperBuilder().enableMapperAnnotation().build(); //这个是在每个mapper上面添加@Mapper
builder.controllerBuilder().enableHyphenStyle() // 开启驼峰转连字符
.enableRestStyle(); // 开启生成@RestController 控制器
builder.addInclude("sys_user") // 设置需要生成的表名
.addTablePrefix("t_", "sys_"); // 设置过滤表前缀
})
//.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
} }

mybatis-plus详解的更多相关文章

  1. 一、Mybatis配置详解

    Mybatis配置详解 XML配置文件层次结构 下图展示了mybatis-config.xml的全部配置元素 properties元素 properties是一个配置属性的元素,让我们能在配置文件的上 ...

  2. ORM框架对比以及Mybatis配置文件详解

    ORM框架对比以及Mybatis配置文件详解 0.数据库操作框架的历程 (1) JDBC ​ JDBC(Java Data Base Connection,java数据库连接)是一种用于执行SQL语句 ...

  3. myBatis foreach详解【转】

    MyBatis的foreach语句详解 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,ope ...

  4. MyBatis Generator 详解

    MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...

  5. MyBatis Generator 详解 【转来纯为备忘】

    版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com   目录(?)[+] MyBatis Generator中文文档 运行MyBatis Generator X ...

  6. Mybatis配置详解

    一.SqlSession的使用范围说明  1.SQLSessionFactoryBuilder   通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory, ...

  7. MyBatis Generator 详解(转)

    MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...

  8. MyBatis Geneator详解<转>

    MyBatis Geneator中文文档地址: http://generator.sturgeon.mopaas.com/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中文版的文档的 ...

  9. Mybatis全面详解——上(学习总结)

    原文地址:https://blog.csdn.net/ITITII/article/details/79969447 一.什么是Mybatis 这里借用官网的一句话介绍什么是mybatis:MyBat ...

  10. MyBatis Generator 详解 专题

    idea中有plugin可提高效率: http://www.henryxi.com/use-idea-mybatis-plugin-generate-mapper-files eg: <?xml ...

随机推荐

  1. 配置Linux的时钟同步

    公众号关注 「开源Linux」 回复「学习」,有我为您特别筛选的学习资料~ Ubuntu系统默认的时钟同步服务器是ntp.ubuntu.com,Debian则是0.debian.pool.ntp.or ...

  2. 『现学现忘』Git基础 — 23、Git中的撤销操作

    目录 1.撤销操作说明 2.撤销工作区中文件的修改 3.撤销暂存区中文件的修改 4.总结 1.撤销操作说明 我们在使用Git版本管理时,往往需要撤销某些操作.比如说我们想将某个修改后的文件撤销到上一个 ...

  3. CentOS7安装部署Mongodb

    1.下载安装包 打开官网,跳转至下载界面,选择对应版本的安装包,拷贝其链接,这里是手动安装,所以下载tgz安装包,如果要自动化安装,选择server的rpm自动安装包 https://www.mong ...

  4. 聊聊 HTTPS

    聊聊 HTTPS 本文写于 2021 年 6 月 30 日 最近工作也是越来越忙了,不像上学的时候,一天下来闲着没事可以写两篇博客. 今天来聊一下 HTTPS. HTTP HTTP 是不安全的协议. ...

  5. 每天一个 HTTP 状态码 前言

    前前言 在重新开始写博文(其实大多也就最多算是日常笔记小结)之际,就想着从短小精悍的文章入手,就想到了 HTTP 状态码.另外,记得很久之前,看过一个<每天一个 Linux 命令>系列文章 ...

  6. 【雅礼集训 2017 Day2】棋盘游戏

    loj 6033 description 给一个\(n*m\)的棋盘,'.'为可通行,'#'为障碍.Alice选择一个起始点,Bob先手从该点往四个方向走一步,Alice再走,不能走走过的点,谁不能动 ...

  7. 产品揭秘】来也Lead 2022产品亮点解读-RPA学习天地

    2022年4月26日,来也举行新品发布会.作为技术人员,花里胡哨的我且不说,我且说技术相关.整体架构"概念"整个平台覆盖了智能自动化的全生命周期包含:业务理解.流程创建.随处运行. ...

  8. CSP J/S 初赛总结

    CSP J/S 初赛总结 2021/9/19 19:29 用官方答案估计 J 涂卡的时候唯一的一支 2B 铅笔坏了,只能用笔芯一个个涂 选择 \(-6\ pts\) 判断 \(-3\ pts\) 回答 ...

  9. 2021.03.20【NOIP提高B组】模拟 总结

    区间 DP 专场:愉快爆炸 T1 题目大意 有 \(n\) 个有颜色的块,连续 \(k\) 个相同颜色的就可以消掉 现在可以在任意位置插入任意颜色的方块,问最少插入多少个可以全部抵消 题解 先把连续的 ...

  10. hadoop集群搭建——单节点(伪分布式)

    1. 准备工作: 前提:需要电脑安装VM,且VM上安装一个Linux系统 注意:本人是在学习完尚学堂视频后,结合自己的理解,在这里做的总结.学习的视频是:大数据. 为了区分是在哪一台机器做的操作,eg ...