MyBatis动态SQL

MyBatis动态SQL

使用动态SQL可简化代码开发,减少开发者工作量,程序可以自动根据业务参数来决定SQL的组成。

  • ⭐if标签
<select id="findByAccount" parameterType="com.oyrf.frist.Account" resultType="com.oyrf.frist.Account">
        select  * from t_account where
        <if test="id!=0">id = #{id} </if>
        <if test="username!=null">and username = #{username} </if>
        <if test="password!=null">and password = #{password }</if>
        <if test="age!=0"> and age = #{age}</if>
</select>

if标签可以自动根据表达式的结果来决定是否将对应的语句添加到SQL中,如果条件不成立则不添加,如果条件成立则添加。

  • ⭐where标签
<select id="findByAccount" parameterType="com.oyrf.frist.Account" resultType="com.oyrf.frist.Account">
        select  * from t_account
        <where>
            <if test="id!=0">id = #{id} </if>
            <if test="username!=null">and username = #{username} </if>
            <if test="password!=null">and password = #{password }</if>
            <if test="age!=0"> and age = #{age}</if>
        </where>
</select>

where标签可以自动判断是否要删除语句块中的and关键字,如果检测到where直接跟and拼接,则自动删除and,通常情况下if和where结合起来使用。

  • ⭐choose、when标签
<select id="findByAccount" parameterType="com.oyrf.frist.Account" resultType="com.oyrf.frist.Account">
        select  * from t_account
        <where>
            <choose>
                <when test="id!=0">id=#{id}</when>
                <when test="username!=null">and username=#{username</when>
                <when test="password!=null">andpassword=#{password</when>
                <when test="age!=0">and age=#{age}</when>
            </choose>
        </where>
</select>
  • ⭐trim标签
    trim标签中的prefix和suffix属性会被用于生成实际的SQL语句,会和标签内部的语句进行拼接,如果语句前后出现了prefixOverrides或者suffixOverrides属性中指定的值,MyBatis框架自动将其删除。
    <select id="findByAccount" parameterType="com.oyrf.frist.Account" resultType="com.oyrf.frist.Account">
        select  * from t_account
        <trim prefix="where" prefixOverrides="and">
            <if test="id!=0">id=#{id}</if>
            <if test="username!=null">and username=#{username}</if>
            <if test="password!=null">and password=#{password}</if>
            <if test="age!=null">and age=#{age}</if>
        </trim>
    </select>
  • ⭐set标签
    set标签用于update操作,会自动根据参数选择生成SQL语句。
    <update id="update" parameterType="com.oyrf.frist.Account">
        update t_account
        <set>
            <if test="username!=null">username=#{username}</if>
            <if test="password!=null">password=#{password}</if>
            <if test="age!=0">age=#{age}</if>
        where id=#{id}
        </set>
    </update>
  • ⭐foreach标签
    foreach标签可以迭代生成一系列的值,这个标签主要用于SQL的in语句。
    <select id="findByIds" parameterType="com.oyrf.frist.Account" resultType="com.oyrf.frist.Account">
        select * from t_account
        <where>
            <foreach collection="ids" open="id in(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </where>
    </select>

Account.java

    private List<Long> ids;

AccountRepository.java

    public List<Account> findByIds(Account account);

Test5.java

        InputStream inputStream = Test5.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Account account = new Account();
        List<Long> ids =new ArrayList<Long>();
        ids.add(2L);
        ids.add(3L);
        ids.add(5L);
        account.setIds(ids);
        AccountRepository accountRepository = sqlSession.getMapper(AccountRepository.class);
        System.out.println( accountRepository.findByIds(account));
        sqlSession.commit();
        sqlSession.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OYBox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值