Skip to content

Commit 818a810

Browse files
committed
add mybatisPlus to table ecosystem
1 parent 3b2815e commit 818a810

9 files changed

Lines changed: 1308 additions & 0 deletions

File tree

91.5 KB
Loading

src/.vuepress/sidebar/V2.0.x/en-Table.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ export const enSidebar = {
168168
{ text: 'DBeaver', link: 'DBeaver' },
169169
],
170170
},
171+
{
172+
text: 'Programming Framework',
173+
collapsible: true,
174+
children: [
175+
{ text: 'Spring Boot Starter', link: 'Spring-Boot-Starter' },
176+
{ text: 'Mybatis Generator', link: 'Mybatis-Generator' },
177+
{ text: 'MyBatisPlus Generator', link: 'MyBatisPlus-Generator' },
178+
],
179+
},
171180
],
172181
},
173182
{

src/.vuepress/sidebar/V2.0.x/zh-Table.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ export const zhSidebar = {
158158
{ text: 'DBeaver', link: 'DBeaver' },
159159
],
160160
},
161+
{
162+
text: '编程框架',
163+
collapsible: true,
164+
children: [
165+
{ text: 'Spring Boot Starter', link: 'Spring-Boot-Starter' },
166+
{ text: 'Mybatis Generator', link: 'Mybatis-Generator' },
167+
{ text: 'MyBatisPlus Generator', link: 'MyBatisPlus-Generator' },
168+
],
169+
},
161170
],
162171
},
163172
{

src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ export const enSidebar = {
173173
{ text: 'DBeaver', link: 'DBeaver' },
174174
],
175175
},
176+
{
177+
text: 'Programming Framework',
178+
collapsible: true,
179+
children: [
180+
{ text: 'Spring Boot Starter', link: 'Spring-Boot-Starter' },
181+
{ text: 'Mybatis Generator', link: 'Mybatis-Generator' },
182+
{ text: 'MyBatisPlus Generator', link: 'MyBatisPlus-Generator' },
183+
],
184+
},
176185
],
177186
},
178187
{

src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ export const zhSidebar = {
162162
{ text: 'DBeaver', link: 'DBeaver' },
163163
],
164164
},
165+
{
166+
text: '编程框架',
167+
collapsible: true,
168+
children: [
169+
{ text: 'Spring Boot Starter', link: 'Spring-Boot-Starter' },
170+
{ text: 'Mybatis Generator', link: 'Mybatis-Generator' },
171+
{ text: 'MyBatisPlus Generator', link: 'MyBatisPlus-Generator' },
172+
],
173+
},
165174
],
166175
},
167176
{
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
20+
# MyBatisPlus Generator
21+
22+
## 1. Overview
23+
24+
MyBatis-Plus Generator is a built-in code generation tool of the MyBatis-Plus framework. It automatically generates standardized entity classes, Mapper interfaces, Service layers, and Controller layer code based on the database table structure. It integrates MyBatis-Plus's general CRUD methods (such as `BaseMapper` built-in create, read, update, delete) and condition constructors (`QueryWrapper`), and supports extended annotations like Lombok and Swagger. Through simple configuration, it can quickly build persistent layer code that conforms to enterprise-level specifications, greatly reducing repetitive development work for single-table operations. It is suitable for quickly building background management systems or standardized data service modules.
25+
26+
The following will introduce how to use Mybatis-Plus Generator to connect to IoTDB and generate entity classes, Mapper interfaces, Service layers, and Controller layer code files corresponding to database tables.
27+
28+
## 2. Usage Steps
29+
30+
### 2.1 Version Requirements
31+
32+
- IoTDB: >=2.0.2-SNAPSHOT
33+
- mybatisPlus: >=3.5.10
34+
- iotdb-jdbc:>=2.0.4-SNAPSHOT
35+
36+
### 2.2 Operating Process
37+
38+
#### 2.2.1 IoTDB Environment Setup
39+
40+
1. Download, install, and start the IoTDB service. For details, refer to [QuickStart](../QuickStart/QuickStart.md)
41+
2. Create the database database1 and tables table1 / table2. Relevant SQL statements can refer to [Sample-Data](../Reference/Sample-Data.md)
42+
43+
#### 2.2.2 Create a Maven Project
44+
45+
1. Create a Maven project
46+
2. Add the following dependency configurations to the pom
47+
48+
```XML
49+
<properties>
50+
<maven.compiler.source>17</maven.compiler.source>
51+
<maven.compiler.target>17</maven.compiler.target>
52+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
53+
</properties>
54+
55+
<dependencies>
56+
<dependency>
57+
<groupId>com.baomidou</groupId>
58+
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
59+
<version>3.5.10</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.baomidou</groupId>
63+
<artifactId>mybatis-plus-generator</artifactId>
64+
<version>3.5.10</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.github.jeffreyning</groupId>
68+
<artifactId>mybatisplus-plus</artifactId>
69+
<version>1.7.5-RELEASE</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.apache.velocity</groupId>
73+
<artifactId>velocity-engine-core</artifactId>
74+
<version>2.0</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.apache.iotdb</groupId>
78+
<artifactId>iotdb-jdbc</artifactId>
79+
<version>2.0.4-SNAPSHOT</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-starter</artifactId>
84+
<version>3.4.3</version>
85+
<exclusions>
86+
<exclusion>
87+
<groupId>org.slf4j</groupId>
88+
<artifactId>slf4j-api</artifactId>
89+
</exclusion>
90+
<exclusion>
91+
<groupId>commons-logging</groupId>
92+
<artifactId>commons-logging</artifactId>
93+
</exclusion>
94+
</exclusions>
95+
</dependency>
96+
<dependency>
97+
<groupId>org.springframework.boot</groupId>
98+
<artifactId>spring-boot-starter-web</artifactId>
99+
<version>3.4.3</version>
100+
<exclusions>
101+
<exclusion>
102+
<groupId>org.slf4j</groupId>
103+
<artifactId>slf4j-api</artifactId>
104+
</exclusion>
105+
<exclusion>
106+
<groupId>commons-logging</groupId>
107+
<artifactId>commons-logging</artifactId>
108+
</exclusion>
109+
</exclusions>
110+
</dependency>
111+
<dependency>
112+
<groupId>io.springfox</groupId>
113+
<artifactId>springfox-swagger2</artifactId>
114+
<version>3.0.0</version>
115+
</dependency>
116+
<dependency>
117+
<groupId>io.springfox</groupId>
118+
<artifactId>springfox-swagger-ui</artifactId>
119+
<version>3.0.0</version>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.projectlombok</groupId>
123+
<artifactId>lombok</artifactId>
124+
<version>1.18.36</version>
125+
</dependency>
126+
<dependency>
127+
<groupId>org.springframework.boot</groupId>
128+
<artifactId>spring-boot-starter-test</artifactId>
129+
<version>3.4.3</version>
130+
<scope>test</scope>
131+
</dependency>
132+
<dependency>
133+
<groupId>org.slf4j</groupId>
134+
<artifactId>slf4j-api</artifactId>
135+
<version>2.0.13</version>
136+
</dependency>
137+
<dependency>
138+
<groupId>ch.qos.logback</groupId>
139+
<artifactId>logback-classic</artifactId>
140+
<version>1.5.16</version>
141+
</dependency>
142+
<dependency>
143+
<groupId>ch.qos.logback</groupId>
144+
<artifactId>logback-core</artifactId>
145+
<version>1.5.16</version>
146+
</dependency>
147+
</dependencies>
148+
<build>
149+
<plugins>
150+
<plugin>
151+
<groupId>org.mybatis.generator</groupId>
152+
<artifactId>mybatis-generator-maven-plugin</artifactId>
153+
<version>1.4.2</version>
154+
<dependencies>
155+
<dependency>
156+
<groupId>org.apache.iotdb</groupId>
157+
<artifactId>mybatis-generator-plugin</artifactId>
158+
<version>2.0.2-SNAPSHOT</version>
159+
</dependency>
160+
</dependencies>
161+
<configuration>
162+
<verbose>true</verbose>
163+
<overwrite>true</overwrite>
164+
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
165+
</configuration>
166+
</plugin>
167+
</plugins>
168+
</build>
169+
```
170+
3. Create an execution file, modify the `url`/`username`/`password` of the target IoTDB, and the target file generation directory `outputDir`/`pathInfo`
171+
172+
```Java
173+
package org.apache.iotdb;
174+
175+
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
176+
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
177+
import com.baomidou.mybatisplus.generator.config.OutputFile;
178+
import com.baomidou.mybatisplus.generator.config.rules.DateType;
179+
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
180+
import org.apache.iotdb.jdbc.IoTDBDataSource;
181+
import org.mybatis.spring.annotation.MapperScan;
182+
import org.springframework.boot.SpringApplication;
183+
import org.springframework.boot.autoconfigure.SpringBootApplication;
184+
185+
import java.sql.Types;
186+
import java.util.Collections;
187+
188+
189+
@SpringBootApplication
190+
@MapperScan("org.apache.iotdb.mapper")
191+
public class Main {
192+
public static void main(String[] args) {
193+
SpringApplication.run(Main.class, args);
194+
IoTDBDataSource dataSource = new IoTDBDataSource();
195+
dataSource.setUrl("jdbc:iotdb://127.0.0.1:6667/database1?sql_dialect=table");
196+
dataSource.setUser("root");
197+
dataSource.setPassword("root");
198+
FastAutoGenerator generator = FastAutoGenerator.create(new DataSourceConfig.Builder(dataSource).driverClassName("org.apache.iotdb.jdbc.IoTDBDriver"));
199+
generator
200+
.globalConfig(builder -> {
201+
builder.author("IoTDB")
202+
.enableSwagger()
203+
.dateType(DateType.ONLY_DATE)
204+
.outputDir("src/main/java");
205+
})
206+
.packageConfig(builder -> {
207+
builder.parent("org.apache.iotdb")
208+
.mapper("mapper")
209+
.pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/java/org/apache/iotdb/xml"));
210+
})
211+
.dataSourceConfig(builder -> {
212+
builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
213+
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
214+
switch (typeCode) {
215+
case Types.FLOAT:
216+
return DbColumnType.FLOAT;
217+
default:
218+
return typeRegistry.getColumnType(metaInfo);
219+
}
220+
});
221+
})
222+
.strategyConfig(builder -> {
223+
builder.addInclude("table1");
224+
builder.entityBuilder()
225+
.enableLombok()
226+
// .addIgnoreColumns("create_time")
227+
.enableFileOverride();
228+
builder.serviceBuilder()
229+
.formatServiceFileName("%sService")
230+
.formatServiceImplFileName("%sServiceImpl")
231+
.convertServiceFileName((entityName -> entityName + "Service"))
232+
.enableFileOverride();
233+
builder.controllerBuilder()
234+
.enableRestStyle()
235+
.enableFileOverride();
236+
})
237+
.strategyConfig(builder -> {
238+
builder.addInclude("table2");
239+
builder.entityBuilder()
240+
.enableLombok()
241+
// .addIgnoreColumns("create_time")
242+
.enableFileOverride();
243+
builder.serviceBuilder()
244+
.formatServiceFileName("%sService")
245+
.formatServiceImplFileName("%sServiceImpl")
246+
.convertServiceFileName((entityName -> entityName + "Service"))
247+
.enableFileOverride();
248+
builder.controllerBuilder()
249+
.enableRestStyle()
250+
.enableFileOverride();
251+
})
252+
.execute();
253+
}
254+
}
255+
```
256+
257+
#### 2.2.3 Generate Target Files
258+
259+
1. Run Main.java
260+
2. View the log output. The following indicates that the target files are generated
261+
262+
```Java
263+
16:10:08.943 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator -- ==========================File generation completed!!!==========================
264+
```
265+
266+
#### 2.2.4 Generate Target Files
267+
268+
```Plain
269+
org/apache/iotdb/controller/Table1Controller.java
270+
org/apache/iotdb/controller/Table2Controller.java
271+
org/apache/iotdb/entity/Table1.java
272+
org/apache/iotdb/mapper/Table2.xml
273+
org/apache/iotdb/service/Table1Service.java
274+
org/apache/iotdb/service/Table2Service.java
275+
org/apache/iotdb/service/impl/Table1ServiceImpl.java
276+
org/apache/iotdb/service/impl/Table2ServiceImpl.java
277+
org/apache/iotdb/xml/Table1Mapper.xml
278+
org/apache/iotdb/xml/Table2Mapper.xml
279+
```
280+
![](/img/MyBatisPlus-Generator.png.png)
281+
282+
#### 2.2.5 Modify Annotations
283+
284+
Manually adjust the generated code `org/apache/iotdb/entity/Table1.java`, `org/apache/iotdb/entity/Table2.java` to support multi-primary key queries.
285+
286+
```TypeScript
287+
// Add new import
288+
import com.github.jeffreyning.mybatisplus.anno.MppMultiId;
289+
290+
// Add new annotation @MppMultiId
291+
@MppMultiId
292+
// Modify annotation @TableId() -->> @TableField()
293+
@TableField("time")
294+
private Date time;
295+
296+
// Add new annotation @MppMultiId
297+
@MppMultiId
298+
// Modify annotation @TableId() -->> @TableField()
299+
@TableField("region")
300+
private String region;
301+
302+
// Add new annotation @MppMultiId
303+
@MppMultiId
304+
// Modify annotation @TableId() -->> @TableField()
305+
@TableField("plant_id")
306+
private String plantId;
307+
308+
// Add new annotation @MppMultiId
309+
@MppMultiId
310+
// Modify annotation @TableId() -->> @TableField()
311+
@TableField("device_id")
312+
private String deviceId;
313+
```
314+
315+
## 3. Usage Example
316+
317+
For a complete usage example, refer to the source code [examples/mybatisplus-generator](https://github.com/apache/iotdb-extras/tree/master/examples/mybatisplus-generator)
318+

0 commit comments

Comments
 (0)