您的位置:首页 > 数据库 > MySQL

mysql主要操作

2019-05-21 10:15 20 查看

关键字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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: