目录
1. Model : XxxMapper.xml
1. IotTypeMapper.xml 基础 × 3 tips
2. Model : XxxMapper.java
1. IotTypeMapper.java 基础 × 3 tips
3. Others info
1. 模糊查询
2. 模糊查询 'name' 导致的异常 --> `name
3. 连接查询 Where 限制主表 , 谨慎 : 使用副表限制 - ★
4. 数据去重 : group by | distinct ( 视情况选择 )
5. 以 map 为查询条件的 API
6. 标签判断处理 : String / 集合 / 基本数据类型参数为 0
7. 将 null 值更新到数据库
1. Model : XxxMapper.xml
1. IotTypeMapper.xml
基础 × 3 tips
<!-- IotTypeVO查询映射结果 -->
<resultMap id="IotTypeVOResultMap" type="com.hzcloud.iot.resource.vo.IotTypeVO">
<result column="id" property="id" />
<result column="name" property="name" />
<result column="type" property="type" />
<result column="attribute" property="attribute" />
<result column="status" property="status" />
<result column="create_time" property="createTime" />
<result column="update_time" property="updateTime" />
<result column="del_flag" property="delFlag" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id , name , type , attribute , status , create_time , update_time , del_flag
</sql>
<!-- 无参查询房源类型List -->
<select id="queryIotTypeList" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM iot_type
WHERE del_flag = 0 AND status = 0
ORDER BY create_time DESC
</select>
<!-- 条件查询类型字段List -->
<select id="queryByConditions" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM iot_type
<where>
del_flag = 0
<if test="iotTypeDTO.type != null and iotTypeDTO.type != '' ">
AND type = #{iotTypeDTO.type}
</if>
<if test="iotTypeDTO.status == null or iotTypeDTO.status == '' ">
AND status in (0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9)
</if>
<if test="iotTypeDTO.status != null and iotTypeDTO.status != '' ">
AND status = #{iotTypeDTO.status}
</if>
</where>
ORDER BY create_time DESC
</select>
优化纪录
1、查存时增加状态 status 条件 , 启动 status 禁用全局隐藏机制 - 20.10.24
2. Model : XxxMapper.java
1. IotTypeMapper.java
基础 × 3 tips
/**
* 无参查询资源类型List
* @return 资源类型展示对象集合IotTypeVOList
*/
List<IotTypeVO> queryIotTypeList();
/**
* 条件查询资源类型List
* @param iotTypeDTO 资源类型接收入参DTO
* @return 资源类型展示对象集合IotTypeVOList
*/
List<IotTypeVO> queryByConditions(@Param("iotTypeDTO") IotTypeDTO iotTypeDTO);
/**
* 条件查询资源类型Page
* @param page 分页参数
* @param iotTypeDTO 资源类型接收入参DTO
* @return 资源类型展示对象集合IotTypeVOList
*/
IPage<IotTypeVO> queryByConditions(@Param("page")Page page , @Param("iotTypeDTO") IotTypeDTO iotTypeDTO);
3. Others info
1. 模糊查询
<if test="userDTO.name != null and userDTO.name != ''">
and u.name like CONCAT('%' , #{userDTO.name} , '%')
</if>
%通配符使用
-- 匹配以"yves"开头的记录:(包括记录"yves")
SELECT * FROM products WHERE products.prod_name like 'yves%';
-- 匹配包含"yves"的记录(包括记录"yves")
SELECT * FROM products WHERE products.prod_name like '%yves%';
-- 匹配以"yves"结尾的记录(包括记录"yves" , 不包括记录"yves " , 也就是yves后面有空格的记录 , 这里需要注意)
SELECT * FROM products WHERE products.prod_name like '%yves';
_ 通配符使用
--匹配结果为: 像"yyves"这样记录.
SELECT * FROM products WHERE products.prod_name like '_yves';
--匹配结果为: 像"yvesHe"这样的记录.(一个下划线只能匹配一个字符 , 不能多也不能少)
SELECT * FROM products WHERE products.prod_name like 'yves__';
2. 模糊查询 'name' 导致的异常 --> `name
<if test="iotTypeDTO.name != null and iotTypeDTO.name != '' ">
AND 'name' like CONCAT('%' , #{iotTypeDTO.name} , '%')
</if>
导致模糊查询 name 参数失效
API 调用返回结果集 : {}
修改
<if test="iotTypeDTO.name != null and iotTypeDTO.name != '' ">
AND name like CONCAT('%' , #{iotTypeDTO.name} , '%')
</if>
3. 连接查询 Where 限制主表 , 谨慎 : 使用副表限制 - ★
SELECT
img.id , img.name , img.cause , img.tenant_id , img.room_id , img.compere_id , img.preparer_id , img.type , img.level , img.start_time , img.end_time ,
img.introduction , img.status , img.create_time , img.update_time , img.del_flag , irm.structure_id , irm.name AS roomName
FROM iot_meeting img
LEFT JOIN iot_room irm ON img.room_id = irm.id
WHERE
img.del_flag = 0 AND irm.del_flag = 0
AND to_days(img.start_time) = to_days("2021-04-015 12:00:00")
SELECT
img.id , img.name , img.cause , img.tenant_id , img.room_id , img.compere_id , img.preparer_id , img.type , img.level , img.start_time , img.end_time ,
img.introduction , img.status , img.create_time , img.update_time , img.del_flag , irm.structure_id , irm.name AS roomName
FROM iot_meeting img
LEFT JOIN iot_room irm ON img.room_id = irm.id
WHERE
img.del_flag = 0 AND irm.del_flag = 0
AND to_days(img.start_time) = to_days("2021-04-015 12:00:00")