IoTDB数据库整合MyBatis实现SpringBoot项目CRUD

server/2024/9/26 3:19:30/

遇到的问题:

1.启动项目提示:testWhileIdle is true, validationQuery not set。
2023-04-26 14:05:39.282 ERROR 13864 --- [           main] com.alibaba.druid.pool.DruidDataSource   : testWhileIdle is true, validationQuery not set

原因:没有在配置文件中设置(最先的作者可能没遇到这个,不过报错也能用,可以不管)

druid:
validation-query: 连接测试语句

2.查询一直报错

  2.1错误一:mismatched input ‘<EOF>,’ excepting ‘{FROM,INTO}‘

反复检查:是因为validation-query:的连接测试语句不能写成网络上大家正对mysql设置的select

2.2错误二:java.lang.NullPointerException

原因:返回结果集的时候类型不能是jdbcType,改成JavaType问题解决,但是…

  2.3错误三:Error attempting to get column “Time” from result set

原因:不管你需不需要Time字段(需要也不能在select后面写),查询结果都会返回Time字段的值,这是iotdb特有的主键,但是本人试了LONG,VARCHAR,DATE等能想到的类型,都无济于事,但是可以通过Java.lang.String接收到返回的Long类型时间值,通过resultMapper就是无法实现。

最终解决:通过Map<String,Object>类型将所有数据接收出来,然后再处理(目前我只能这样操作,希望有更好办法的兄弟交流学习)

 3.删除操作,提示成功,但是去库里查询,还在!!!

我这就无语了,没删除就没删除呗,有错你就提呗,你来个成功,然后结果未实现。

原因:删除的时候,比如要删除某个时间段的一组数据,必须在设备id后面添加.*,如:

delete from root.XXX.XXX.demo20130424_4.* where time > XXXXXX

最后总算成功通过mybatis实现了自己想要的功能,不过还是有很多不足,还希望有兴趣的同学一起研究。

接下来上代码:

1.主要依赖:

<dependency><groupId>org.apache.iotdb</groupId><artifactId>iotdb-jdbc</artifactId><version>1.3.0</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.16</version></dependency>

2.数据库配置

spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:driver-class-name: org.apache.iotdb.jdbc.IoTDBDriverurl: jdbc:iotdb://127.0.0.1:6667/username: rootpassword: rootinitial-size: 5max-active: 20min-idle: 1max-wait: 60000remove-abandoned: trueremove-abandoned-timeout: 30time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 300000test-while-idle: falsetest-on-borrow: falsetest-on-return: falsemybatis-plus:mapper-locations: classpath:/mapper/*.xmlconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

WeatherEntity

package com.example.wys.entity;import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;/*** 添加说明** @author kit* @version 1.0* @date 2023/11/7 17:02*/
@Data
@Builder
public class WeatherEntity {/*** 固定对应Time字段*/private long timestamp;/*** 采样时间时间戳*/private String samplingTime;/*** 采样时间字符*/private String samplingTimeStr;/*** 城市编码*/private Integer cityKey;/*** 城市*/private String city;/*** 温度 ℃*/private float temperature;/*** 湿度 %*/private float humidity;/*** pm10*/private float pm10;/*** pm10*/private float pm25;/*** 空气质量*/private String quality;/*** 天气描述*/private String remark;}

Rst

package com.example.wys.utils;import java.io.Serializable;
import java.util.LinkedHashMap;/*** 返回结果** @author kit* @version 1.0* @date 2023/11/9 11:23*/
public class Rst extends LinkedHashMap<String, Object> implements Serializable {private static final long serialVersionUID = 1L;    // 序列化版本号public static final int CODE_SUCCESS = 200;public static final int CODE_ERROR = 500;public Rst() {}public Rst(int code, String msg, Object data) {this.setCode(code);this.setMsg(msg);this.setData(data);}/*** 获取code** @return code*/public Integer getCode() {return (Integer) this.get("code");}/*** 获取msg** @return msg*/public String getMsg() {return (String) this.get("msg");}/*** 获取data** @return data*/public Object getData() {return (Object) this.get("data");}/*** 给code赋值,连缀风格** @param code code* @return 对象自身*/public Rst setCode(int code) {this.put("code", code);return this;}/*** 给msg赋值,连缀风格** @param msg msg* @return 对象自身*/public Rst setMsg(String msg) {this.put("msg", msg);return this;}/*** 给data赋值,连缀风格** @param data data* @return 对象自身*/public Rst setData(Object data) {this.put("data", data);return this;}// 构建成功public static Rst ok() {return new Rst(CODE_SUCCESS, "ok", null);}public static Rst ok(String msg) {return new Rst(CODE_SUCCESS, msg, null);}public static Rst code(int code) {return new Rst(code, null, null);}public static Rst data(Object data) {return new Rst(CODE_SUCCESS, "ok", data);}// 构建失败public static Rst error() {return new Rst(CODE_ERROR, "error", null);}public static Rst error(String msg) {return new Rst(CODE_ERROR, msg, null);}@Overridepublic String toString() {return "{"+ "\"code\": " + this.getCode()+ ", \"msg\": " + transValue(this.getMsg())+ ", \"data\": " + transValue(this.getData())+ "}";}private String transValue(Object value) {if (value instanceof String) {return "\"" + value + "\"";}return String.valueOf(value);}
}

3.controller,

package com.example.wys.controller;import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;import com.example.wys.entity.WeatherEntity;
import com.example.wys.service.WeatherService;
import com.example.wys.utils.Constant;
import com.example.wys.utils.Rst;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.math.RoundingMode;
import java.util.Date;/*** 添加说明** @author kit* @version 1.0* @date 2023/11/7 16:50*/
@RestController
@RequestMapping("/weather")
public class WeatherController {@ResourceWeatherService weatherService;/*** 新增** @return*/@GetMapping("add")public Rst add() {Date date = new Date();Long dateTime = date.getTime();// 模拟数据// 此处涉及到的字符串的,必须前后加',如下面的city字段,quality字段, remark字段WeatherEntity testEntity = WeatherEntity.builder().samplingTime(dateTime.toString()).samplingTimeStr("'" + DateUtil.format(date, "yyyy-MM-dd HH:mm:ss") + "'").cityKey(101190101).city("'南京'").temperature(NumberUtil.parseFloat(StrUtil.toString(RandomUtil.randomDouble(-18.2, 30.5, 1, RoundingMode.HALF_UP)))).humidity(NumberUtil.parseFloat(StrUtil.toString(RandomUtil.randomDouble(1, 100, 1, RoundingMode.HALF_UP)))).pm10(NumberUtil.parseFloat(StrUtil.toString(RandomUtil.randomDouble(0, 300, 0, RoundingMode.HALF_UP)))).pm25(NumberUtil.parseFloat(StrUtil.toString(RandomUtil.randomDouble(0, 300, 1, RoundingMode.HALF_UP)))).quality("'" + Constant.QUALITY_OPTIONS[RandomUtil.randomInt(0, 3)] + "'").remark("'模拟插入'").build();return Rst.data(weatherService.addWeather(testEntity));}/*** 分页** @param page     第几页* @param pageSize 每页多少条* @return*/@GetMapping("page")public Rst page(Integer page, Integer pageSize) {return Rst.data(weatherService.pageWeather(page, pageSize));}/*** 删除数据* 对于delete语句,其中子句只能包含时间表达式,目前不支持值筛选** @param startTime 需要固定格式为yyyy-MM-dd HH:mm:ss* @param endTime   需要固定格式为yyyy-MM-dd HH:mm:ss* @return*/@GetMapping("delete")public Rst delete(String startTime, String endTime) {// 官方对于delete语句,其中子句只能包含时间表达式,目前不支持其他值筛选return Rst.data(weatherService.deleteWeather(startTime, endTime));}
}

4.service

package com.example.wys.service;import com.example.wys.entity.WeatherEntity;import java.util.List;/*** 添加说明** @author kit* @version 1.0* @date 2023/11/7 16:50*/
public interface WeatherService {Integer addWeather(WeatherEntity weatherEntity);List<WeatherEntity> pageWeather(Integer page, Integer pageSize);Integer deleteWeather(String startTime, String endTime);
}

4.2实现类

package com.example.wys.service.impl;import com.example.wys.entity.WeatherEntity;
import com.example.wys.mapper.WeatherMapper;
import com.example.wys.service.WeatherService;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.List;/*** 添加说明** @author kit* @version 1.0* @date 2023/11/7 16:50*/
@Service
public class WeatherServiceImpl implements WeatherService {@ResourceWeatherMapper weatherMapper;@Overridepublic Integer addWeather(WeatherEntity testEntity) {return weatherMapper.addWeather(testEntity);}@Overridepublic List<WeatherEntity> pageWeather(Integer page, Integer pageSize) {if (page == null || page < 1) {page = 1;}if (pageSize == null || pageSize < 1) {pageSize = 10;}int offset = (page - 1) * pageSize;List<WeatherEntity> entityList = weatherMapper.pageWeather(pageSize, offset);System.out.println(entityList);return entityList;}@Overridepublic Integer deleteWeather(String startTime, String endTime) {return weatherMapper.deleteWeather(startTime, endTime);}
}

5.mapper

package com.example.wys.mapper;import com.example.wys.entity.WeatherEntity;import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;import java.util.List;/*** 添加说明** @author kit* @version 1.0* @date 2023/11/7 16:52*/
@Mapper
public interface WeatherMapper {Integer addWeather(WeatherEntity weatherEntity);List<WeatherEntity> pageWeather(@Param("pageSize") Integer pageSize, @Param("offset") Integer offset);Integer deleteWeather(@Param("startTime") String startTime, @Param("endTime") String endTime);
}

6.mapper.xml

<?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"><mapper namespace="com.example.wys.mapper.WeatherMapper"><resultMap id="BaseResultMap" type="com.example.wys.entity.WeatherEntity"><result column="Time" property="timestamp" /><result column="root.ln.weather.samplingTime" property="samplingTime"/><result column="root.ln.weather.samplingTimeStr" property="samplingTimeStr"/><result column="root.ln.weather.cityKey" property="cityKey"/><result column="root.ln.weather.city" property="city"/><result column="root.ln.weather.temperature" property="temperature"/><result column="root.ln.weather.humidity" property="humidity"/><result column="root.ln.weather.pm10" property="pm10"/><result column="root.ln.weather.pm25" property="pm25"/><result column="root.ln.weather.quality" property="quality"/><result column="root.ln.weather.remark" property="remark"/></resultMap><insert id="addWeather" parameterType="com.example.wys.entity.WeatherEntity">insert into root.ln.weather(samplingTime, samplingTimeStr, cityKey, city, temperature, humidity, pm10, pm25,quality, remark)values (#{samplingTime}, #{samplingTimeStr}, #{cityKey}, #{city}, #{temperature}, #{humidity}, #{pm10}, #{pm25},#{quality}, #{remark})</insert><select id="pageWeather" resultMap="BaseResultMap">select amplingTime, samplingTimeStr,city, temperature, humidity, pm10, pm25,quality, remark from root.ln.weather</select><delete id="deleteWeather" parameterType="java.lang.String">deletefrom root.ln.weather.*where time <![CDATA[ >= ]]> ${startTime}and time <![CDATA[ <= ]]> ${endTime};</delete></mapper>

postman 测试

127.0.0.1:8083/weather/page

注意:利用postman测试时,传入的string类型的属性值和整合mysql的用法有区别:

  平时传入的json如下:
{"timestamp":"2023-05-04T18:28:20.689+08:00","name":"小明","age":20,"sex":"男","address":"西安108号"}
    但是这里json传参需要给String类型加上单引号' ',否则,运行会报sql错误
{"timestamp":"2023-05-04T18:28:20.689+08:00","name":"'小明'","age":20,"sex":"'男'","address":"'西安108号'"}

http://www.ppmy.cn/server/13511.html

相关文章

C++笔记:类和对象(一)->封装

类和对象 认识类和对象 先来回忆一下C语言中的类型和变量&#xff0c;类型就像是定义了数据的规则&#xff0c;而变量则是根据这些规则来实际存储数据的容器。类是我们自己定义的一种数据类型&#xff0c;而对象则是这种数据类型的一个具体实例。类就可以理解为类型&#xff0c…

音频格式编码

TCFG_DEC_MP3_ENABLE MP3数据压缩格式 TCFG_DEC_WMA_ENABLE Windows Media Audio是一种数字音频格式 TCFG_DEC_WAV_ENABLE 可以保存高质量的音频数据&#xff0c;分为PCM格式和非PCM格式。 TCFG_DEC_FLAC_ENABLE 无损音频 TCFG_DEC_APE_ENABLE 数字音乐的无损压缩格式 TC…

华为Pura 70 Ultra可发送卫星图片,卫星通信升级再突破!

华为Pura 70 Ultra&#xff0c;作为华为全新Pura系列的首款产品&#xff0c;自发布以来便引起了广泛关注。这款手机不仅在设计、性能和功能等方面都有着出色的表现&#xff0c;更在手机市场中独树一帜&#xff0c;成为了引领智能手机新时代的代表。 首先&#xff0c;华为Pura 7…

算法课程笔记——List

缺点&#xff1a;不能用下标计算得到 只能 一步步来 这样才是赋值 只是得到拷贝的结果 很多容器都需要&#xff08;int&#xff09;强制转化 list可以用sort 但是 例如&#xff0c;sort(L2.begin(), L2.end());&#xff0c;这种是algorithm标准算法类提供&#xff0c;属于…

python学习笔记(集合)

知识点思维导图 # 直接使用{}进行创建 s{10,20,30,40} print(s)# 使用内置函数set()创建 sset() print(s)# 创建一个空的{}默认是字典类型 s{} print(s,type(s))sset(helloworld) print(s) sset([10,20,30]) print(s) s1set(range(1,10)) print(s1)print(max:,max(s1)) print(m…

在 Linux 操作系统中使用locate 命令快速定位文件和目录

在 Linux 操作系统中&#xff0c;locate 命令是一个非常实用的工具&#xff0c;用于快速定位文件和目录。它通过搜索系统的数据库来查找文件和目录的位置&#xff0c;而不是像 find 命令那样实时搜索文件系统。虽然在某些 Linux 发行版中 locate 命令并不默认安装&#xff0c;但…

string 类以及模拟实现

&#x1d649;&#x1d65e;&#x1d658;&#x1d65a;!!&#x1f44f;&#x1f3fb;‧✧̣̥̇‧✦&#x1f44f;&#x1f3fb;‧✧̣̥̇‧✦ &#x1f44f;&#x1f3fb;‧✧̣̥̇:Solitary_walk ⸝⋆ ━━━┓ - 个性标签 - &#xff1a;来于“云”的“羽球人”。…

性能测试工具三——perf + FlameGraph(采样测试)

1. 安装 sudo apt install linux-tools-common 如果没安装成功可尝试制定特定内核版本的工具&#xff0c;例如sudo apt install linux-tools-6.5.0-27-generic。 2. 使用perf Step1&#xff1a;编译得到可执行程序&#xff0c;假设是a.out。 Step2&#xff1a;执行类似如下指…