千家信息网

Fluent Mybatis中Update语法怎么用

发表于:2024-09-24 作者:千家信息网编辑
千家信息网最后更新 2024年09月24日,小编给大家分享一下Fluent Mybatis中Update语法怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!数据准备还是用之前在数据库存的数据,数据如下:Update语法简单
千家信息网最后更新 2024年09月24日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语法怎么用"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

    0