Fluent Mybatis學(xué)習(xí)之Update語法實(shí)踐
前言
本篇文章主要針對update語法進(jìn)行驗(yàn)證。
本項(xiàng)目Github地址:項(xiàng)目倉庫
數(shù)據(jù)準(zhǔn)備
還是用之前在數(shù)據(jù)庫存的數(shù)據(jù),數(shù)據(jù)如下:
Update語法
簡單的寫法
fm的update簡單寫法可以直接使用is()來對表字段進(jìn)行賦值,如果需要將字段設(shè)置為Null的話,直接使用isNull()方法。
接口層代碼添加
/** * 簡單的更新語法 * * @return */ Integer updateSimple();
實(shí)現(xiàn)類代碼添加
@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); } }
驗(yàn)證一下
代碼說明
1、可以看一下代碼執(zhí)行的具體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根據(jù)表實(shí)體更新數(shù)據(jù)
fm支持使用表實(shí)體進(jìn)行數(shù)據(jù)更新,但是有一些限制,具體看我后面的代碼說明。
接口層代碼添加
/** * 根據(jù)實(shí)體更新語法 * * @param req 實(shí)體參數(shù) * @return */ Integer updateByEntity(TestFluentMybatisEntity req);
實(shí)現(xiàn)類代碼添加
@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 = "根據(jù)實(shí)體更新語法", notes = "根據(jù)實(shí)體更新語法") @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); } }
驗(yàn)證一下
代碼說明
1、先看看sql的執(zhí)行語句,如下圖:
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方法支持傳遞字段參數(shù),很好理解,只更新傳遞的字段值,不論是否為null。官方原話為:指定字段時,更新指定的字段(包括null字段), 但指定主鍵無效。
4、我在方法中選定了name、age兩個字段,所以只更新這兩項(xiàng)。
UpdateByExclude根據(jù)表實(shí)體排除更新數(shù)據(jù)
fm對排除字段情有獨(dú)鐘,簡單理解一下,就是在設(shè)置字段的時候,byEntity是只選定選擇的字段,byExclude是只排除選擇的字段。
接口層代碼添加
/** * 根據(jù)排除項(xiàng)更新語法 * * @param req 實(shí)體參數(shù) * @return */ Integer updateByExclude(TestFluentMybatisEntity req);
實(shí)現(xiàn)類代碼添加
@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 = "根據(jù)排除項(xiàng)更新語法", notes = "根據(jù)排除項(xiàng)更新語法") @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); } }
驗(yàn)證一下
代碼說明
1、先看看sql的執(zhí)行語句,如下圖:
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、這里需要注意,實(shí)體中沒有給create_time設(shè)值,所以會把其設(shè)置為null,官方原話為:未指定字段列表時, 更新除主鍵外字段(包括null字段)
3、而我指定了字段age和del_flag,但是并沒有作用,這就是byExclude的作用,官方原話為:排除指定字段。
applyFunc
可以在更新語法中,增加對字段的函數(shù)操作,使用applyFunc即可,這個方法還是很好用的,簡化代碼。
接口層代碼添加
/** * 使用applyFunc更新語法 * * @return */ Integer updateByApplyFunc();
實(shí)現(xiàn)類代碼添加
@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); } }
驗(yàn)證一下
代碼說明
1、先看看sql的執(zhí)行語句,如下圖:
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歲。
總結(jié)
總的來看,fm提供的方法還是很優(yōu)越的,后面會繼續(xù)把fm剩下的功能調(diào)試完。
到此這篇關(guān)于FluentMybatis學(xué)習(xí)之Update語法實(shí)踐的文章就介紹到這了,更多相關(guān)FluentMybatis的內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Beanutils.copyproperties實(shí)體類對象不一致的問題
這篇文章主要介紹了解決Beanutils.copyproperties實(shí)體類對象不一致的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java中finally和return的關(guān)系實(shí)例解析
這篇文章主要介紹了Java中finally和return的關(guān)系實(shí)例解析,總結(jié)了二者的關(guān)系,然后分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02簡述springboot及springboot cloud環(huán)境搭建
這篇文章主要介紹了簡述springboot及springboot cloud環(huán)境搭建的方法,包括spring boot 基礎(chǔ)應(yīng)用環(huán)境搭建,需要的朋友可以參考下2017-07-07Springboot根據(jù)配置文件動態(tài)注入接口實(shí)現(xiàn)類詳解
這篇文章主要介紹了Springboot根據(jù)配置文件動態(tài)注入接口實(shí)現(xiàn)類詳解,具有很好的參考價值,希望對大家有所幫助,需要的朋友可以參考下,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10java實(shí)現(xiàn)電腦定時關(guān)機(jī)的方法
這篇文章主要介紹了java實(shí)現(xiàn)電腦定時關(guān)機(jī)的方法,首先通過java注冊windows服務(wù)程序,再以一個簡單的java程序?qū)崿F(xiàn)定時關(guān)機(jī)的功能,非常具有實(shí)用價值,需要的朋友可以參考下2014-11-11spring boot之SpringApplication 事件監(jiān)聽
這篇文章主要介紹了spring boot之SpringApplication 事件監(jiān)聽,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03