网站建设建设价格,网站导航,做网站用vps还是虚拟主机,建设厅网站业绩备案公示期Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能#xff0c;它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。 1、if
if标签可通过test属性的表达式进行判断#xff0c;若表达式的结果为true#xff0c;则标签中的内容会执行#xff1b;反之标签… Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。 1、if
if标签可通过test属性的表达式进行判断若表达式的结果为true则标签中的内容会执行反之标签中的内容不会执行
!--ListEmp getEmpListByMoreTJ(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
/select2、where
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
/selectwhere和if一般结合使用 若where标签中的if条件都不满足则where标签没有任何功能即不会添加where关键字若where标签中的if条件满足则where标签会自动添加where关键字并将条件最前方多余的and或or去掉 注意where标签不能去掉条件最后多余的and
3、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
/selecttrim用于去掉或添加标签中的内容 若标签中有内容时 prefix在trim标签中的内容的前面添加某些内容prefixOverrides在trim标签中的内容的前面去掉某些内容suffix在trim标签中的内容的后面添加某些内容suffixOverrides在trim标签中的内容的后面去掉某些内容 若标签中没有内容时trim标签也没有任何效果
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}/whenotherwisedid1/otherwise/choose/where
/select5、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 iddeleteMoreByArray
delete from t_emp where
foreach collectioneids itemeid separatororeid #{eid}
/foreach
/deletedelete iddeleteMoreByArray
delete from t_emp where eid in
(
foreach collectioneids itemeid separator,#{eid}
/foreach
)
/delete!--int deleteMoreByArray(int[] eids);--
delete iddeleteMoreByArray
delete from t_emp where eid in
foreach collectioneids itemeid separator, open( close)#{eid}
/foreach
/delete属性 collection设置要循环的数组或集合 item表示集合或数组中的每一个数据 separator设置循环体之间的分隔符 open设置foreach标签中的内容的开始符 close设置foreach标签中的内容的结束符 6、SQL片段
sql片段可以记录一段公共sql片段在使用的地方通过include标签进行引入
sql idempColumnseid,ename,age,sex,did
/sql
select include refidempColumns/include from t_emp