Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
<build>
<plugins>
<plugin>
Expand All @@ -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

Expand All @@ -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
```
47 changes: 47 additions & 0 deletions examples/mybatis-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,44 @@
<artifactId>mybatis-generator-example</artifactId>
<name>IoTDB: Example: Mybatis Generator</name>
<version>2.0.2-SNAPHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.19</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.38</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.iotdb</groupId>
<artifactId>iotdb-jdbc</artifactId>
<version>2.0.4-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations-jakarta</artifactId>
<version>2.2.30</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/plugin/xml/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
Expand All @@ -49,6 +86,16 @@
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<usedDependencies>
<!-- These are used at runtime in tests -->
<usedDependency>org.apache.iotdb:iotdb-jdbc</usedDependency>
</usedDependencies>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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();

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'Date now' is never read.

// 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<Mix> 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<Mix> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Mix> selectAll();

int batchInsert(@Param("records") List<Mix> records);
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--

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.

-->
<mapper namespace="org.apache.iotdb.mybatis.plugin.mapper.MixMapper">
<resultMap id="BaseResultMap" type="org.apache.iotdb.mybatis.plugin.model.Mix">
<id column="time" jdbcType="TIMESTAMP" property="time"/>
<id column="device_id" jdbcType="VARCHAR" property="deviceId"/>
<result column="region" jdbcType="VARCHAR" property="region"/>
<result column="plant_id" jdbcType="VARCHAR" property="plantId"/>
<result column="model_id" jdbcType="VARCHAR" property="modelId"/>
<result column="maintenance" jdbcType="VARCHAR" property="maintenance"/>
<result column="temperature" jdbcType="FLOAT" property="temperature"/>
<result column="humidity" jdbcType="FLOAT" property="humidity"/>
<result column="status" jdbcType="BOOLEAN" property="status"/>
<result column="arrival_time" jdbcType="TIMESTAMP" property="arrivalTime"/>
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="map">delete from test.mix where time = #{time,jdbcType=TIMESTAMP} and device_id = #{deviceId,jdbcType=VARCHAR}</delete>
<insert id="insert" parameterType="org.apache.iotdb.mybatis.plugin.model.Mix">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>
<select id="selectByPrimaryKey" parameterType="map" resultMap="BaseResultMap">select time, device_id, region, plant_id, model_id, maintenance, temperature, humidity, status, arrival_time from test.mix where time = #{time,jdbcType=TIMESTAMP} and device_id = #{deviceId,jdbcType=VARCHAR}</select>
<select id="selectAll" resultMap="BaseResultMap">select time, device_id, region, plant_id, model_id, maintenance, temperature, humidity, status, arrival_time from test.mix</select>
<insert id="batchInsert" parameterType="java.util.List">insert into test.mix ( time, device_id, region, plant_id, model_id, maintenance, temperature, humidity, status, arrival_time ) values
<foreach collection="records" index="index" item="item" separator=",">( #{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} )</foreach>
</insert>
</mapper>
Loading