这个语句应该如何理解?

abz7676 2016-05-25 11:53:19


select
*
from activity
where 1=1
<if test="city != null and city!=''">
and city =#{city,jdbcType=VARCHAR}
</if>

<if test="contentType != null and contentType >0">
and content_type =#{contentType,jdbcType=INTEGER}
</if>
and status =1
order by create_time desc
limit #{start,jdbcType=INTEGER},#{limit,jdbcType=INTEGER}



其中test和jdbcType不是数据库表activity里的字段

那么这句sql语句应该如何理解,最好能一句一句帮我解释一下哦。
...全文
845 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2016-05-25
  • 打赏
  • 举报
回复
大概可这样理解 SELECT * FROM City=#City--参数 AND ContentType=#ContentType--参数 AND STATUS=1 ORDER BY create_time DESC limit #Start,#Limit--取记录参数 开始显示记录数行数-1,限制显示的记录数 其中如果参数有值时以下条件没意义 city != null and city!=''--City is not null--这样改才有用,非null 和空字符 contentType != null and contentType >0 --contentType is not null--这样改才有用,非null和0
卖水果的net 版主 2016-05-25
  • 打赏
  • 举报
回复
这段文本,应该是一个 mapper 中的内容,你的开发框架是不是 mybatis ? 你的传入参数,是实体还一个 Dto ?
Ginnnnnnnn 2016-05-25
  • 打赏
  • 举报
回复
这个应该是传入数据库之前程序预先根据动态条件拼接传入数据库的语句吧。具体效果你只要选择性地填条件调试一下实际生成的语句即可 select * from activity where 1=1 ---根据是否有city 的值来判断要不要加上 and city = 变量值 的这个条件,没有则不加 <if test="city != null and city!=''"> and city =#{city,jdbcType=VARCHAR} </if> ---同上,这次是判断contenttype 这个字段的。 <if test="contentType != null and contentType >0"> and content_type =#{contentType,jdbcType=INTEGER} </if> and status =1 order by create_time desc --分页语句。 limit #{start,jdbcType=INTEGER},#{limit,jdbcType=INTEGER}
Tiger_Zhao 2016-05-25
  • 打赏
  • 举报
回复
这看起来不是最终的SQL,而是拼SQL语句的一个模板。

大致上调用这个模板的时候有这样一组参数表
参数名称,类型,值
city,VARCHAR,?
contentType,INTEGER,?
start,INTEGER,80
limit,INTEGER,10

后两个参数是必须的,随便举了个例子。
前两个参数是可空的,决定两个 <if></if> 块内的语句是否要拼在里面。

最终拼出来的语句最少/最多是下面这样
select 
*
from activity
where 1=1
and status =1
order by create_time desc
limit 80,10

select 
*
from activity
where 1=1
and city ='abc'
and content_type =3
and status =1
order by create_time desc
limit 80,10

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧