Fluent Mybatis学习之Update语法实践

2022-10-22,,,,

目录
  • 前言
  • 数据准备
  • update语法
    • 简单的写法
    • updatebyentity根据表实体更新数据
    • updatebyexclude根据表实体排除更新数据
    • applyfunc
  • 总结

    前言

    本篇文章主要针对update语法进行验证。

    本项目github地址:项目仓库

    数据准备

    还是用之前在数据库存的数据,数据如下:

    update语法

    简单的写法

    fm的update简单写法可以直接使用is()来对表字段进行赋值,如果需要将字段设置为null的话,直接使用isnull()方法。

    接口层代码添加

    /**
     * 简单的更新语法
     *
     * @return
     */
    integer updatesimple();

    实现类代码添加

    @override
    public integer updatesimple() {
      return testfluentmybatismapper.updateby(
          new testfluentmybatisupdate()
              .set
              .name()
              .is("何九")
              .set
              .age()
              .isnull()
              .end()
              .where
              .id()
              .eq(2)
              .end());
    }

    控制层代码添加

    @autowired private iupdateservice updateservice;
     
    @apioperation(value = "简单语法", notes = "简单语法")
    @requestmapping(value = "/simple", method = requestmethod.get)
    @responsebody
    public result<integer> updatesimple() {
      try {
        return result.ok(updateservice.updatesimple());
      } catch (exception exception) {
        return result.error(errorcode.base_error_code.getcode(), exception.getmessage(), null);
      }
    }

    验证一下

    代码说明

    1、可以看一下代码执行的具体sql,如下:

    2021-11-23 13:41:20.277 debug 20820 --- [nio-8090-exec-1] c.h.f.f.m.t.updateby                     : ==>  preparing: update `test_fluent_mybatis` set `name` = ?, `age` = ? where `id` = ?
    2021-11-23 13:41:20.464 debug 20820 --- [nio-8090-exec-1] c.h.f.f.m.t.updateby                     : ==> parameters: 何九(string), null, 2(integer)
    2021-11-23 13:41:20.470 debug 20820 --- [nio-8090-exec-1] c.h.f.f.m.t.updateby                     : <==    updates: 1

    updatebyentity根据表实体更新数据

    fm支持使用表实体进行数据更新,但是有一些限制,具体看我后面的代码说明。

    接口层代码添加

    /**
     * 根据实体更新语法
     *
     * @param req 实体参数
     * @return
     */
    integer updatebyentity(testfluentmybatisentity req);

    实现类代码添加

    @override
    public integer updatebyentity(testfluentmybatisentity req) {
      return testfluentmybatismapper.updateby(
          new testfluentmybatisupdate()
              .set
              .byentity(req, ref.field.testfluentmybatis.name, ref.field.testfluentmybatis.age)
              .end()
              .where
              .id()
              .eq(2)
              .end());
    }

    控制层代码添加

    @autowired private iupdateservice updateservice;
     
    @apioperation(value = "根据实体更新语法", notes = "根据实体更新语法")
    @requestmapping(value = "/updatebyentity", method = requestmethod.post)
    @responsebody
    public result<integer> updatebyentity(@requestbody testfluentmybatisentity req) {
      try {
        return result.ok(updateservice.updatebyentity(req));
      } catch (exception exception) {
        return result.error(errorcode.base_error_code.getcode(), exception.getmessage(), null);
      }
    }

    验证一下

    代码说明

    1、先看看sql的执行语句,如下图:

    2021-11-23 13:56:16.572 debug 20820 --- [nio-8090-exec-4] c.h.f.f.m.t.updateby                     : ==>  preparing: update `test_fluent_mybatis` set `name` = ?, `age` = ? where `id` = ?
    2021-11-23 13:56:16.573 debug 20820 --- [nio-8090-exec-4] c.h.f.f.m.t.updateby                     : ==> parameters: 李四(string), 29(integer), 2(integer)
    2021-11-23 13:56:16.580 debug 20820 --- [nio-8090-exec-4] c.h.f.f.m.t.updateby                     : <==    updates: 1

    2、这里需要注意,在使用byentity方法的时候,不会使用entity中的id作为判断的。官方原话为:未指定字段列表时, 更新除主键外非null字段。

    3、byentity方法支持传递字段参数,很好理解,只更新传递的字段值,不论是否为null。官方原话为:指定字段时,更新指定的字段(包括null字段), 但指定主键无效。

    4、我在方法中选定了name、age两个字段,所以只更新这两项。

    updatebyexclude根据表实体排除更新数据

    fm对排除字段情有独钟,简单理解一下,就是在设置字段的时候,byentity是只选定选择的字段,byexclude是只排除选择的字段。

    接口层代码添加

    /**
     * 根据排除项更新语法
     *
     * @param req 实体参数
     * @return
     */
    integer updatebyexclude(testfluentmybatisentity req);

    实现类代码添加

    @override
    public integer updatebyexclude(testfluentmybatisentity req) {
      return testfluentmybatismapper.updateby(
          new testfluentmybatisupdate()
              .set
              .byexclude(req, testfluentmybatisentity::getage, testfluentmybatisentity::getdelflag)
              .end()
              .where
              .id()
              .eq(2)
              .end());
    }

    控制层代码添加

    @autowired private iupdateservice updateservice;
     
    @apioperation(value = "根据排除项更新语法", notes = "根据排除项更新语法")
    @requestmapping(value = "/updatebyexclude", method = requestmethod.post)
    @responsebody
    public result<integer> updatebyexclude(@requestbody testfluentmybatisentity req) {
      try {
        return result.ok(updateservice.updatebyexclude(req));
      } catch (exception exception) {
        return result.error(errorcode.base_error_code.getcode(), exception.getmessage(), null);
      }
    }

    验证一下

    代码说明

    1、先看看sql的执行语句,如下图:

    2021-11-23 15:21:42.262 debug 20820 --- [nio-8090-exec-6] c.h.f.f.m.t.updateby                     : ==>  preparing: update `test_fluent_mybatis` set `name` = ?, `create_time` = ? where `id` = ?
    2021-11-23 15:21:42.266 debug 20820 --- [nio-8090-exec-6] c.h.f.f.m.t.updateby                     : ==> parameters: 李四(string), null, 2(integer)
    2021-11-23 15:21:42.271 debug 20820 --- [nio-8090-exec-6] c.h.f.f.m.t.updateby                     : <==    updates: 1

    2、这里需要注意,实体中没有给create_time设值,所以会把其设置为null,官方原话为:未指定字段列表时, 更新除主键外字段(包括null字段)

    3、而我指定了字段age和del_flag,但是并没有作用,这就是byexclude的作用,官方原话为:排除指定字段。

    applyfunc

    可以在更新语法中,增加对字段的函数操作,使用applyfunc即可,这个方法还是很好用的,简化代码。

    接口层代码添加

    /**
     * 使用applyfunc更新语法
     *
     * @return
     */
    integer updatebyapplyfunc();

    实现类代码添加

    @override
    public integer updatebyapplyfunc() {
      return testfluentmybatismapper.updateby(
          new testfluentmybatisupdate()
              .set
              .name()
              .applyfunc("concat(name, ?)", "_男")
              .set
              .age()
              .applyfunc("age+5")
              .end()
              .where
              .id()
              .eq(2)
              .end());
    }

    控制层代码添加

    @autowired private iupdateservice updateservice;
     
    @apioperation(value = "使用applyfunc更新语法", notes = "使用applyfunc更新语法")
    @requestmapping(value = "/updatebyapplyfunc", method = requestmethod.get)
    @responsebody
    public result<integer> updatebyapplyfunc() {
      try {
        return result.ok(updateservice.updatebyapplyfunc());
      } catch (exception exception) {
        return result.error(errorcode.base_error_code.getcode(), exception.getmessage(), null);
      }
    }

    验证一下

    代码说明

    1、先看看sql的执行语句,如下图:

    2021-11-23 15:31:09.772 debug 20820 --- [nio-8090-exec-8] c.h.f.f.m.t.updateby                     : ==>  preparing: update `test_fluent_mybatis` set `name` = concat(name, ?), `age` = age+5 where `id` = ?
    2021-11-23 15:31:09.782 debug 20820 --- [nio-8090-exec-8] c.h.f.f.m.t.updateby                     : ==> parameters: _男(string), 2(integer)
    2021-11-23 15:31:09.787 debug 20820 --- [nio-8090-exec-8] c.h.f.f.m.t.updateby                     : <==    updates: 1

    2、sql可以看出,将那么字段拼接了后缀"_男",同时年龄增加5岁。

    总结

    总的来看,fm提供的方法还是很优越的,后面会继续把fm剩下的功能调试完。

    到此这篇关于fluentmybatis学习之update语法实践的文章就介绍到这了,更多相关fluentmybatis的内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

    《Fluent Mybatis学习之Update语法实践.doc》

    下载本文的Word格式文档,以方便收藏与打印。