0
点赞
收藏
分享

微信扫一扫

2022-1-21 MyBatis 动态sql

松鼠树屋 2022-01-21 阅读 25

一、动态SQL之if

    <select id="getAccountByScope" resultMap="AccountMap" >
        select * from account 
            <if test="start!=null and size!=null">
                limit #{start},#{size}
            </if>
    </select>

在这里插入图片描述
如果有参数,则执行select * from account limit x,y;否则执行select * from account;

    @Test
    public void getAccountByScope()
    {
        sqlSession=SqlSessionFactoryUtils.getInstance().openSession();
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        List<Account> accountByScope = mapper.getAccountByScope(null, null);
        for(Account acc:accountByScope)
        {
            System.out.println(acc);
        }
    }

这个if标签可以用多个。

二、动态SQL之choose

choose和带break的switch差不多?
如果username和id都存在,那么则以上面那个username为查询条件

    <select id="getAccountByNameOrId" resultMap="AccountMap">
        select * from account
        where 1=1
        <choose>
            <when test="username!=null and username!=''">
                and username=#{username}
            </when>
            <when test="id!=null and id>=0">
                and id=#{id}
            </when>

        </choose>
    </select>

同时使用两个条件查询则要改成if:

    <select id="getAccountByNameOrId" resultMap="AccountMap">
        select * from account
        where 1=1
            <if test="username!=null and username!=''">
                and username=#{username}
            <if>
            <if test="id!=null and id>=0">
                and id=#{id}
            </if>

    </select>

三、动态SQL之where

使用where标签,只有下面任意一个条件满足才能有where。
注意还是要写and 因为<where>这个标签决定了and的有无

        <where>
            <choose>
                <when test="username!=null and username!=''">
                    and username=#{username}
                </when>
                <when test="id!=null and id>=0">
                    and id=#{id}
                </when>
    
            </choose>
        </where>

四、set节点

用于update
用于多个set…

    <update id="updateAccount" parameterType="org.kk.mybatis02.model.Account">
        update account
        <set>
            <if test="username!=null and username!=''">
                username=#{username},
            </if>
            <if test="id!=null and id>0">
                id=#{id},
            </if>
        </set>
            where id=#{id};
    </update>

五、trim节点

代替where和set

<trim prefix="where" prefixOverrides="or ">

</trim>

注意prefixOverrides中的空格
trim主要看sql语句中可能多了什么,会自动帮忙处理。

六、foreach

  1. 针对这样的sql语句select * from account where id in(1,2,3)
    List<Account> gwtAccountsByIds(List<Long> ids);//参数是传入的List型ids
    <select id="getAccountsByIds" resultMap="AccountMap">
        select * from account where id in
    <foreach collection="ids" open="(" close=")" item="id" separator=",">
        #{id}
    </foreach>
    </select>

collection 可以用list,如果用ids那么还要加个注解@Param(否则默认名arg0、agr1、param0、param1…) item就像foreach循环一样,取个元素名;
separator是分隔符
open和close代表头尾包着的符号…

  1. 针对这样的sql语句
    insert into account(username,money) values(xxx,xxx),(xxx,xxx)
    <insert id="addBatch" >
        insert into account(username,money) vaules
        <foreach collection="accounts"  separator="," item="account">
            (#{account.username},#{account.money})
        </foreach>
    </insert>
Integer batchAddAccount(@Param("accounts") List<Account> accounts);
  1. 针对更新的情况(set)
    遍历map
    <update id="updateBook">
        update t_book
        <set>
            <foreach collection="maps" index="key" item="val" separator=",">
                ${key}=#{val}
            </foreach>
        </set>
    </update>

七、bind

bind可以定义变量

    <select id="getBooksByAuthorFirstName" resultMap="BookMap">
        <bind name="authorLike" value="author+'%'"/>
        select * from t_book where author like #{authorLike};

    </select>

八、MyBatis对多数据库的支持

在这里插入图片描述

所有数据库驱动都会实现一个DatabaseMetaData的类,里面有个方法getDatabaseProduceName会返回数据库标志

举报

相关推荐

0 条评论