diff --git a/src/.vuepress/public/img/MyBatisPlus-Generator.png b/src/.vuepress/public/img/MyBatisPlus-Generator.png new file mode 100644 index 000000000..612b57050 Binary files /dev/null and b/src/.vuepress/public/img/MyBatisPlus-Generator.png differ diff --git a/src/.vuepress/sidebar/V2.0.x/en-Table.ts b/src/.vuepress/sidebar/V2.0.x/en-Table.ts index fb6d4455a..b2e5044d6 100644 --- a/src/.vuepress/sidebar/V2.0.x/en-Table.ts +++ b/src/.vuepress/sidebar/V2.0.x/en-Table.ts @@ -168,6 +168,15 @@ export const enSidebar = { { text: 'DBeaver', link: 'DBeaver' }, ], }, + { + text: 'Programming Framework', + collapsible: true, + children: [ + { text: 'Spring Boot Starter', link: 'Spring-Boot-Starter' }, + { text: 'Mybatis Generator', link: 'Mybatis-Generator' }, + { text: 'MyBatisPlus Generator', link: 'MyBatisPlus-Generator' }, + ], + }, ], }, { diff --git a/src/.vuepress/sidebar/V2.0.x/zh-Table.ts b/src/.vuepress/sidebar/V2.0.x/zh-Table.ts index 6361b1d33..6318e4388 100644 --- a/src/.vuepress/sidebar/V2.0.x/zh-Table.ts +++ b/src/.vuepress/sidebar/V2.0.x/zh-Table.ts @@ -158,6 +158,15 @@ export const zhSidebar = { { text: 'DBeaver', link: 'DBeaver' }, ], }, + { + text: '编程框架', + collapsible: true, + children: [ + { text: 'Spring Boot Starter', link: 'Spring-Boot-Starter' }, + { text: 'Mybatis Generator', link: 'Mybatis-Generator' }, + { text: 'MyBatisPlus Generator', link: 'MyBatisPlus-Generator' }, + ], + }, ], }, { diff --git a/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts b/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts index 2620c9f23..d1a1b32d6 100644 --- a/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts +++ b/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts @@ -173,6 +173,15 @@ export const enSidebar = { { text: 'DBeaver', link: 'DBeaver' }, ], }, + { + text: 'Programming Framework', + collapsible: true, + children: [ + { text: 'Spring Boot Starter', link: 'Spring-Boot-Starter' }, + { text: 'Mybatis Generator', link: 'Mybatis-Generator' }, + { text: 'MyBatisPlus Generator', link: 'MyBatisPlus-Generator' }, + ], + }, ], }, { diff --git a/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts b/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts index 135414f0a..6d6f3db9a 100644 --- a/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts +++ b/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts @@ -162,6 +162,15 @@ export const zhSidebar = { { text: 'DBeaver', link: 'DBeaver' }, ], }, + { + text: '编程框架', + collapsible: true, + children: [ + { text: 'Spring Boot Starter', link: 'Spring-Boot-Starter' }, + { text: 'Mybatis Generator', link: 'Mybatis-Generator' }, + { text: 'MyBatisPlus Generator', link: 'MyBatisPlus-Generator' }, + ], + }, ], }, { diff --git a/src/UserGuide/Master/Table/Ecosystem-Integration/MyBatisPlus-Generator.md b/src/UserGuide/Master/Table/Ecosystem-Integration/MyBatisPlus-Generator.md new file mode 100644 index 000000000..58968106d --- /dev/null +++ b/src/UserGuide/Master/Table/Ecosystem-Integration/MyBatisPlus-Generator.md @@ -0,0 +1,318 @@ + + +# MyBatisPlus Generator + +## 1. Overview + +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. + +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. + +## 2. Usage Steps + +### 2.1 Version Requirements + +- IoTDB: >=2.0.2-SNAPSHOT +- mybatisPlus: >=3.5.10 +- iotdb-jdbc:>=2.0.4-SNAPSHOT + +### 2.2 Operating Process + +#### 2.2.1 IoTDB Environment Setup + +1. Download, install, and start the IoTDB service. For details, refer to [QuickStart](../QuickStart/QuickStart.md) +2. Create the database database1 and tables table1 / table2. Relevant SQL statements can refer to [Sample-Data](../Reference/Sample-Data.md) + +#### 2.2.2 Create a Maven Project + +1. Create a Maven project +2. Add the following dependency configurations to the pom + +```XML + + 17 + 17 + UTF-8 + + + + + com.baomidou + mybatis-plus-spring-boot3-starter + 3.5.10 + + + com.baomidou + mybatis-plus-generator + 3.5.10 + + + com.github.jeffreyning + mybatisplus-plus + 1.7.5-RELEASE + + + org.apache.velocity + velocity-engine-core + 2.0 + + + org.apache.iotdb + iotdb-jdbc + 2.0.4-SNAPSHOT + + + org.springframework.boot + spring-boot-starter + 3.4.3 + + + org.slf4j + slf4j-api + + + commons-logging + commons-logging + + + + + org.springframework.boot + spring-boot-starter-web + 3.4.3 + + + org.slf4j + slf4j-api + + + commons-logging + commons-logging + + + + + io.springfox + springfox-swagger2 + 3.0.0 + + + io.springfox + springfox-swagger-ui + 3.0.0 + + + org.projectlombok + lombok + 1.18.36 + + + org.springframework.boot + spring-boot-starter-test + 3.4.3 + test + + + org.slf4j + slf4j-api + 2.0.13 + + + ch.qos.logback + logback-classic + 1.5.16 + + + ch.qos.logback + logback-core + 1.5.16 + + + + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.4.2 + + + org.apache.iotdb + mybatis-generator-plugin + 2.0.2-SNAPSHOT + + + + true + true + src/main/resources/generatorConfig.xml + + + + +``` +3. Create an execution file, modify the `url`/`username`/`password` of the target IoTDB, and the target file generation directory `outputDir`/`pathInfo` + +```Java +package org.apache.iotdb; + +import com.baomidou.mybatisplus.generator.FastAutoGenerator; +import com.baomidou.mybatisplus.generator.config.DataSourceConfig; +import com.baomidou.mybatisplus.generator.config.OutputFile; +import com.baomidou.mybatisplus.generator.config.rules.DateType; +import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; +import org.apache.iotdb.jdbc.IoTDBDataSource; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.sql.Types; +import java.util.Collections; + + +@SpringBootApplication +@MapperScan("org.apache.iotdb.mapper") +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + IoTDBDataSource dataSource = new IoTDBDataSource(); + dataSource.setUrl("jdbc:iotdb://127.0.0.1:6667/database1?sql_dialect=table"); + dataSource.setUser("root"); + dataSource.setPassword("root"); + FastAutoGenerator generator = FastAutoGenerator.create(new DataSourceConfig.Builder(dataSource).driverClassName("org.apache.iotdb.jdbc.IoTDBDriver")); + generator + .globalConfig(builder -> { + builder.author("IoTDB") + .enableSwagger() + .dateType(DateType.ONLY_DATE) + .outputDir("src/main/java"); + }) + .packageConfig(builder -> { + builder.parent("org.apache.iotdb") + .mapper("mapper") + .pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/java/org/apache/iotdb/xml")); + }) + .dataSourceConfig(builder -> { + builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> { + int typeCode = metaInfo.getJdbcType().TYPE_CODE; + switch (typeCode) { + case Types.FLOAT: + return DbColumnType.FLOAT; + default: + return typeRegistry.getColumnType(metaInfo); + } + }); + }) + .strategyConfig(builder -> { + builder.addInclude("table1"); + builder.entityBuilder() + .enableLombok() +// .addIgnoreColumns("create_time") + .enableFileOverride(); + builder.serviceBuilder() + .formatServiceFileName("%sService") + .formatServiceImplFileName("%sServiceImpl") + .convertServiceFileName((entityName -> entityName + "Service")) + .enableFileOverride(); + builder.controllerBuilder() + .enableRestStyle() + .enableFileOverride(); + }) + .strategyConfig(builder -> { + builder.addInclude("table2"); + builder.entityBuilder() + .enableLombok() +// .addIgnoreColumns("create_time") + .enableFileOverride(); + builder.serviceBuilder() + .formatServiceFileName("%sService") + .formatServiceImplFileName("%sServiceImpl") + .convertServiceFileName((entityName -> entityName + "Service")) + .enableFileOverride(); + builder.controllerBuilder() + .enableRestStyle() + .enableFileOverride(); + }) + .execute(); + } +} +``` + +#### 2.2.3 Generate Target Files + +1. Run Main.java +2. View the log output. The following indicates that the target files are generated + +```Java +16:10:08.943 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator -- ==========================File generation completed!!!========================== +``` + +#### 2.2.4 Generate Target Files + +```Plain +org/apache/iotdb/controller/Table1Controller.java +org/apache/iotdb/controller/Table2Controller.java +org/apache/iotdb/entity/Table1.java +org/apache/iotdb/mapper/Table2.xml +org/apache/iotdb/service/Table1Service.java +org/apache/iotdb/service/Table2Service.java +org/apache/iotdb/service/impl/Table1ServiceImpl.java +org/apache/iotdb/service/impl/Table2ServiceImpl.java +org/apache/iotdb/xml/Table1Mapper.xml +org/apache/iotdb/xml/Table2Mapper.xml +``` +![](/img/MyBatisPlus-Generator.png) + +#### 2.2.5 Modify Annotations + +Manually adjust the generated code `org/apache/iotdb/entity/Table1.java`, `org/apache/iotdb/entity/Table2.java` to support multi-primary key queries. + +```TypeScript +// Add new import +import com.github.jeffreyning.mybatisplus.anno.MppMultiId; + +// Add new annotation @MppMultiId +@MppMultiId +// Modify annotation @TableId() -->> @TableField() +@TableField("time") +private Date time; + +// Add new annotation @MppMultiId +@MppMultiId +// Modify annotation @TableId() -->> @TableField() +@TableField("region") +private String region; + +// Add new annotation @MppMultiId +@MppMultiId +// Modify annotation @TableId() -->> @TableField() +@TableField("plant_id") +private String plantId; + +// Add new annotation @MppMultiId +@MppMultiId +// Modify annotation @TableId() -->> @TableField() +@TableField("device_id") +private String deviceId; +``` + +## 3. Usage Example + +For a complete usage example, refer to the source code [examples/mybatisplus-generator](https://github.com/apache/iotdb-extras/tree/master/examples/mybatisplus-generator) + diff --git a/src/UserGuide/latest-Table/Ecosystem-Integration/MyBatisPlus-Generator.md b/src/UserGuide/latest-Table/Ecosystem-Integration/MyBatisPlus-Generator.md new file mode 100644 index 000000000..58968106d --- /dev/null +++ b/src/UserGuide/latest-Table/Ecosystem-Integration/MyBatisPlus-Generator.md @@ -0,0 +1,318 @@ + + +# MyBatisPlus Generator + +## 1. Overview + +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. + +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. + +## 2. Usage Steps + +### 2.1 Version Requirements + +- IoTDB: >=2.0.2-SNAPSHOT +- mybatisPlus: >=3.5.10 +- iotdb-jdbc:>=2.0.4-SNAPSHOT + +### 2.2 Operating Process + +#### 2.2.1 IoTDB Environment Setup + +1. Download, install, and start the IoTDB service. For details, refer to [QuickStart](../QuickStart/QuickStart.md) +2. Create the database database1 and tables table1 / table2. Relevant SQL statements can refer to [Sample-Data](../Reference/Sample-Data.md) + +#### 2.2.2 Create a Maven Project + +1. Create a Maven project +2. Add the following dependency configurations to the pom + +```XML + + 17 + 17 + UTF-8 + + + + + com.baomidou + mybatis-plus-spring-boot3-starter + 3.5.10 + + + com.baomidou + mybatis-plus-generator + 3.5.10 + + + com.github.jeffreyning + mybatisplus-plus + 1.7.5-RELEASE + + + org.apache.velocity + velocity-engine-core + 2.0 + + + org.apache.iotdb + iotdb-jdbc + 2.0.4-SNAPSHOT + + + org.springframework.boot + spring-boot-starter + 3.4.3 + + + org.slf4j + slf4j-api + + + commons-logging + commons-logging + + + + + org.springframework.boot + spring-boot-starter-web + 3.4.3 + + + org.slf4j + slf4j-api + + + commons-logging + commons-logging + + + + + io.springfox + springfox-swagger2 + 3.0.0 + + + io.springfox + springfox-swagger-ui + 3.0.0 + + + org.projectlombok + lombok + 1.18.36 + + + org.springframework.boot + spring-boot-starter-test + 3.4.3 + test + + + org.slf4j + slf4j-api + 2.0.13 + + + ch.qos.logback + logback-classic + 1.5.16 + + + ch.qos.logback + logback-core + 1.5.16 + + + + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.4.2 + + + org.apache.iotdb + mybatis-generator-plugin + 2.0.2-SNAPSHOT + + + + true + true + src/main/resources/generatorConfig.xml + + + + +``` +3. Create an execution file, modify the `url`/`username`/`password` of the target IoTDB, and the target file generation directory `outputDir`/`pathInfo` + +```Java +package org.apache.iotdb; + +import com.baomidou.mybatisplus.generator.FastAutoGenerator; +import com.baomidou.mybatisplus.generator.config.DataSourceConfig; +import com.baomidou.mybatisplus.generator.config.OutputFile; +import com.baomidou.mybatisplus.generator.config.rules.DateType; +import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; +import org.apache.iotdb.jdbc.IoTDBDataSource; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.sql.Types; +import java.util.Collections; + + +@SpringBootApplication +@MapperScan("org.apache.iotdb.mapper") +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + IoTDBDataSource dataSource = new IoTDBDataSource(); + dataSource.setUrl("jdbc:iotdb://127.0.0.1:6667/database1?sql_dialect=table"); + dataSource.setUser("root"); + dataSource.setPassword("root"); + FastAutoGenerator generator = FastAutoGenerator.create(new DataSourceConfig.Builder(dataSource).driverClassName("org.apache.iotdb.jdbc.IoTDBDriver")); + generator + .globalConfig(builder -> { + builder.author("IoTDB") + .enableSwagger() + .dateType(DateType.ONLY_DATE) + .outputDir("src/main/java"); + }) + .packageConfig(builder -> { + builder.parent("org.apache.iotdb") + .mapper("mapper") + .pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/java/org/apache/iotdb/xml")); + }) + .dataSourceConfig(builder -> { + builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> { + int typeCode = metaInfo.getJdbcType().TYPE_CODE; + switch (typeCode) { + case Types.FLOAT: + return DbColumnType.FLOAT; + default: + return typeRegistry.getColumnType(metaInfo); + } + }); + }) + .strategyConfig(builder -> { + builder.addInclude("table1"); + builder.entityBuilder() + .enableLombok() +// .addIgnoreColumns("create_time") + .enableFileOverride(); + builder.serviceBuilder() + .formatServiceFileName("%sService") + .formatServiceImplFileName("%sServiceImpl") + .convertServiceFileName((entityName -> entityName + "Service")) + .enableFileOverride(); + builder.controllerBuilder() + .enableRestStyle() + .enableFileOverride(); + }) + .strategyConfig(builder -> { + builder.addInclude("table2"); + builder.entityBuilder() + .enableLombok() +// .addIgnoreColumns("create_time") + .enableFileOverride(); + builder.serviceBuilder() + .formatServiceFileName("%sService") + .formatServiceImplFileName("%sServiceImpl") + .convertServiceFileName((entityName -> entityName + "Service")) + .enableFileOverride(); + builder.controllerBuilder() + .enableRestStyle() + .enableFileOverride(); + }) + .execute(); + } +} +``` + +#### 2.2.3 Generate Target Files + +1. Run Main.java +2. View the log output. The following indicates that the target files are generated + +```Java +16:10:08.943 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator -- ==========================File generation completed!!!========================== +``` + +#### 2.2.4 Generate Target Files + +```Plain +org/apache/iotdb/controller/Table1Controller.java +org/apache/iotdb/controller/Table2Controller.java +org/apache/iotdb/entity/Table1.java +org/apache/iotdb/mapper/Table2.xml +org/apache/iotdb/service/Table1Service.java +org/apache/iotdb/service/Table2Service.java +org/apache/iotdb/service/impl/Table1ServiceImpl.java +org/apache/iotdb/service/impl/Table2ServiceImpl.java +org/apache/iotdb/xml/Table1Mapper.xml +org/apache/iotdb/xml/Table2Mapper.xml +``` +![](/img/MyBatisPlus-Generator.png) + +#### 2.2.5 Modify Annotations + +Manually adjust the generated code `org/apache/iotdb/entity/Table1.java`, `org/apache/iotdb/entity/Table2.java` to support multi-primary key queries. + +```TypeScript +// Add new import +import com.github.jeffreyning.mybatisplus.anno.MppMultiId; + +// Add new annotation @MppMultiId +@MppMultiId +// Modify annotation @TableId() -->> @TableField() +@TableField("time") +private Date time; + +// Add new annotation @MppMultiId +@MppMultiId +// Modify annotation @TableId() -->> @TableField() +@TableField("region") +private String region; + +// Add new annotation @MppMultiId +@MppMultiId +// Modify annotation @TableId() -->> @TableField() +@TableField("plant_id") +private String plantId; + +// Add new annotation @MppMultiId +@MppMultiId +// Modify annotation @TableId() -->> @TableField() +@TableField("device_id") +private String deviceId; +``` + +## 3. Usage Example + +For a complete usage example, refer to the source code [examples/mybatisplus-generator](https://github.com/apache/iotdb-extras/tree/master/examples/mybatisplus-generator) + diff --git a/src/zh/UserGuide/Master/Table/Ecosystem-Integration/MyBatisPlus-Generator.md b/src/zh/UserGuide/Master/Table/Ecosystem-Integration/MyBatisPlus-Generator.md new file mode 100644 index 000000000..a80ea4306 --- /dev/null +++ b/src/zh/UserGuide/Master/Table/Ecosystem-Integration/MyBatisPlus-Generator.md @@ -0,0 +1,318 @@ + + +# MyBatisPlus Generator + +## 1. 概述 + +MyBatis-Plus Generator 是 MyBatis-Plus 框架内置的代码生成工具,基于数据库表结构自动生成标准化的实体类、Mapper 接口、Service 层及 Controller 层代码,集成 MyBatis-Plus 的通用 CRUD 方法(如 `BaseMapper` 内置增删改查)和条件构造器(`QueryWrapper`),支持 Lombok、Swagger 等扩展注解,通过简单配置即可快速构建符合企业级规范的持久层代码,大幅减少单表操作的重复开发工作,适用于快速搭建后台管理系统或标准化数据服务模块。 + +下文将介绍如何使用 Mybatis-Plus Generator 连接 IoTDB ,并生成数据库表对应的实体类、Mapper 接口、Service 层及 Controller 层代码文件。 + +## 2. 使用步骤 + +### 2.1 版本要求 + +- IoTDB: >=2.0.2-SNAPSHOT +- mybatisPlus: >=3.5.10 +- iotdb-jdbc:>=2.0.4-SNAPSHOT + +### 2.2 操作流程 + +#### 2.2.1 IoTDB 环境搭建 + +1. 下载、安装并启动 IoTDB 服务,具体可参考[快速上手](../QuickStart/QuickStart.md) +2. 创建数据库 database1 及表 table1 / table2,相关 SQL 语句可参考[示例数据](../Reference/Sample-Data.md) + +#### 2.2.2 创建 Maven项目 + +1. 创建 Maven 项目 +2. 在 pom 中增加如下依赖配置 + +```XML + + 17 + 17 + UTF-8 + + + + + com.baomidou + mybatis-plus-spring-boot3-starter + 3.5.10 + + + com.baomidou + mybatis-plus-generator + 3.5.10 + + + com.github.jeffreyning + mybatisplus-plus + 1.7.5-RELEASE + + + org.apache.velocity + velocity-engine-core + 2.0 + + + org.apache.iotdb + iotdb-jdbc + 2.0.4-SNAPSHOT + + + org.springframework.boot + spring-boot-starter + 3.4.3 + + + org.slf4j + slf4j-api + + + commons-logging + commons-logging + + + + + org.springframework.boot + spring-boot-starter-web + 3.4.3 + + + org.slf4j + slf4j-api + + + commons-logging + commons-logging + + + + + io.springfox + springfox-swagger2 + 3.0.0 + + + io.springfox + springfox-swagger-ui + 3.0.0 + + + org.projectlombok + lombok + 1.18.36 + + + org.springframework.boot + spring-boot-starter-test + 3.4.3 + test + + + org.slf4j + slf4j-api + 2.0.13 + + + ch.qos.logback + logback-classic + 1.5.16 + + + ch.qos.logback + logback-core + 1.5.16 + + + + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.4.2 + + + org.apache.iotdb + mybatis-generator-plugin + 2.0.2-SNAPSHOT + + + + true + true + src/main/resources/generatorConfig.xml + + + + +``` +3. 新建执行文件,修改目标 IoTDB 的 `url`/ `username` / `password`,和目标文件生成目录 `outputDir`/`pathInfo` + +```Java +package org.apache.iotdb; + +import com.baomidou.mybatisplus.generator.FastAutoGenerator; +import com.baomidou.mybatisplus.generator.config.DataSourceConfig; +import com.baomidou.mybatisplus.generator.config.OutputFile; +import com.baomidou.mybatisplus.generator.config.rules.DateType; +import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; +import org.apache.iotdb.jdbc.IoTDBDataSource; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.sql.Types; +import java.util.Collections; + + +@SpringBootApplication +@MapperScan("org.apache.iotdb.mapper") +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + IoTDBDataSource dataSource = new IoTDBDataSource(); + dataSource.setUrl("jdbc:iotdb://127.0.0.1:6667/database1?sql_dialect=table"); + dataSource.setUser("root"); + dataSource.setPassword("root"); + FastAutoGenerator generator = FastAutoGenerator.create(new DataSourceConfig.Builder(dataSource).driverClassName("org.apache.iotdb.jdbc.IoTDBDriver")); + generator + .globalConfig(builder -> { + builder.author("IoTDB") + .enableSwagger() + .dateType(DateType.ONLY_DATE) + .outputDir("src/main/java"); + }) + .packageConfig(builder -> { + builder.parent("org.apache.iotdb") + .mapper("mapper") + .pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/java/org/apache/iotdb/xml")); + }) + .dataSourceConfig(builder -> { + builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> { + int typeCode = metaInfo.getJdbcType().TYPE_CODE; + switch (typeCode) { + case Types.FLOAT: + return DbColumnType.FLOAT; + default: + return typeRegistry.getColumnType(metaInfo); + } + }); + }) + .strategyConfig(builder -> { + builder.addInclude("table1"); + builder.entityBuilder() + .enableLombok() +// .addIgnoreColumns("create_time") + .enableFileOverride(); + builder.serviceBuilder() + .formatServiceFileName("%sService") + .formatServiceImplFileName("%sServiceImpl") + .convertServiceFileName((entityName -> entityName + "Service")) + .enableFileOverride(); + builder.controllerBuilder() + .enableRestStyle() + .enableFileOverride(); + }) + .strategyConfig(builder -> { + builder.addInclude("table2"); + builder.entityBuilder() + .enableLombok() +// .addIgnoreColumns("create_time") + .enableFileOverride(); + builder.serviceBuilder() + .formatServiceFileName("%sService") + .formatServiceImplFileName("%sServiceImpl") + .convertServiceFileName((entityName -> entityName + "Service")) + .enableFileOverride(); + builder.controllerBuilder() + .enableRestStyle() + .enableFileOverride(); + }) + .execute(); + } +} +``` + +#### 2.2.3 生成目标文件 + +1. 运行 Main.java +2. 查看日志输出,如下所示即生成目标文件 + +```Java +16:10:08.943 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator -- ==========================文件生成完成!!!========================== +``` + +#### 2.2.4 查看目标文件 + +```Plain +org/apache/iotdb/controller/Table1Controller.java +org/apache/iotdb/controller/Table2Controller.java +org/apache/iotdb/entity/Table1.java +org/apache/iotdb/mapper/Table2.xml +org/apache/iotdb/service/Table1Service.java +org/apache/iotdb/service/Table2Service.java +org/apache/iotdb/service/impl/Table1ServiceImpl.java +org/apache/iotdb/service/impl/Table2ServiceImpl.java +org/apache/iotdb/xml/Table1Mapper.xml +org/apache/iotdb/xml/Table2Mapper.xml +``` +![](/img/MyBatisPlus-Generator.png) + +#### 2.2.5 修改注释 + +手动调整生成的代码`org/apache/iotdb/entity/Table1.java`,`org/apache/iotdb/entity/Table2.java`使其支持多主键查询。 + +```TypeScript +// 新增 import +import com.github.jeffreyning.mybatisplus.anno.MppMultiId; + +// 新增注解 @MppMultiId +@MppMultiId +// 修改注解 @TableId() -->> @TableField() +@TableField("time") +private Date time; + +// 新增注解 @MppMultiId +@MppMultiId +// 修改注解 @TableId() -->> @TableField() +@TableField("region") +private String region; + +// 新增注解 @MppMultiId +@MppMultiId +// 修改注解 @TableId() -->> @TableField() +@TableField("plant_id") +private String plantId; + +// 新增注解 @MppMultiId +@MppMultiId +// 修改注解 @TableId() -->> @TableField() +@TableField("device_id") +private String deviceId; +``` + +## 3. 使用示例 + +完整的使用示例可参考源码 [examples/mybatisplus-generator](https://github.com/apache/iotdb-extras/tree/master/examples/mybatisplus-generator) + diff --git a/src/zh/UserGuide/latest-Table/Ecosystem-Integration/MyBatisPlus-Generator.md b/src/zh/UserGuide/latest-Table/Ecosystem-Integration/MyBatisPlus-Generator.md new file mode 100644 index 000000000..a80ea4306 --- /dev/null +++ b/src/zh/UserGuide/latest-Table/Ecosystem-Integration/MyBatisPlus-Generator.md @@ -0,0 +1,318 @@ + + +# MyBatisPlus Generator + +## 1. 概述 + +MyBatis-Plus Generator 是 MyBatis-Plus 框架内置的代码生成工具,基于数据库表结构自动生成标准化的实体类、Mapper 接口、Service 层及 Controller 层代码,集成 MyBatis-Plus 的通用 CRUD 方法(如 `BaseMapper` 内置增删改查)和条件构造器(`QueryWrapper`),支持 Lombok、Swagger 等扩展注解,通过简单配置即可快速构建符合企业级规范的持久层代码,大幅减少单表操作的重复开发工作,适用于快速搭建后台管理系统或标准化数据服务模块。 + +下文将介绍如何使用 Mybatis-Plus Generator 连接 IoTDB ,并生成数据库表对应的实体类、Mapper 接口、Service 层及 Controller 层代码文件。 + +## 2. 使用步骤 + +### 2.1 版本要求 + +- IoTDB: >=2.0.2-SNAPSHOT +- mybatisPlus: >=3.5.10 +- iotdb-jdbc:>=2.0.4-SNAPSHOT + +### 2.2 操作流程 + +#### 2.2.1 IoTDB 环境搭建 + +1. 下载、安装并启动 IoTDB 服务,具体可参考[快速上手](../QuickStart/QuickStart.md) +2. 创建数据库 database1 及表 table1 / table2,相关 SQL 语句可参考[示例数据](../Reference/Sample-Data.md) + +#### 2.2.2 创建 Maven项目 + +1. 创建 Maven 项目 +2. 在 pom 中增加如下依赖配置 + +```XML + + 17 + 17 + UTF-8 + + + + + com.baomidou + mybatis-plus-spring-boot3-starter + 3.5.10 + + + com.baomidou + mybatis-plus-generator + 3.5.10 + + + com.github.jeffreyning + mybatisplus-plus + 1.7.5-RELEASE + + + org.apache.velocity + velocity-engine-core + 2.0 + + + org.apache.iotdb + iotdb-jdbc + 2.0.4-SNAPSHOT + + + org.springframework.boot + spring-boot-starter + 3.4.3 + + + org.slf4j + slf4j-api + + + commons-logging + commons-logging + + + + + org.springframework.boot + spring-boot-starter-web + 3.4.3 + + + org.slf4j + slf4j-api + + + commons-logging + commons-logging + + + + + io.springfox + springfox-swagger2 + 3.0.0 + + + io.springfox + springfox-swagger-ui + 3.0.0 + + + org.projectlombok + lombok + 1.18.36 + + + org.springframework.boot + spring-boot-starter-test + 3.4.3 + test + + + org.slf4j + slf4j-api + 2.0.13 + + + ch.qos.logback + logback-classic + 1.5.16 + + + ch.qos.logback + logback-core + 1.5.16 + + + + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.4.2 + + + org.apache.iotdb + mybatis-generator-plugin + 2.0.2-SNAPSHOT + + + + true + true + src/main/resources/generatorConfig.xml + + + + +``` +3. 新建执行文件,修改目标 IoTDB 的 `url`/ `username` / `password`,和目标文件生成目录 `outputDir`/`pathInfo` + +```Java +package org.apache.iotdb; + +import com.baomidou.mybatisplus.generator.FastAutoGenerator; +import com.baomidou.mybatisplus.generator.config.DataSourceConfig; +import com.baomidou.mybatisplus.generator.config.OutputFile; +import com.baomidou.mybatisplus.generator.config.rules.DateType; +import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; +import org.apache.iotdb.jdbc.IoTDBDataSource; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.sql.Types; +import java.util.Collections; + + +@SpringBootApplication +@MapperScan("org.apache.iotdb.mapper") +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + IoTDBDataSource dataSource = new IoTDBDataSource(); + dataSource.setUrl("jdbc:iotdb://127.0.0.1:6667/database1?sql_dialect=table"); + dataSource.setUser("root"); + dataSource.setPassword("root"); + FastAutoGenerator generator = FastAutoGenerator.create(new DataSourceConfig.Builder(dataSource).driverClassName("org.apache.iotdb.jdbc.IoTDBDriver")); + generator + .globalConfig(builder -> { + builder.author("IoTDB") + .enableSwagger() + .dateType(DateType.ONLY_DATE) + .outputDir("src/main/java"); + }) + .packageConfig(builder -> { + builder.parent("org.apache.iotdb") + .mapper("mapper") + .pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/java/org/apache/iotdb/xml")); + }) + .dataSourceConfig(builder -> { + builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> { + int typeCode = metaInfo.getJdbcType().TYPE_CODE; + switch (typeCode) { + case Types.FLOAT: + return DbColumnType.FLOAT; + default: + return typeRegistry.getColumnType(metaInfo); + } + }); + }) + .strategyConfig(builder -> { + builder.addInclude("table1"); + builder.entityBuilder() + .enableLombok() +// .addIgnoreColumns("create_time") + .enableFileOverride(); + builder.serviceBuilder() + .formatServiceFileName("%sService") + .formatServiceImplFileName("%sServiceImpl") + .convertServiceFileName((entityName -> entityName + "Service")) + .enableFileOverride(); + builder.controllerBuilder() + .enableRestStyle() + .enableFileOverride(); + }) + .strategyConfig(builder -> { + builder.addInclude("table2"); + builder.entityBuilder() + .enableLombok() +// .addIgnoreColumns("create_time") + .enableFileOverride(); + builder.serviceBuilder() + .formatServiceFileName("%sService") + .formatServiceImplFileName("%sServiceImpl") + .convertServiceFileName((entityName -> entityName + "Service")) + .enableFileOverride(); + builder.controllerBuilder() + .enableRestStyle() + .enableFileOverride(); + }) + .execute(); + } +} +``` + +#### 2.2.3 生成目标文件 + +1. 运行 Main.java +2. 查看日志输出,如下所示即生成目标文件 + +```Java +16:10:08.943 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator -- ==========================文件生成完成!!!========================== +``` + +#### 2.2.4 查看目标文件 + +```Plain +org/apache/iotdb/controller/Table1Controller.java +org/apache/iotdb/controller/Table2Controller.java +org/apache/iotdb/entity/Table1.java +org/apache/iotdb/mapper/Table2.xml +org/apache/iotdb/service/Table1Service.java +org/apache/iotdb/service/Table2Service.java +org/apache/iotdb/service/impl/Table1ServiceImpl.java +org/apache/iotdb/service/impl/Table2ServiceImpl.java +org/apache/iotdb/xml/Table1Mapper.xml +org/apache/iotdb/xml/Table2Mapper.xml +``` +![](/img/MyBatisPlus-Generator.png) + +#### 2.2.5 修改注释 + +手动调整生成的代码`org/apache/iotdb/entity/Table1.java`,`org/apache/iotdb/entity/Table2.java`使其支持多主键查询。 + +```TypeScript +// 新增 import +import com.github.jeffreyning.mybatisplus.anno.MppMultiId; + +// 新增注解 @MppMultiId +@MppMultiId +// 修改注解 @TableId() -->> @TableField() +@TableField("time") +private Date time; + +// 新增注解 @MppMultiId +@MppMultiId +// 修改注解 @TableId() -->> @TableField() +@TableField("region") +private String region; + +// 新增注解 @MppMultiId +@MppMultiId +// 修改注解 @TableId() -->> @TableField() +@TableField("plant_id") +private String plantId; + +// 新增注解 @MppMultiId +@MppMultiId +// 修改注解 @TableId() -->> @TableField() +@TableField("device_id") +private String deviceId; +``` + +## 3. 使用示例 + +完整的使用示例可参考源码 [examples/mybatisplus-generator](https://github.com/apache/iotdb-extras/tree/master/examples/mybatisplus-generator) +