Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。
1、if
if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之标签中的内容不会执行
select * from t_emp where 1=1and ename = #{ename}and age = #{age}and sex = #{sex}
2、where
select * from t_empename = #{ename}and age = #{age}and sex = #{sex}
where和if一般结合使用:
a>若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
b>若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and去掉
注意:where标签不能去掉条件最后多余的and
3、trim
select * from t_empename = #{ename} andage = #{age} andsex = #{sex}
更多Java –大数据 – 前端 – UI/UE - Android - 人工智能资料下载,可访问百度:尚硅谷官网(www.atguigu.com)
trim用于去掉或添加标签中的内容
常用属性:
prefix:在trim标签中的内容的前面添加某些内容
prefixOverrides:在trim标签中的内容的前面去掉某些内容
suffix:在trim标签中的内容的后面添加某些内容
suffixOverrides:在trim标签中的内容的后面去掉某些内容
4、choose、when、otherwise
choose、when、otherwise相当于if...else if..else
select from t_empename = #{ename}age = #{age}sex = #{sex}email = #{email}
5、foreach
insert into t_emp values
(null,#{emp.ename},#{emp.age},#{emp.sex},#{emp.email},null)
delete from t_emp where
eid = #{eid}
delete from t_emp where eid in
#{eid}
更多Java –大数据 – 前端 – UI/UE - Android - 人工智能资料下载,可访问百度:尚硅谷官网(www.atguigu.com)
属性:
collection:设置要循环的数组或集合
item:表示集合或数组中的每一个数据
separator:设置循环体之间的分隔符
open:设置foreach标签中的内容的开始符
close:设置foreach标签中的内容的结束符
6、SQL片段
sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入
eid,ename,age,sex,did
select from t_emp
MyBatis全局配置文件
java开发SSM框架整合之MyBatis动态SQL
java培训Mybatis动态Sql处理解析
Mybatis如何解决字段名和属性名不同的冲突
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.