京东网站建设目标,网站设计服务表,wordpress po编译mo,优秀的响应式网站9、动态SQL
Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能#xff0c;它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。
9.1、if
if标签可通过test属性的表达式进行判断#xff0c;若表达式的结果为true#xff0c;则标签中的内容会执行…9、动态SQL
Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。
9.1、if
if标签可通过test属性的表达式进行判断若表达式的结果为true则标签中的内容会执行反之标签中的内容不会执行
!--ListEmp getEmpListByCondition(Emp emp);--
select idgetEmpListByMoreTJ resultTypeEmpselect * from t_emp where 11if testename ! and ename ! nulland ename #{ename}/ifif testage ! and age ! nulland age #{age}/ifif testsex ! and sex ! nulland sex #{sex}/if
/select9.2、where
where和if一般结合使用
若where标签中的if条件都不满足则where标签没有任何功能即不会添加where关键字若where标签中的if条件满足则where标签会自动添加where关键字并将条件最前方多余的and去掉
注意where标签不能去掉条件最后多余的and
select idgetEmpListByMoreTJ2 resultTypeEmpselect * from t_empwhereif testename ! and ename ! nullename #{ename}/ifif testage ! and age ! nulland age #{age}/ifif testsex ! and sex ! nulland sex #{sex}/if/where
/select9.3、trim
trim用于去掉或添加标签中的内容
常用属性
prefix在trim标签中的内容的前面添加某些内容
prefixOverrides在trim标签中的内容的前面去掉某些内容
suffix在trim标签中的内容的后面添加某些内容
suffixOverrides在trim标签中的内容的后面去掉某些内容
select idgetEmpListByMoreTJ resultTypeEmpselect * from t_emptrim prefixwhere suffixOverridesandif testename ! and ename ! nullename #{ename} and/ifif testage ! and age ! nullage #{age} and/ifif testsex ! and sex ! nullsex #{sex}/if/trim
/select更多关于trim标签的资料可以参考: https://www.cnblogs.com/zhihongming/p/15627939.html
9.4、choose、when、otherwise
choose、when、 otherwise相当于if…else if…else
!--ListEmp getEmpListByChoose(Emp emp);--
select idgetEmpListByChoose resultTypeEmpselect include refidempColumns/include from t_empwherechoosewhen testename ! and ename ! nullename #{ename}/whenwhen testage ! and age ! nullage #{age}/whenwhen testsex ! and sex ! nullsex #{sex}/whenwhen testemail ! and email ! nullemail #{email}/when/choose/where
/select9.5、foreach
!--int insertMoreEmp(ListEmp emps);--
insert idinsertMoreEmpinsert into t_emp valuesforeach collectionemps itememp separator,(null,#{emp.ename},#{emp.age},#{emp.sex},#{emp.email},null)/foreach
/insert!--int deleteMoreByArray(int[] eids);--
delete iddeleteMoreByArraydelete from t_emp whereforeach collectioneids itemeid separatororeid #{eid}/foreach
/delete!--int deleteMoreByArray(int[] eids);--
delete iddeleteMoreByArraydelete from t_emp where eid inforeach collectioneids itemeid separator, open( close)#{eid}/foreach
/delete9.6、SQL片段
sql片段可以记录一段公共sql片段在使用的地方通过include标签进行引入
sql idempColumns
eid,ename,age,sex,did
/sqlselect include refidempColumns/include from t_emp本文章参考B站 【尚硅谷】SSM框架全套教程MyBatisSpringSpringMVCSSM整合一套通关仅供个人学习使用部分内容为本人自己见解与尚硅谷无关。