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」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 可以使用df命令来显示在Linux.macOS和类Unix系统中挂载的文件系统上有多少可用磁盘 ...
- Vue报错: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'protocol')
Vue报错: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'protocol') 报错信 ...
- 【软件构造】Mutable类型与Immutable类型
[软件构造]Mutable类型与Immutable类型 1.前言 在软件构造这门课中,对mutable类型和immutable类型的深入理解,有助于后续ADT.可维护性.可复用性的学习,因此我们有必要 ...
- Redis进阶知识一览
Redis的持久化机制 RDB: Redis DataBase 什么是RDB RDB∶每隔一段时间,把内存中的数据写入磁盘的临时文件,作为快照,恢复的时候把快照文件读进内存.如果宕机重启,那么内存里的 ...
- SpringBoot实现基于token的登录验证
一.SpringBoot实现基于token的登录验证 基于token的登录验证实现原理:客户端通过用户名和密码调用登录接口,当验证数据库中存在该用户后,将用户的信息按照token的生成规则,生成一个字 ...
- K8S 使用Minikube搭建Kubernetes(K8S)~单机运行Kubernetes~适用于快速学习
在一台主机上运行起来的Kubernetes,仅适用于学习!~~~ 系统版本:CentOS Linux release 7.6.1810 (Core) 软件版本:Docker-ce-18.06.0.Ku ...
- 学习Linux须知1.2之Linux命令的实战
(一)学习Linux 的准备工作 1.在线学习linux 学习网站推荐:Linux 基础入门_Linux - 蓝桥云课 (lanqiao.cn) 2.连接远程服务器学习[下文的案例就是使用xshell ...
- ABP框架之——数据访问基础架构
大家好,我是张飞洪,感谢您的阅读,我会不定期和你分享阅读心得,希望我的文章能成为你成长路上的一块垫脚石,我们一起精进. 几乎所有的业务应用程序都要适用一种数据库基础架构,用来实现数据访问逻辑,以便从数 ...
- Linux下添加MySql组件后报无权限问题解决
Tomcat日志报错如下: Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using pas ...
- 【物联网天线选择攻略】2.4GHz 频段增益天线模块设备选择
天线模块设备(antenna)是一种能量变换器,它把传输线上传播的导行波,变换成在无界媒介中传播的电磁波,或者进行相反的变换.对于设计一个应用于射频系统中的小功率.短距离的2.4GHz无线收发设备, ...