mybatis-plus入门

maven配置

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
<!--    sql    -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.15</version>
</dependency>
<!-- generator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.9</version>
</dependency>

代码生成器

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
52
53
54
55
56
57
package cn.sdadgz.common;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

/**
* <p>
* 废物本物
* </p>
*
* @author sdadgz
* @since 2023/2/15 21:51:54
*/
public class Generator {
public static void main(String[] args) {
// src 输出目录
String outputDir = "D:\\工具\\编程\\idea\\java工作区\\springcloudalibaba\\seata\\src\\main\\java";
// resources 输出目录
String mapperOutputDir = "D:\\工具\\编程\\idea\\java工作区\\springcloudalibaba\\seata\\src\\main\\resources"
+ "\\mapper";
// 父项目包名
String packagePath = "cn.sdadgz";

// 表名
String tableName = "user";

FastAutoGenerator.create("jdbc:mysql://sdadgz.cn:3307/spring-cloud", "root", "123456")
.globalConfig(builder -> {
builder.author("sdadgz") // 设置作者
.enableSwagger() // 开启 swagger 模式
.disableOpenDir()//完成后不打开文件夹
// 指定输出目录
//直接右键复制项目根目录的绝对路径
.outputDir(outputDir);
})
.packageConfig(builder -> {
builder.parent(packagePath) // 设置父包名
// 设置mapperXml生成路径
//直接右键复制项目mapper文件夹的绝对路径
.pathInfo(Collections.singletonMap(OutputFile.xml, mapperOutputDir));
})
.strategyConfig(builder -> {
builder.addInclude(tableName)//设置表名
.entityBuilder() //entity前置,才能用lombok
.enableLombok() //使用lombok
.controllerBuilder()
.enableRestStyle() //restMapping注解
.mapperBuilder()
.enableMapperAnnotation(); // 开启mapper注解
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}

配置文件

哪天我要看不懂这个了,重开吧

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
datasource:
url: jdbc:mysql://sdadgz.cn:3307/spring-cloud?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
auto-mapping-behavior: full
type-aliases-package: cn.sdadgz.entity