mybatis-plus详解
旧的代码生成
记得导包,依赖如下
<!-- 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();
}
}
部分代码详解
定义生成的实体类中日期类型
默认生成日期格式为 LocalDateTime 和 LocalDate

自定义添加类型 添加设置 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详解的更多相关文章
- 一、Mybatis配置详解
Mybatis配置详解 XML配置文件层次结构 下图展示了mybatis-config.xml的全部配置元素 properties元素 properties是一个配置属性的元素,让我们能在配置文件的上 ...
- ORM框架对比以及Mybatis配置文件详解
ORM框架对比以及Mybatis配置文件详解 0.数据库操作框架的历程 (1) JDBC JDBC(Java Data Base Connection,java数据库连接)是一种用于执行SQL语句 ...
- myBatis foreach详解【转】
MyBatis的foreach语句详解 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,ope ...
- MyBatis Generator 详解
MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...
- MyBatis Generator 详解 【转来纯为备忘】
版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com 目录(?)[+] MyBatis Generator中文文档 运行MyBatis Generator X ...
- Mybatis配置详解
一.SqlSession的使用范围说明 1.SQLSessionFactoryBuilder 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory, ...
- MyBatis Generator 详解(转)
MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...
- MyBatis Geneator详解<转>
MyBatis Geneator中文文档地址: http://generator.sturgeon.mopaas.com/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中文版的文档的 ...
- Mybatis全面详解——上(学习总结)
原文地址:https://blog.csdn.net/ITITII/article/details/79969447 一.什么是Mybatis 这里借用官网的一句话介绍什么是mybatis:MyBat ...
- MyBatis Generator 详解 专题
idea中有plugin可提高效率: http://www.henryxi.com/use-idea-mybatis-plugin-generate-mapper-files eg: <?xml ...
随机推荐
- 实战 | Linux根分区扩容
一个执着于技术的公众号 一个执着于技术的公众号 前言 Linux系统作为服务器操作系统,经常遇到一个问题就是服务器分区磁盘空间不足需要扩容的情况.本文以linux系统最常见的发行版centos7系统为 ...
- NLP教程(5) - 语言模型、RNN、GRU与LSTM
作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/36 本文地址:http://www.showmeai.tech/article-det ...
- Java 对象头那点事
概览 对象头 存放:关于堆对象的布局.类型.GC状态.同步状态和标识哈希码的基本信息.Java对象和vm内部对象都有一个共同的对象头格式. (后面做详细介绍) 实例数据 存放:类的数据信息,父类的信息 ...
- 从rocketmq入手,解析各种零拷贝的jvm层原理
在上一篇文章中,主要介绍了rocketmq消息的存储流程.其主要使用了mmap的零拷贝技术实现了硬盘和内存的映射,从而提高了读写性能.在流程中有一个非常有意思的预热方法并没有详细分析,因为其中涉及到了 ...
- 安装Python到Linux(Pyenv)
pyenv是一个多Python版本的托管工具,我们可以使用它安装Python和随意的切换系统环境中默认使用的Python版本. 运行环境 系统版本:CentOS Linux release 7.6.1 ...
- veeambackup通过虚拟机还原系统文件操作说明
如何从 VeeamBackup Replication 从备份中提取文件恢复到本地.当我们的服务器中误操作删除了一些文件特别是共享文件,文件被删除后往往都是几个小时或者几天后才被发现.特别是文件服务器 ...
- 我熬夜读完这份“高分宝典”,竟4面拿下字节跳动offer
前言 怎样的契机? 实际上,目前毕业已经两年时间了,在大学时就已经开始关注字节跳动的发展.一开始,我是电气自动化专业的,大二清楚目标之后就转计算机了,大四进了一家小型的互联网公司实习,具体就不说哪家了 ...
- SQL年龄计算方法
第一种方法: 用DATEDIFF函数,DATEDIFF(YEAR,beginDate,endDate). 测试语句: 1 DECLARE @birthdayDate DATE 2 DECLARE @e ...
- 联发科 (MTK) sensor bring up
MT6768平台 1.添加驱动文件 2.添加硬件配置支持 3.添加硬件配置 4.添加编译配置 5.分配空间(非必要,当代码量超过当前空间大小时将会报错,根据报错log改大小即可.) 6.兼容配置 7. ...
- 回流&重绘
浏览器加载解析页面:把HTML解析为DOM树,解析CSS生成CSSOM树,HTML DOM树和CSSOM树组合构建render树,首次触发回流和重绘后将页面结构信息发送给GPU进行绘制渲染. 回流:当 ...