(二)Mybatis动态sql

首先动态sql简单来讲就是可以根据传入参数的不同来动态的生成sql语句,拼接where语句,这样你就不用写很多个sql语句了,并且它里面有一些特性也可以帮助你避免sql语句的拼接错误,主要分为4个:

if,chooose,trim,foreach

IF:

对参数进行判断,拼接不同的sql语句,看一个例子

    <select id="selectByIf" parameterType="person" resultType="person">
        select *from person where sex != 0
        <if test="name != null">
            AND name = #{name}
        </if>
    </select>

很简单,test=".."里面的是条件,当传入的name属性不为空时,就将if标签体当中的sql语句拼接到其后,当然你后面也可以写很多个if,满足条件的都会拼接。另外test里面的条件也是可以进行复合判断的,比如:

    <select id="selectByIf" parameterType="person" resultType="person">
        select *from person where sex != 0
        <if test="name != null and name!='温鸿飞'">
            AND name = #{name}
        </if>
    </select>

这时候如果传入对象的name为温鸿飞,那就不会拼接sql语句了。

Choose:

这个语句其实即使if,else,从第一个开始向下找,找到满足条件的就停止,比如;

    <select id="selectByIf" parameterType="person" resultType="person">
        select *from person where sex != 0
        <choose>
            <when test="name!=null">
                and name = #{name}
            </when>
            <when test="name=='温鸿飞'">
                and name = '321321'
            </when>
            <otherwise></otherwise>
        </choose>
    </select>

很容易理解,不过说明一点,比如你传入的name值为温鸿飞,那么拼接的sql语句是and name = #{name},而不是第二个条件,即多个条件都满足时,拼接第一个。

,where,

是为了解决出现sql语法错误的情况:比如将上面if语句后面的sex!=0去掉,

  <select id="selectByIf" parameterType="person" resultType="person">       
select *from person where
    <if test="sex!=null">
      sex != #{sex}
    </if> <if test="name != null"> AND name = #{name} </if>
</select>

这时如果满足条件,很明显拼接出来的sql语句是不正确的,这时候只要给if外面加上<where>标签就可以了,加上之后会自动去除and,并且会加上where(如果满足其中一个if条件的话),如下:

    <select id="selectByIf" parameterType="person" resultType="person">
        select *from person
        <where>
        <if test="sex!=null">
            sex != #{sex}
        </if>
        <if test="name!=null">
            and name = #{name}
        </if>
        </where>
    </select>

set,

这个标签是在动态更新的时候用的,可以帮助我们加上set,并去除无关的逗号,如下

    <update id="updateByIf" parameterType="person">
        update person
        <set>
            <if test="name!=null">name=#{name },</if>
            <if test="sex!=null">sex=#{sex}</if>
        </set>
        where id =#{id}
    </update>

如果只传入了name,那么name=#{name},  后面的这个逗号也会帮我们去掉,这个意思

Foreach

一般用于in标识符后面,如下:

    <select id="selectByForeach" parameterType="list" resultType="person">
        select * from person where id in
        <foreach collection="list" item="id" close=")" open="(" separator=",">
            #{id}
        </foreach>
    </select>

item是别名,collection是类型,list和array一般,后面三个是拼接时候的开始结尾分隔符,接口这样写:

List<Person> selectByForeach(List list);

传入的是List类型,

例,插入多条数据,传入list,每一项都是Person对象,构建sql语句即可

    <insert id="insertAll" parameterType="list">
        insert into person (`name`,`loves`,`sex`) values
        <foreach collection="list" separator="," open="" close="" item="person">
            (#{person.name},#{person.loves},#{person.sex})
        </foreach>
    </insert>

规则就这些,用的时候肯定会很复杂

posted @ 2019-08-08 14:14  _Ennio  阅读(164)  评论(0编辑  收藏  举报