-
Notifications
You must be signed in to change notification settings - Fork 18
add a example for mybatis, format markdown #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a903058
add a example for mybatis, format markdown
xiaoyekanren 8623a5c
add default param (#77)
2b3c511 7f2069b
add the code generated by mybatis
xiaoyekanren b583a77
add a example for mybatis, format markdown
xiaoyekanren b9a1527
add the code generated by mybatis
xiaoyekanren 7cb7412
fix conflict
xiaoyekanren 140d2ab
fix compile
CritasWang fa7c565
Update README.md
CritasWang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/Main.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 noticeCode 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); | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
...les/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/mapper/MixMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
59 changes: 59 additions & 0 deletions
59
examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/model/Mix.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
40 changes: 40 additions & 0 deletions
40
examples/mybatis-generator/src/main/java/org/apache/iotdb/mybatis/plugin/xml/MixMapper.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.