mysql主要操作
关键字: mySQL,正则化,插查修改
一、 主要表
Id(varchar):设备唯一标识
Name(varchar):设备名称
Type(int):设备类型,比如摄像头、传感器、气象站、水阀等。
BlockId(text):设备所属地块
updateTime(varchar):更新时间
二、数据结构
@Data
public class DeviceParam {
@NotBlank
private String id;
private Integer name;
@NotNull
private Integer type;
@NotBlank
private String blockIds;
}
三、数据库操作
1 插入
(1)单个设备
@Insert("insert into Device(id, name, type, blockId) values (#{id},#{name},#{type},#{blockIds})") public void insertDevice(DeviceParam param);
(2)多个设备
@Insert("<script>" + "insert ignore into Device(id, name, type, blockId) values " + "<foreach item=device collection=list separator=','>" + "(#{device.id},#{device.name},#{device.type},#{device.blockId})" + "</foreach>" + "</script>") public void insertDevice(@Param("list") List<DeviceParam> param);
注:如果插入记录的主键在表中已经存在,insert ignore则会忽略此条记录,而insert into则会再次保存。
2更新
更新所有设备的更新时间
@Update("update Device set updateTime = #{updateTime}") public void updateUpdateTime(@Param("updateTime") String updateTime);
3 查询
(1)获取设备列表的最后更新时间
@Select("select updateTime from Device order by type limit 1") public String getUpdateTime();
(2)查询地块上的多个类型的设备
@Select( "<script>" + "select * from Device where blockId REGEXP #{ids} and type in" + "<foreach item = 'type' index = 'index' collection = 'types' open='(' separator=',' close=')'>" + "#{type} </foreach>" + "order by type, name</script>" ) public List<Device> getDevice(@Param("ids") String blockIds, @Param("types") List<Integer> types);
注意:
(a) 一个设备可能属于多个地块,即blockId的值可能很长,所以用text类型。多个地块可以用逗号进行分割。
(b)查询属于某个地块的设备时,由于blockId的值可能是多个地块信息(如,一个气象站对应多个地块)。要用正则表达式进行匹配,此时需要把地块信息中的逗号替换成竖线‘|’,然后再执行下面的语句,不然匹配为空。
(c)下面语句中的blockId对应表中的字段,#{blockIds}和’types’ 对应接口中用@Param申明的参数。
(3)查询地块上的单个类型的设备
@Select("select cameraUuid from Device where blockId like '%${blockID}%' and type = #{deviceType}") public List<String> getDevice(@Param("blockID") String blockId, @Param("deviceType") Integer deviceType);
(4)联合查询水阀数据,包含它所属的传感器
@Select("select valve.* from Valve as valve inner join SensorValveBind as bind on bind.valveId = valve.id and bind.sensorId = #{sensorId} and bind.sensorType = #{sensorType} ) public List<ValveData> getValveBySensor(@Param("sensorId") String sensorId, @Param("sensorType") Integer sensorType);
(5) 联合查询传感器状态
@Select("<script>" + "select valve.status,valve.autoIrrigate,bind.sensorId,bind.sensorType from ValveData as valve inner join SensorValveVind as bind on bind.valveId = valve.id and bind.sensorId in" + "<foreach item = 'sensorId' index = 'index' collection = ids open='(' separator=',' close=')'>" + "#{sensorId} </foreach> group by bind.sensor_id" + "</script>") public List<SensorStatus> getSensorStatus(@Param("ids") List<String> sensorIds);
(6)复杂语句操作
可以借助于@InsertProvider注解
@InsertProvider(type = Provider.class, method = "insertStationData") public void insertStationData(StationParam param);
然后,在Provider.java里,添加如下函数:
public String insertStationData(StationParam param) { return new SQL() { { SELECT("*"); FROM("stationData"); WHERE("windSpeed = #{windSpeed }"); WHERE("windDirection= #{windDirection}"); WHERE("temprature = #{temprature }"); WHERE("is_deleted = #{isDeleted}"); } }.toString(); }
或者
public String insertStationData(StationParam param ) { SQL sql = new SQL() .SELECT("*") .FROM("stationData") .WHERE("windSpeed = #{windSpeed }") .WHERE("windDirection= #{windDirection}") .WHERE("temprature = #{temprature }") .WHERE("is_deleted = #{isDeleted}"); return sql.toString(); }
- MySQL主要日志的基本操作与简单解析
- MYSQL账户管理及主要操作
- Windows下Mysql常用操作命令
- mysql(2a记录的操作)
- 关于MySQL中的操作学习
- mysql 操作日期函数【增加,减少时间】
- Python 操作 MySQL 的正确姿势
- Mysql----操作
- mysql常用基础操作语法(六)--对数据排序和限制结果数量的条件查询【命令行模式】
- Python操作MySQL模拟银行转账
- mysql操作
- 实例讲解mysql入门基本操作语句
- 数据库MYSQL入门教程(2)——数据库操作指令
- 20181022mysql操作一:创建库,表的增删改查,数据的增删改
- MySQL一些操作备忘
- 本章主要介绍C语言程序的结构和书写规则,以及Turbo C V2.0的基本操作
- Mysql 存储引擎中InnoDB与Myisam的主要区别
- Mysql 存储引擎中InnoDB与Myisam的主要区别
- MySQL一些操作备忘
- c++操作mysql - 查询