★学习目标:
掌握MyBatis中的<where>元素,能够熟练使用“拼接”元素
★思考任务:
<where>元素的作用是什么?
★任务学习:
★知识要点:
<where>元素
<select id="findCustomerByNameAndJobs"
parameterType="com.itheima.pojo.Customer"
resultType="com.itheima.pojo.Customer">
select * from t_customer where username like concat('%',#{username}, '%') and jobs= #{jobs}
<where>
<if test="username !=null and username !=''">
and username like concat('%',#{username}, '%')</if>
<if test="jobs !=null and jobs !=''">
and jobs= #{jobs}</if>
</where></select>
上述代码配置中,<where>元素会自动判断由组合条件拼装的SQL语句,只有<where>元素内的某一个或多个条件成立时,才会在拼接SQL中加入where关键字,否则将不会添加;即使where之后的内容有多余的“AND”或“OR”,<where>元素也会自动将他们去除。

