装修公司排名 装饰设计短视频seo优化
比如我们有一个订单记录管理界面
条件可以通过订单号、商品名称、创建日期范围、价格范围。。。来进行筛选查询。
首先我们先确定数据库订单表(我这里就不做连表了,都放在一个表中)模拟一个订单表
order表
订单号 | 商品名称 | 创建日期 | 价格 | 地址 | 用户 |
121 | 飞机 | 2023-03-23 | 199 | bj | xxx |
212 | 大炮 | 2023-04-23 | 29 | bj | xxx |
就比如我们有一个这样的订单表,
前端的话说一下思路,就不做演示了,我们将查询的每个字段当作参数传递给后端(我们可以通过order实体类来接受,此时要注意前端字段命名问题了,但是时间和价格都是一个范围,包括开始时间-结束时间,我们实体类中没有这个字段,此时我们就需要添加一个DTO来继承那个实体类,在DTO中添加这几个实体类中没有的但是我们查询过程中会用到的字段,通过DTO来接受参数,注意:前端通过JSON传入后端,后端通过@RequestBody接收)
如果不懂怎么写的可以去看下我的这个文章前后端交互问题
如果没什么特殊的业务操作我们的业务层和控制层基本不用写代码(记得时间要格式化);
实现条件查询做重要的的部分来了也就是我们的sql语句
这里我们使用mybatis的 .xml配置文件来写sql
这里sql中就用到动态拼接的方法,也就是<where> <if> ....
本文实现功能只用到了这两个,其他你们可以去了解
//这里的resultMap="BaseResultMap"可以改成resultType="DTO类的全路径"
<select id="selectOrder" resultMap="BaseResultMap">
select
tipo.id, tipo.prizes_name,tipo.Receive_address_id, tipo.create_time,
tipo.Audit_time,
tipo.integral, tipo.user_name
from
t_order tipo
<where>
'1'='1'
<if test="prizesName != null and prizesName != ''">
and tip.prizes_name = #{prizesName}
</if>
<if test="startIntegral != null and startIntegral != ''">
<if test="endIntegral != null and endIntegral != ''">
and tip.prizes_integral between #{startIntegral} and #{endIntegral}
</if>
</if>
<if test="changeStartTime != null and changeStartTime != ''">
<if test="changeEndTime != null and changeEndTime != ''">
and tipo.create_time between #{changeStartTime} and #{changeEndTimestartTime}
</if>
</if>
</where>
order by tipo.create_time
</select>
上面我们就通过动态拼接实现了按多条件查询的例子啦~,上面这种也可以不通过sql,直接在前端使用filter进行实现(可以提高用户体验,秒速查询)可以去看这个vue实现过滤器