经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
Fluent Mybatis学习之Update语法实践
来源:jb51  时间:2021/11/24 11:07:42  对本文有异议

前言

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

本项目Github地址:项目仓库

数据准备

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

Update语法

简单的写法

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

接口层代码添加

  1. /**
  2. * 简单的更新语法
  3. *
  4. * @return
  5. */
  6. Integer updateSimple();

实现类代码添加

  1. @Override
  2. public Integer updateSimple() {
  3. return testFluentMybatisMapper.updateBy(
  4. new TestFluentMybatisUpdate()
  5. .set
  6. .name()
  7. .is("何九")
  8. .set
  9. .age()
  10. .isNull()
  11. .end()
  12. .where
  13. .id()
  14. .eq(2)
  15. .end());
  16. }

控制层代码添加

  1. @Autowired private IUpdateService updateService;
  2. @ApiOperation(value = "简单语法", notes = "简单语法")
  3. @RequestMapping(value = "/simple", method = RequestMethod.GET)
  4. @ResponseBody
  5. public Result<Integer> updateSimple() {
  6. try {
  7. return Result.ok(updateService.updateSimple());
  8. } catch (Exception exception) {
  9. return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
  10. }
  11. }

验证一下

代码说明

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

  1. 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` = ?
  2. 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)
  3. 2021-11-23 13:41:20.470 DEBUG 20820 --- [nio-8090-exec-1] c.h.f.f.m.T.updateBy : <== Updates: 1

UpdateByEntity根据表实体更新数据

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

接口层代码添加

  1. /**
  2. * 根据实体更新语法
  3. *
  4. * @param req 实体参数
  5. * @return
  6. */
  7. Integer updateByEntity(TestFluentMybatisEntity req);

实现类代码添加

  1. @Override
  2. public Integer updateByEntity(TestFluentMybatisEntity req) {
  3. return testFluentMybatisMapper.updateBy(
  4. new TestFluentMybatisUpdate()
  5. .set
  6. .byEntity(req, Ref.Field.TestFluentMybatis.name, Ref.Field.TestFluentMybatis.age)
  7. .end()
  8. .where
  9. .id()
  10. .eq(2)
  11. .end());
  12. }

控制层代码添加

  1. @Autowired private IUpdateService updateService;
  2. @ApiOperation(value = "根据实体更新语法", notes = "根据实体更新语法")
  3. @RequestMapping(value = "/updateByEntity", method = RequestMethod.POST)
  4. @ResponseBody
  5. public Result<Integer> updateByEntity(@RequestBody TestFluentMybatisEntity req) {
  6. try {
  7. return Result.ok(updateService.updateByEntity(req));
  8. } catch (Exception exception) {
  9. return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
  10. }
  11. }

验证一下

代码说明

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

  1. 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` = ?
  2. 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)
  3. 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是只排除选择的字段。

接口层代码添加

  1. /**
  2. * 根据排除项更新语法
  3. *
  4. * @param req 实体参数
  5. * @return
  6. */
  7. Integer updateByExclude(TestFluentMybatisEntity req);

实现类代码添加

  1. @Override
  2. public Integer updateByExclude(TestFluentMybatisEntity req) {
  3. return testFluentMybatisMapper.updateBy(
  4. new TestFluentMybatisUpdate()
  5. .set
  6. .byExclude(req, TestFluentMybatisEntity::getAge, TestFluentMybatisEntity::getDelFlag)
  7. .end()
  8. .where
  9. .id()
  10. .eq(2)
  11. .end());
  12. }

控制层代码添加

  1. @Autowired private IUpdateService updateService;
  2. @ApiOperation(value = "根据排除项更新语法", notes = "根据排除项更新语法")
  3. @RequestMapping(value = "/updateByExclude", method = RequestMethod.POST)
  4. @ResponseBody
  5. public Result<Integer> updateByExclude(@RequestBody TestFluentMybatisEntity req) {
  6. try {
  7. return Result.ok(updateService.updateByExclude(req));
  8. } catch (Exception exception) {
  9. return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
  10. }
  11. }

验证一下

代码说明

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

  1. 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` = ?
  2. 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)
  3. 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即可,这个方法还是很好用的,简化代码。

接口层代码添加

  1. /**
  2. * 使用applyFunc更新语法
  3. *
  4. * @return
  5. */
  6. Integer updateByApplyFunc();

实现类代码添加

  1. @Override
  2. public Integer updateByApplyFunc() {
  3. return testFluentMybatisMapper.updateBy(
  4. new TestFluentMybatisUpdate()
  5. .set
  6. .name()
  7. .applyFunc("concat(name, ?)", "_男")
  8. .set
  9. .age()
  10. .applyFunc("age+5")
  11. .end()
  12. .where
  13. .id()
  14. .eq(2)
  15. .end());
  16. }

控制层代码添加

  1. @Autowired private IUpdateService updateService;
  2. @ApiOperation(value = "使用applyFunc更新语法", notes = "使用applyFunc更新语法")
  3. @RequestMapping(value = "/updateByApplyFunc", method = RequestMethod.GET)
  4. @ResponseBody
  5. public Result<Integer> updateByApplyFunc() {
  6. try {
  7. return Result.ok(updateService.updateByApplyFunc());
  8. } catch (Exception exception) {
  9. return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
  10. }
  11. }

验证一下

代码说明

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

  1. 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` = ?
  2. 2021-11-23 15:31:09.782 DEBUG 20820 --- [nio-8090-exec-8] c.h.f.f.m.T.updateBy : ==> Parameters: _男(String), 2(Integer)
  3. 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的内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号