diff --git a/examples/mybatis-generator/readme.md b/examples/mybatis-generator/README.md similarity index 76% rename from examples/mybatis-generator/readme.md rename to examples/mybatis-generator/README.md index 7cbdab8a..234f0a6b 100644 --- a/examples/mybatis-generator/readme.md +++ b/examples/mybatis-generator/README.md @@ -45,7 +45,7 @@ use test; ``` Then we need to create a database 'table' ``` -CREATE TABLE mix3 ( +CREATE TABLE mix ( time TIMESTAMP TIME, region STRING TAG, plant_id STRING TAG, @@ -56,12 +56,12 @@ CREATE TABLE mix3 ( humidity FLOAT FIELD, status Boolean FIELD, arrival_time TIMESTAMP FIELD -) WITH (TTL=31536000000); +); ``` ### 3. Build Dependencies with Maven in your Project -``` +```xml @@ -87,11 +87,19 @@ CREATE TABLE mix3 ( ### 5. put The generatorConfig.xml in your project -The location of the ` configurationFile ` configuration ` generatorConfig.xml ` file can be found in the ` src/main/resources ` template of this project for reference` Copy its content and place it in the corresponding location +- `src/main/resources/generatorConfig.xml` + +each table generates an entity object + +- `src/main/resources/generatorConfigByExample.xml` -### 6. exec 'mvn mybatis-generator:generate' +The generated object will contain many "by Example" methods. If you do not want to generate these, you can configure to cancel them in the subsequent table elements -Execute the command at the location of the 'pom' in the project:` Mvn mybatis generator: generate generates corresponding Java classes and mapper files +### 6. generate generates corresponding Java classes and mapper files + +exec `mvn mybatis-generator:generate` + +Execute the command at the location of the 'pom' in the project: Mvn mybatis generator: generate generates corresponding Java classes and mapper files ### 7、the target file location @@ -102,3 +110,11 @@ org/apache/iotdb/mybatis/plugin/model/Mix.java org/apache/iotdb/mybatis/plugin/mapper/MixMapper.java org/apache/iotdb/mybatis/plugin/xml/MixMapper.xml ``` + +if you are using the 'src/main/resources/generatorConfiguraByExample. xml' file`, You can see the target file in your Project +``` +org/apache/iotdb/mybatis/plugin/model/Mix.java +org/apache/iotdb/mybatis/plugin/model/MixExample.java +org/apache/iotdb/mybatis/plugin/mapper/MixMapper.java +org/apache/iotdb/mybatis/plugin/xml/MixMapper.xml +``` diff --git a/examples/mybatis-generator/pom.xml b/examples/mybatis-generator/pom.xml index c7d73e3f..39c714d6 100644 --- a/examples/mybatis-generator/pom.xml +++ b/examples/mybatis-generator/pom.xml @@ -30,7 +30,44 @@ mybatis-generator-example IoTDB: Example: Mybatis Generator 2.0.2-SNAPHOT + + + org.mybatis + mybatis + 3.5.19 + + + org.projectlombok + lombok + 1.18.38 + provided + + + org.apache.iotdb + iotdb-jdbc + 2.0.4-SNAPSHOT + + + io.swagger.core.v3 + swagger-annotations-jakarta + 2.2.30 + + + + + src/main/resources + + **/*.xml + + + + src/main/java + + **/plugin/xml/*.xml + + + org.mybatis.generator @@ -49,6 +86,16 @@ src/main/resources/generatorConfig.xml + + org.apache.maven.plugins + maven-dependency-plugin + + + + org.apache.iotdb:iotdb-jdbc + + + diff --git a/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/Main.java b/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/Main.java new file mode 100644 index 00000000..5fb32d64 --- /dev/null +++ b/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/Main.java @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.mybatis; + +import org.apache.iotdb.mybatis.plugin.mapper.MixMapper; +import org.apache.iotdb.mybatis.plugin.model.Mix; + +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class Main { + public static void main(String[] args) throws IOException { + String resource = "mybatis-config.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + + try (SqlSession session = sqlSessionFactory.openSession(true)) { + MixMapper mapper = session.getMapper(MixMapper.class); + + Date now = new Date(); + + // 1. insertOne + System.out.println("1. insertOne..."); + insertOne(mapper, new Date(1)); + insertOne(mapper, new Date(2)); + insertOne(mapper, new Date(3)); + System.out.println("------------------\n"); + + // 2. selectAll + System.out.println("2. selectAll..."); + selectAll(mapper); + System.out.println("------------------\n"); + + // 3. selectByPrimaryKey + System.out.println("3. selectByPrimaryKey..."); + selectByPrimaryKey(mapper, new Date(1), "dev001"); + System.out.println("------------------\n"); + + // 4. deleteByPrimaryKey + System.out.println("4. deleteByPrimaryKey..."); + deleteByPrimaryKey(mapper, new Date(1), "dev001"); + deleteByPrimaryKey(mapper, new Date(2), "dev001"); + System.out.println("selectAll..."); + selectAll(mapper); + System.out.println("------------------\n"); + + // 5. batchInsert + System.out.println("5. batchInsert..."); + batchInsert(mapper, new Date(6)); + System.out.println("selectAll..."); + selectAll(mapper); + System.out.println("------------------\n"); + } + } + + private static void insertOne(MixMapper mapper, Date now) { + Mix mix = new Mix(); + mix.setTime(now); + mix.setDeviceId("dev001"); + mix.setRegion("华东"); + mix.setPlantId("plantA"); + mix.setModelId("modelX"); + mix.setMaintenance("正常"); + mix.setTemperature(25.5); + mix.setHumidity(60.0); + mix.setStatus(true); + mix.setArrivalTime(now); + mapper.insert(mix); + } + + private static void selectAll(MixMapper mapper) { + List all = mapper.selectAll(); + all.forEach(System.out::println); + } + + private static void selectByPrimaryKey(MixMapper mapper, Date time, String deviceId) { + Mix one = mapper.selectByPrimaryKey(time, deviceId); + System.out.println("results: " + one); + } + + private static void deleteByPrimaryKey(MixMapper mapper, Date time, String deviceId) { + mapper.deleteByPrimaryKey(time, deviceId); + } + + private static void batchInsert(MixMapper mapper, Date now) { + List batchList = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + Mix m = new Mix(); + m.setTime(new Date(now.getTime() + i * 1000)); + m.setDeviceId("dev00" + (i + 2)); + m.setRegion("华东"); + m.setPlantId("plantA"); + m.setModelId("modelX"); + m.setMaintenance("正常"); + m.setTemperature(20.0 + i); + m.setHumidity(50.0 + i); + m.setStatus(i % 2 == 0); + m.setArrivalTime(new Date(now.getTime() + i * 1000)); + batchList.add(m); + } + mapper.batchInsert(batchList); + } +} diff --git a/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/mapper/MixMapper.java b/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/mapper/MixMapper.java new file mode 100644 index 00000000..fb54c92d --- /dev/null +++ b/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/mapper/MixMapper.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.mybatis.plugin.mapper; + +import org.apache.iotdb.mybatis.plugin.model.Mix; + +import org.apache.ibatis.annotations.Param; + +import java.util.Date; +import java.util.List; + +public interface MixMapper { + int deleteByPrimaryKey(@Param("time") Date time, @Param("deviceId") String deviceId); + + int insert(Mix row); + + Mix selectByPrimaryKey(@Param("time") Date time, @Param("deviceId") String deviceId); + + List selectAll(); + + int batchInsert(@Param("records") List records); +} diff --git a/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/model/Mix.java b/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/model/Mix.java new file mode 100644 index 00000000..d32e3e45 --- /dev/null +++ b/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/model/Mix.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.mybatis.plugin.model; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +/** + * table: test.mix of model class + * + * @author IoTDB + * @date 2025-06-04 16:07:49 + */ +@Schema(title = "test.mix", description = "") +@Data +public class Mix implements Serializable { + /** class serial version id */ + private static final long serialVersionUID = 1L; + + private Date time; + + private String deviceId; + + private String region; + + private String plantId; + + private String modelId; + + private String maintenance; + + private Double temperature; + + private Double humidity; + + private Boolean status; + + private Date arrivalTime; +} diff --git a/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/xml/MixMapper.xml b/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/xml/MixMapper.xml new file mode 100644 index 00000000..ea23c977 --- /dev/null +++ b/examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/xml/MixMapper.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + delete from test.mix where time = #{time,jdbcType=TIMESTAMP} and device_id = #{deviceId,jdbcType=VARCHAR} + insert into test.mix (time, device_id, region, plant_id, model_id, maintenance, temperature, humidity, status, arrival_time) values (#{time,jdbcType=TIMESTAMP}, #{deviceId,jdbcType=VARCHAR}, #{region,jdbcType=VARCHAR}, #{plantId,jdbcType=VARCHAR}, #{modelId,jdbcType=VARCHAR}, #{maintenance,jdbcType=VARCHAR}, #{temperature,jdbcType=FLOAT}, #{humidity,jdbcType=FLOAT}, #{status,jdbcType=BOOLEAN}, #{arrivalTime,jdbcType=TIMESTAMP}) + + + insert into test.mix ( time, device_id, region, plant_id, model_id, maintenance, temperature, humidity, status, arrival_time ) values + ( #{item.time,jdbcType=TIMESTAMP}, #{item.deviceId,jdbcType=VARCHAR}, #{item.region,jdbcType=VARCHAR}, #{item.plantId,jdbcType=VARCHAR}, #{item.modelId,jdbcType=VARCHAR}, #{item.maintenance,jdbcType=VARCHAR}, #{item.temperature,jdbcType=FLOAT}, #{item.humidity,jdbcType=FLOAT}, #{item.status,jdbcType=BOOLEAN}, #{item.arrivalTime,jdbcType=TIMESTAMP} ) + + diff --git a/examples/mybatis-generator/src/main/resources/generatorConfig.xml b/examples/mybatis-generator/src/main/resources/generatorConfig.xml index 530ff4ff..6ad1c100 100644 --- a/examples/mybatis-generator/src/main/resources/generatorConfig.xml +++ b/examples/mybatis-generator/src/main/resources/generatorConfig.xml @@ -18,7 +18,7 @@ --> - + @@ -51,7 +51,7 @@ - +
diff --git a/examples/mybatis-generator/src/main/resources/generatorConfigByExample.xml b/examples/mybatis-generator/src/main/resources/generatorConfigByExample.xml new file mode 100644 index 00000000..63b45598 --- /dev/null +++ b/examples/mybatis-generator/src/main/resources/generatorConfigByExample.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
diff --git a/examples/mybatis-generator/src/main/resources/mybatis-config.xml b/examples/mybatis-generator/src/main/resources/mybatis-config.xml new file mode 100644 index 00000000..06dfbe99 --- /dev/null +++ b/examples/mybatis-generator/src/main/resources/mybatis-config.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + diff --git a/mybatis-generator/README-zh.md b/mybatis-generator/README-zh.md index 94eb875d..b8b2b3f4 100644 --- a/mybatis-generator/README-zh.md +++ b/mybatis-generator/README-zh.md @@ -24,28 +24,28 @@ * 在要生成的项目的 `pom` 文件中添加如下配置: -```java - - - - 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 - - - - +```xml + + + + 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 + + + + ``` * `configurationFile` 配置 `generatorConfig.xml` 文件的位置,其内容在本项目的 `src/main/resources` 有一个模板供参考,`copy` 其内容放到相应的位置 diff --git a/mybatis-generator/README.md b/mybatis-generator/README.md index fbd3eb42..efc9df55 100644 --- a/mybatis-generator/README.md +++ b/mybatis-generator/README.md @@ -26,28 +26,28 @@ * Add the following configuration to the 'pom' file of the project to be generated: -```java - - - - 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 - - - - +```xml + + + + 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 + + + + ``` * The location of the ` configurationFile ` configuration ` generatorConfig. xml ` file can be found in the ` src/main/resources ` template of this project for reference` Copy its content and place it in the corresponding location