千家信息网

Fluent Mybatis中Update语法怎么用

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,小编给大家分享一下Fluent Mybatis中Update语法怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!数据准备还是用之前在数据库存的数据,数据如下:Update语法简单
千家信息网最后更新 2026年11月02日Fluent Mybatis中Update语法怎么用

小编给大家分享一下Fluent Mybatis中Update语法怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

    数据准备

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

    Update语法

    简单的写法

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

    接口层代码添加

    /** * 简单的更新语法 * * @return */Integer updateSimple();

    实现类代码添加

    @Overridepublic 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)@ResponseBodypublic Result 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);

    实现类代码添加

    @Overridepublic 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)@ResponseBodypublic Result 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);

    实现类代码添加

    @Overridepublic 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)@ResponseBodypublic Result 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();

    实现类代码添加

    @Overridepublic 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)@ResponseBodypublic Result 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岁。

    看完了这篇文章,相信你对"Fluent Mybatis中Update语法怎么用"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

    字段 代码 更新 语法 实体 数据 方法 原话 官方 接口 控制 验证 参数 语句 作用 写法 就是 时候 篇文章 还是 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 上海挚品互联网科技有限公司 深圳市人人互联网科技有限公司 深圳九邦互联网科技有限公司 上海挚品互联网科技 互联网科技公司怎么样 上海卓越科技 互联网 人人互联网科技是培训 深圳卡尔迅互联网科技有限公司怎么样 互联网科技公司招聘 互联网科技热点资讯 中国互联网有什么科技含量 燊美集互联网科技有限公司 华夏恒生科技与互联网科技 无锡工业互联网科技有限公司 北京市互联网新科技 广州互联网高科技取餐柜 广州梦谷互联网科技有限公司领导 科技互联网行业投资研究 互联网科技企业商标注意事项 东风日产互联网科技公司叫什么 咖啡 互联网科技 嘟嘟瓜互联网科技 深圳艾佳互联网科技 宁波青枫互联网科技有限公司 央行互联网金融科技公司 语蝶互联网科技有限公司 科技赋能视角下互联网保险发展 四川互联网展示科技馆设计 互联网科技的前景
    0