mybaits十四:使用if和where标签构建动态sql
生活随笔
收集整理的這篇文章主要介紹了
mybaits十四:使用if和where标签构建动态sql
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?<where>標簽會在sql語句中加上 'where',并且會去掉'and'(使用where標簽的話,and就要放在語句的前面,比如'and ename = #{ename}', 如果放在語句的后面,比如?'ename = #{ename} and' , where標簽可能去不掉and,就會造成拼接的sql語句最后多一個and,造成錯誤)
<select id="getEmpsByConditionIf" resultType="com.atChina.bean.Employee">select * from(select a.empno as no, a.ename, a.job, a.mgr, a.hiredate, a.sal, a.deptno from emptest a) <where><if test="empno!=null">no = #{empno}</if><if test="ename != null and ename.trim() !='' ">and ename = #{ename}</if><if test="job !=null and job !='' ">and job = #{job}</if></where></select>?
總結
以上是生活随笔為你收集整理的mybaits十四:使用if和where标签构建动态sql的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybaits十三:使用collecti
- 下一篇: mybaits十五:使用trim自定义字