一.Mybatis 提供了<if>、<where>、<foreach>标签来实现sql的动态拼接
1.<if test="中间为判断的条件">
2.<where> 代替sql中的关键字,用where 标签可以根据实际情况自动处理if开头的and关键字
3.<foreach> 传入的参数为grade 是实体类的别名 List<Interger> stuNos
select * from student
<where>
<if test="stuNos!=null and stu.size()>0">
<foreach collection="stuNos" open= "stuNo in (" close=")" item="stuNo" separator=",">
#{stuNo}
</foreach>
</if>
</where>
二.传入参数为其他类型的集合
1.将集合或数组以对象属性的形式传入
内容在上面
2.List类型的集合
parameterType="java.util.List" 将collection里面的内容改为list 其他的与上面一致
3.传入简单的数组类型
将parameterTye改为指定的数组类型,必须用array名字来接收传进来的数组
4.传入对象数组
与简单数组基本相同,将parameterType的值固定写成Object[]
select * from student
<where>
<if test="array!=null and array.lenth>0">
<foreach collection="array" open= "stuNo in (" close=")" item="student" separator=",">
#{student.stuNo}
</foreach>
</if>
</where>
三、为了达到复用的目地,可以将<where> 中的if提取出来,在需要的地方<include>导入
select * from student
<where>
<include refid="sql片段的id">
</where>