Mybatis的映射文件中,前面我们的SQL都是比较简单的,有些时候业务逻辑复杂时,我们的SQL是动态变化的,此时在前面的学习中我们的SQL就不能满足要求了。
有时候的参数是不一定的,以前我们学的都是固定死的,参数的个数也是确定的,动态SQL的参数是不一定的。
在这里我们主要讲解一下if和foreach两种:
准备条件:配置方面用的是代理方式。跟mybatis的的代理开发_Outlier_Lin的博客-CSDN博客这个是一样的。
关于if:(我们查询一个但有多个限制条件)
<mapper namespace="com.outlier.mapper.UserMapper">
<select id="findByCondition" resultType="user" parameterType="user">
select * from user
<where>
<if test="id!=0">
and id=#{id}
</if>
<if test="username!=null">
and username&