02 - Unit09:动态SQL
动态SQL
什么是?
系统运行过程中,动态生成的SQL语句
为什么?
当我们不能确定用户操作,所要使用的具体SQL的时候.
案例: 搜索笔记功能 按用户名 笔记本名 笔记名 搜索
搜索功能 按用户 A B C
select * from cn_note where userName=#{} and bookName=#{} and noteTitle=#{}
如何实现?
-
{id}
if标签
if起到选择判断的作用
语法:
应用:
<select id="findNotes" parameterType="Map">
select * from cn_note
where cn_note_status_id="1"
<if test="userName!=null">
and cn_user_name=#{userName}</if>
<if test="bookName!=null">
and cn_notebook_name=#{bookName}</if>
<if test="noteName!=null">
and cn_note_name=#{noteName}</if>
</select>
相当于JAVA中的switch语句,当匹配(when)到符合条件的语句后,直接输入,结束匹配;
当没有匹配到任何条件,最后输出otherWise
语法:
<choose>
<when test=""></when>
<when test=""></when>
<when test=""></when>
<otherwise></otherwise>
</choose>
where/set
在开始标签的位置自动输入 where关键字.
在拼凑SQL语句的过程中,会自动处理多余的and/or/空格.
语法:
<where>
<if test=""></if>
<if test=""></if>
</where>
<set>
<if test=""></if>
<if test=""></if>
</set>
trim
语法:
<trim prefix="where" prefixOverrides="and/or"></trim>
练习:批量删除笔记
delete from cn_note where cn_note_id
in (#{id1},#{id2},#{id3}.....)
<foreach collection="list/array"
item="id"
open="("
close=")"
separator=",">
#{id}
</foreach>
组合查询笔记的功能
标题 状态 开始时间 结束时间 搜索按钮
搜索列表
标题 状态 创建时间
发送Ajax请求
-
绑定事件:搜索按钮的单击事件
-
获取参数:标题 状态 开始时间 结束时间
-
请求地址:/note/manage.do
服务器处理
- Controller.find(String userId,String title,String
status,Stringbegin,String end)
- Service.find(String userId,String title,String status,
String begin,String end)
-
NoteDao.findNotes(Map params)
-
Mapper select * from cn_note 动态SQL
Ajax回调处理
- success
遍历返回的数据集合,显示在结果列表中
- error
提示:搜索失败
作业:重构搜索笔记需求的mapper定义
-
使用
-
使用
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法