Mybatisplus 再学习
前言:
本文对代码生成器、多数据源、mybatisx插件作了简单介绍。
其官网均有对应的内容,作为新手的我阅读起来有点困难,希望本文能帮助读者进行理解。
代码生成器:https://baomidou.com/guides/code-generator/
多数据源支持;https://baomidou.com/guides/dynamic-datasource/
mybatisx:https://baomidou.com/guides/mybatis-x/
代码生成器
代码生成器:顾名思义,就是根据数据库表帮我们生成实体类、MVC三层模型的基础CURD,大大简化了开发。)
mybatis-plus-generator 3.5.1以下版本:为旧版本的代码生成器。可以自行查阅官网。
详细经过以下案例,你对对代码生成器会有一定了解的。
使用案例:
依赖:
1
2
3
4
5
|
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.9</version>
</dependency>
|
代码生成的模板框架:
image-20241211141755550
1
2
3
4
5
6
|
<!-- 这个是代码生成的模板框架-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
|
编写代码生成器:
有快速生成和交互式生成的方法。这里就演示快速生成。
在对应的包下创建代码生成类:
image-20241211142053680
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package top.rose.mpgender;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.sql.Types;
import java.util.Collections;
/**
* @ author: rose
* @ description: 简单的代码生成器的使用案例。
*/
public class generator {
public static void main(String[] args) {
// 1 编写输出的路径,可以不用修改
final String OUTPUTPATH=System.getProperty("user.dir")+"\\src\\main\\java";
final String XMLOUTPUTPATH=System.getProperty("user.dir")+"\\src\\main\\resources\\mapper\\";
final String url = "jdbc:mysql://localhost:3306/personal_health?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC"
// 2 这里填写连接信息
FastAutoGenerator.create(url, "root", "123456")
.globalConfig(builder -> {
builder.author("rose") // 设置作者
.outputDir(OUTPUTPATH); // 指定输出目录
})
.dataSourceConfig(builder ->
builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
if (typeCode == Types.SMALLINT) {
// 自定义类型转换
return DbColumnType.INTEGER;
}
return typeRegistry.getColumnType(metaInfo);
})
)
// 3 设置模板名和 xml文件位置
.packageConfig(builder ->
builder.parent("top.rose") // 设置父包名
.moduleName("mp") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, XMLOUTPUTPATH))
)
// 4 指定要生成的表,以及设置需要去掉的前缀
.strategyConfig(builder ->
builder.addInclude("user") // 设置需要生成的表名
.addTablePrefix("user_") // 设置过滤表前缀
)
// 5 使用Freemarker引擎模板,默认的是Velocity引擎模板
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
}
|
运行-生成代码
运行过后,就会生成对应的三层架构以及实体类。
image-20241211142616070
注意:案例中 生成的代码不是和 启动类在一个包中的,实际使用时要设置为同一个包。
至此,一个简单的代码生成器案例就结束了,深入学习,查看官网,相信这个时候,已经能看到官网是什么意思了。
还可以深入了解其各个配置的使用场景。
多数据源
适用于多种场景:纯粹多库、 读写分离、 一主多从、 混合模式等
这里简单说明一下,多库的使用。
导入依赖:
1
2
3
4
5
|
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
|
配置多数据源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
spring:
# 配置数据源信息
datasource:
dynamic:
# 设置默认的数据源或者数据源组,默认值即为master
primary: master
# 严格匹配数据源,默认false.true未匹配到指定数据源时抛异常,false使用默认数据源
strict: false
datasource:
master:
url: jdbc:mysql://localhost:3306/mp?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
slave_1:
url: jdbc:mysql://localhost:3306/mp1?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
|
最后,要使用哪个库就添加上 @DS("slave_1")
注解 指定所操作的数据源
MybatisX
一些复杂的SQL,多表联查,就需要自己去编写代码和SQL语句,这个时候可以使用MyBatisX插件.
MyBatisX一款基于 IDEA 的快速开发插件,为效率而生
官方地址:MybatisX快速开发插件 | MyBatis-Plus (baomidou.com)
安装MybatisX插件
File -> Settings -> Plugins -> Browse Repositories
在idea中的插件中搜索进行下载安装。
核心功能
xml 跳转
点击mapper中方法 前面的蓝色小鸟,即可一键跳转到对应的xml映射。
image-20241211144303213
代码快速生成
也就是对应其前面的代码生成器。
image-20241211144922450
编写对应的信息;(基础包、忽略前缀、忽略后缀等等)
设置注解 、组件、生成模板、生成位置等等。
image-20241211145255891
效果与代码生成器一致。
快速生成CURD
- 当你自定义编写mapper方法时,可以
alt+enter
选择快速生成映射。
image-20241211144555948
- 快速生成基础的CURD 。例如输入 insert 会提示出 insertxxxx,可以进行选择一键生成。(其他的类似)
image-20241211144737892
参考:
hua师傅: https://blog.csdn.net/weixin_49001740/article/details/123986857