mybatis-动态sql语句-if用法

   上一篇,初步了解了一下mybatis----mybatis-简介,我们已经了解mybatis进行调用的过程,这一次主要来说如何进行动态sql语句的拼写,这次主要讲解if的应用。


查询:

咱们接着上一篇博客说,依然选用根据模糊查询或邮箱查询

    <select id="selectByUser" parameterType="com.tgb.mybatis.entity.SysUser" resultType="com.tgb.mybatis.entity.SysUser">
        select
            user_name "userName",
            user_password "userPassword",
            user_info "userInfo",
            head_img	"headImg",
            create_time "createTime"
        from
            sys_user
        where
            1=1
            <if test="userName != null and userName !=''">
              and user_name like CONCAT('%',#{userName},'%')
            </if>
            <if test="userEmail != null and userEmail != ''">
              and user_email =#{userEmail}
            </if>
    </select>

   if标签的必填选项test,test的属性是一个符合OGNL要求的判断表达式,表达式结果可以是true或false(除此之外非0的值为true,只有0为false)

   property != null 或 property == null 适用于任何类型的字段,用于判断属性值是否为空

   property != "" 或 property == "" 仅用于判String类型的字段,判断是否为空字符串

   and 和 or,当多个条件判断时,使用and或or进行连接

   1=1是为了避免当if条件判断都为false时,不出错,该条件始终成立。


更新:

    <update id="updateById">
        update sys_user
            SET
              <if test="userName != null and userName !=''">
                user_name = #{userName},
              </if>
              <if test="userPassword != null and userPassword !=''">
                user_password = #{userPassword},
              </if>
              <if test="userEmail != null and userEmail !=''">
                user_email = #{userEmail},
              </if>
              <if test="userInfo != null and userInfo !=''">
                user_info = #{userInfo},
              </if>
              <if test="headImg != null and headImg !=''">
                head_img = #{headImg},
              </if>
              <if test="createTime != null and createTime !=''">
                create_time =#{createTime},
              </if>
              id = #{id}
            where
                id = #{id}
    </update>

插入:

    <insert id="insert">
        insert into sys_user(
          user_name,
          user_password,
          <if test="userEmail !=null and userEmail != ''">
              user_email,
          </if>
          user_info,
          head_img,
          create_time
        )
        VALUES
        (#{userName},
         #{userPassword},
        <if test="userEmail != null and userEmail !=''">
            user_email = #{userEmail},
        </if>
        #{userInfo},
        #{headImg, jdbcType=BLOB},
        #{createTime,jdbcType=TIMESTAMP})
    </insert>

总结:

   综合上面的做法,我们看到if用法非常简单,关键是要确定是否所有判断都是false时的处理,查询我们可以用1=1这样的判断保证该条件始终正确,插入修改也一样需要注意这一点。





   

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
Mybatis-plus是在Mybatis基础上进行封装的一个框架,它简化了平时开发过程中对常用接口的调用,可以省去一些繁琐的操作。然而,对于一些更为复杂的查询,Mybatis-plus可能无法满足需求,此时就需要我们自定义SQL语句来实现。通过在入口类的MybatisSqlSessionFactoryBuilder#build方法中注入mybatis-plus自定义的动态配置xml文件,可以实现自定义SQL语句动态SQL的功能。具体的实现步骤如下: 1. 在应用启动时,在入口类的MybatisSqlSessionFactoryBuilder#build方法中将mybatis-plus的自定义动态配置xml文件注入到Mybatis中。 2. 在自定义的动态配置xml文件中,可以使用各种Mybatis-plus提供的方法来实现动态SQL的功能,比如IF标签、CHOOSE标签、FOREACH标签等。 3. 在自定义SQL语句中,可以结合Mybatis-plus的Wrapper类来实现条件查询,例如使用LambdaQueryWrapper来构建查询条件。 总结起来,Mybatis-plus提供了简化开发的接口,但对于一些更为复杂的查询,仍然需要我们自定义SQL语句动态SQL来实现。通过注入自定义的动态配置xml文件,并结合Mybatis-plus提供的方法和Wrapper类,可以实现更加灵活和高效的数据查询。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mybatis-plus/mybatis 自定义 sql 语句、动态 sql](https://blog.csdn.net/CREATE_17/article/details/109117091)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Mybatis Plus实现动态SQL语句的原理,你知道吗?](https://blog.csdn.net/weixin_38405253/article/details/119880820)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值