请问xml的if test如何转换成SQL语句,为什么不需要添加if?
<select id="selectByDepartmentAndPost" resultMap="employee"> select e.*,d.sn dsn,d.name dname from employee e left join department d on d.sn=e.department_sn where e.sn is not NULL <if test="dsn!=null"> and e.department_sn=#{dsn} </if> <if test="post!=null"> and e.post=#{post} </if> </select>
正在回答
下面这个地方都是if呀,为什么说不需要if呢?
这里相当于拼接成一个完整的sql语句,如果两个if的条件都不满足,sql语句的内容为:
select e.*,d.sn dsn,d.name dname from employee e left join department d on d.sn=e.department_sn where e.sn is not NULL
如果满足第一个if的条件,也就是dsn不等于null,那么sql语句就会变为:
select e.*,d.sn dsn,d.name dname from employee e left join department d on d.sn=e.department_sn where e.sn is not NULL and e.department_sn=#{dsn}
同理,如果第二个if满足,sql语句会变为:
select e.*,d.sn dsn,d.name dname from employee e left join department d on d.sn=e.department_sn where e.sn is not NULL and e.post=#{post}
所以也就是满足if条件,就把if的内容接在之前sql语句的后面
祝学习愉快!
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星