|
| 1 | +<!-- |
| 2 | +
|
| 3 | + Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + or more contributor license agreements. See the NOTICE file |
| 5 | + distributed with this work for additional information |
| 6 | + regarding copyright ownership. The ASF licenses this file |
| 7 | + to you under the Apache License, Version 2.0 (the |
| 8 | + "License"); you may not use this file except in compliance |
| 9 | + with the License. You may obtain a copy of the License at |
| 10 | + |
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + |
| 13 | + Unless required by applicable law or agreed to in writing, |
| 14 | + software distributed under the License is distributed on an |
| 15 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + KIND, either express or implied. See the License for the |
| 17 | + specific language governing permissions and limitations |
| 18 | + under the License. |
| 19 | +
|
| 20 | +--> |
| 21 | + |
| 22 | +# Mybatis Generator |
| 23 | + |
| 24 | + |
| 25 | +## 1. Overview |
| 26 | + |
| 27 | +MyBatis Generator is an automated code generation tool officially released by MyBatis for the persistence layer. By parsing database table structures, it automatically generates entity classes (POJOs), Mapper interfaces, and XML mapping files. This enables rapid construction of standardized CRUD operation logic and significantly reduces the development cost of foundational code. |
| 28 | + |
| 29 | +To simplify integration in time-series database scenarios, the IoTDB team has extended this tool to launch **IoTDB MyBatis Generator**. Deeply adapted for IoTDB's time-series data model characteristics, it automatically generates entity classes, Mapper interfaces, and corresponding Mapper XML files that conform to time-series storage semantics. This provides a standardized and reusable engineering practice solution for efficient IoTDB integration. |
| 30 | + |
| 31 | +## 2. Usage Steps |
| 32 | + |
| 33 | +### 2.1 Version Requirements |
| 34 | + |
| 35 | +* `IoTDB`: >=2.0.2 |
| 36 | + |
| 37 | +* `mybatis-generator-plugin`: >=2.0.3 |
| 38 | + |
| 39 | +* `iotdb-jdbc`: >=2.0.3 |
| 40 | + |
| 41 | +### 2.2 Operating Procedure |
| 42 | + |
| 43 | +1. Install and start IoTDB, Refer to [IoTDB Quick Start](../QuickStart/QuickStart.md) |
| 44 | + |
| 45 | +2. Obtain the plugin |
| 46 | + |
| 47 | + * Method 1*: Directly reference the Maven dependency |
| 48 | + |
| 49 | + * Method 2: Clone the plugin source code and install locally: |
| 50 | + |
| 51 | +```Bash |
| 52 | +git clone https://github.com/apache/iotdb-extras.git |
| 53 | +cd .\mybatis-generator\ |
| 54 | +mvn clean install |
| 55 | +``` |
| 56 | + |
| 57 | +3. Add dependency |
| 58 | + |
| 59 | + Add the following configuration to your project's `pom.xml`: |
| 60 | + |
| 61 | +```xml |
| 62 | +<properties> |
| 63 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 64 | +</properties> |
| 65 | + |
| 66 | +<build> |
| 67 | + <plugins> |
| 68 | + <plugin> |
| 69 | + <groupId>org.mybatis.generator</groupId> |
| 70 | + <artifactId>mybatis-generator-maven-plugin</artifactId> |
| 71 | + <version>1.4.2</version> |
| 72 | + <dependencies> |
| 73 | + <dependency> |
| 74 | + <groupId>org.apache.iotdb</groupId> |
| 75 | + <artifactId>mybatis-generator-plugin</artifactId> |
| 76 | + <version>{lastest_version}</version> |
| 77 | + <!--Example: <version>2.0.3</version> --> |
| 78 | + </dependency> |
| 79 | + </dependencies> |
| 80 | + <configuration> |
| 81 | + <verbose>true</verbose> |
| 82 | + <overwrite>true</overwrite> |
| 83 | + <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> |
| 84 | + </configuration> |
| 85 | + </plugin> |
| 86 | + </plugins> |
| 87 | +</build> |
| 88 | +``` |
| 89 | + |
| 90 | +4. Configure generatorConfig.xml |
| 91 | + |
| 92 | +* Place the following configuration in `src/main/resources/generatorConfig.xml`: |
| 93 | + |
| 94 | +* Key customizable elements: `jdbcConnection`, `javaModelGenerator`, `sqlMapGenerator`, `javaClientGenerator`, `table` |
| 95 | + |
| 96 | +```xml |
| 97 | +<?xml version="1.0" encoding="UTF-8"?> |
| 98 | +<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> |
| 99 | +<generatorConfiguration> |
| 100 | + <classPathEntry location="/apache/iotdb/iotdb-client/jdbc/target/iotdb-jdbc-2.0.4-SNAPSHOT-jar-with-dependencies.jar"/> |
| 101 | + <!-- mvn mybatis-generator:generate hierarchical/flat--> |
| 102 | + <context id="myBatis3Simple" targetRuntime="MyBatis3Simple" defaultModelType="flat"> |
| 103 | + <plugin type="org.apache.iotdb.mybatis.plugin.LombokPlugin"/> |
| 104 | + <plugin type="org.apache.iotdb.mybatis.plugin.BatchInsertPlugin"/> |
| 105 | + <plugin type="org.apache.iotdb.mybatis.plugin.SerializablePlugin"/> |
| 106 | + <plugin type="org.mybatis.generator.plugins.VirtualPrimaryKeyPlugin"/> |
| 107 | + <commentGenerator type="org.apache.iotdb.mybatis.plugin.generator.SwaggerCommentGenerator"> |
| 108 | + <property name="suppressAllComments" value="true"/> |
| 109 | + <property name="suppressDate" value="true"/> |
| 110 | + <property name="addRemarkComments" value="true"/> |
| 111 | + </commentGenerator> |
| 112 | + <jdbcConnection driverClass="org.apache.iotdb.jdbc.IoTDBDriver" connectionURL="jdbc:iotdb://127.0.0.1:6667/database1?sql_dialect=table" userId="root" password="root"/> |
| 113 | + <javaTypeResolver type="org.apache.iotdb.mybatis.plugin.generator.resolver.IoTDBJavaTypeResolver"> |
| 114 | + <property name="forceBigDecimals" value="false"/> |
| 115 | + </javaTypeResolver> |
| 116 | + <javaModelGenerator targetPackage="org.apache.iotdb.mybatis.plugin.model" targetProject="src/main/java"> |
| 117 | + <property name="trimStrings" value="true"/> |
| 118 | + </javaModelGenerator> |
| 119 | + <sqlMapGenerator targetPackage="org.apache.iotdb.mybatis.plugin.xml" targetProject="src/main/java"> |
| 120 | + </sqlMapGenerator> |
| 121 | + <javaClientGenerator type="XMLMAPPER" targetPackage="org.apache.iotdb.mybatis.plugin.mapper" targetProject="src/main/java"> |
| 122 | + </javaClientGenerator> |
| 123 | + <table schema="database1" tableName="table1" domainObjectName="table1" enableSelectByPrimaryKey="true" enableInsert="true" enableDeleteByPrimaryKey="true"> |
| 124 | + <property name="virtualKeyColumns" value="time,region,plant_id,device_id,model_id,maintenance,temperature,humidity,status,arrival_time"/> |
| 125 | + </table> |
| 126 | + </context> |
| 127 | +</generatorConfiguration> |
| 128 | +``` |
| 129 | + |
| 130 | +5. Execute code generation |
| 131 | + |
| 132 | +Run in terminal: |
| 133 | + |
| 134 | +```Shell |
| 135 | +mvn mybatis-generator:generate |
| 136 | +``` |
| 137 | + |
| 138 | +6. View generated code |
| 139 | + |
| 140 | +Find the generated files in your project: |
| 141 | + |
| 142 | +```Shell |
| 143 | +# You can see the target file in your Project |
| 144 | +org/apache/iotdb/mybatis/plugin/model/table1.java |
| 145 | +org/apache/iotdb/mybatis/plugin/mapper/table1Mapper.java |
| 146 | +org/apache/iotdb/mybatis/plugin/xml/table1Mapper.xml |
| 147 | +``` |
| 148 | + |
| 149 | +## 3. Usage Example |
| 150 | + |
| 151 | +For detailed usage examples, refer to the source code: [examples/mybatis-generator](https://github.com/apache/iotdb-extras/tree/master/examples/mybatis-generator) |
0 commit comments