✉️ 查询业务
   ➿ 目前互联网项目的诸多业务中数查询业务居多,而查询业务中又数展示类接口、报表类接口是我们接触最多的查询类接口。在实际项目中,不是全局查询数据就做完了业务,这样的需求也没有意义。条件查询因此作为最普遍的查询业务,涉及到各种条件使查询的数据结果规范化和合理化。
✉️ 条件查询
   ➿ 条件查询本质上在sql层面是包含在where条件中的语句,SpringBoot接口传参一般都是多个字段组合构成,那么对应的where中就包含多个条件语句。传参只由基本数据类型字段组成是我们一开始做得最多的业务,注意点不多,这里就不再做过多阐述。下面就传参是List集合类型字段时,聊聊在代码层面的撰写方式。
📫 实例演示
🔍应用最好的解答方式就是通过实际案例演示,下面就此进行展开。
   🔒实例应用:如何查询某个时间段内多个路段在多个方向上的车流量?
🔑① 构建返回的实体类RoadSectionTrafficVolume
@Data
public class RoadSectionTrafficVolume {
    @ApiModelProperty("****")
    private String roadSectionName;
    @ApiModelProperty("****")
    private String direction;
    @ApiModelProperty("****")
    private Integer vehicleCnt;
}
🔑② mapper层面
其中查询条件中包含开始时间,结束时间,方向及路段名称两个List集合
List<RoadSectionTrafficVolume> selectEveryRoadSectionTrafficList(@Param("beginDate")String beginDate, @Param("endDate")String endDate,
                                                                     @Param("directionList")List<String> directionList, @Param("roadSectionNameList")List<String> roadSectionNameList);
🔑③ XML写法
🔨 其中传参为List类型数据时,需要借助<foreach> </foreach>,与本人之前的文章:Mybatis如何批量插入数据?有相似之处(大家可以此为参考),但是又不是全部相同,毕竟一个是插入操作,另外一个查询操作。
🔨 这里给出一个项目中的Tip:像List这种多数据字段的条件sql语句用“in”语句将其条件进行包含,尽量少用or,因为踩过坑😂
🔨 下面的XML语句中,有曾未说明的元素属性需要做解释为:
 open = "(" :该语句遇到 "(" 以此作为开始标志;
 close = ")" :该语句遇到 ")" 以此作为结束标志。
<select id="selectEveryRoadSectionTrafficList" resultType="com.***.RoadSectionTrafficVolume">
    SELECT road_section_name AS roadSectionName, direction, SUM(vehicle_cnt) AS vehicleCnt
    FROM dws_road_section_traffic_volume
    WHERE formatDateTime(toDateTime(loadtime),'%Y-%m-%d %H:%M') >= #{beginDate}
    AND formatDateTime(toDateTime(loadtime),'%Y-%m-%d %H:%M') < #{endDate}
    AND road_section_name in
    <foreach collection="roadSectionNameList" item="roadSectionNameList" index="index" open="(" close=")" separator=",">
        #{roadSectionNameList}
    </foreach>
    AND direction in
    <foreach collection="directionList" item="directionList" index="index" open="(" close=")" separator=",">
        #{directionList}
    </foreach>
    GROUP BY road_section_name , direction
</select>
🍆🍆🍆路过的小伙伴,如果本篇博文对你的学习或者工作有所帮助,可以点赞+收藏+关注一波呀~👊👊👊小编后续每过一段时间会整理出相关项目实例的博文,感谢您的支持哦!!!✈️✈️✈️
 



















