Java Fluent Mybatis 項(xiàng)目工程化與常規(guī)操作詳解流程篇 下
前言
接著上一篇:Java Fluent Mybatis 項(xiàng)目工程化與常規(guī)操作詳解流程篇 上
倉(cāng)庫(kù)地址:GitHub倉(cāng)庫(kù)
查詢
定義查詢請(qǐng)求體
package com.hy.fmp.dto.req; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; /** @Author huyi @Date 2021/10/20 19:37 @Description: 查詢條件 */ @Data @AllArgsConstructor @NoArgsConstructor @Builder public class TestFluentMybatisQueryReq { private String age; private String name; }
查詢寫法1
查詢接口方法定義
/** * 查詢接口1 * * @param queryReq 查詢請(qǐng)求 * @return 列表 */ List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq);
方法實(shí)現(xiàn),這里我們改用了mapper來(lái)實(shí)現(xiàn)一下官方給出的查詢語(yǔ)法模式。
package com.hy.fmp.service.Impl; import cn.hutool.core.util.StrUtil; import com.hy.fmp.dto.req.TestFluentMybatisQueryReq; import com.hy.fmp.fluent.dao.intf.TestFluentMybatisDao; import com.hy.fmp.fluent.entity.TestFluentMybatisEntity; import com.hy.fmp.fluent.helper.TestFluentMybatisMapping; import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper; import com.hy.fmp.fluent.wrapper.TestFluentMybatisQuery; import com.hy.fmp.service.IBaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; /** @Author huyi @Date 2021/10/20 17:10 @Description: 基礎(chǔ)操作接口實(shí)現(xiàn) */ @Slf4j @Service public class BaseServiceImpl implements IBaseService { @Autowired private TestFluentMybatisDao testFluentMybatisDao; @Autowired private TestFluentMybatisMapper testFluentMybatisMapper; @Override public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) { testFluentMybatisDao.saveOrUpdate(param); return param; } @Override public List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq) { return testFluentMybatisMapper.listEntity( new TestFluentMybatisQuery() .selectAll() .where .age() .eq(queryReq.getAge()) .and .name() .eq(queryReq.getName()) .end()); } }
control層方法定義
@ApiOperation(value = "查詢數(shù)據(jù)1", notes = "查詢數(shù)據(jù)1") @RequestMapping(value = "/query1", method = RequestMethod.POST) @ResponseBody public Result<List<TestFluentMybatisEntity>> query1( @RequestBody TestFluentMybatisQueryReq queryReq) { try { return Result.ok(baseService.query1(queryReq)); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null); } }
調(diào)試一下接口
一眼望去貌似沒(méi)問(wèn)題,但是長(zhǎng)期后端開(kāi)發(fā)的朋友應(yīng)該能看出來(lái),這個(gè)實(shí)現(xiàn)方式如果一旦age或者name參數(shù)為空的話,那么肯定查不出結(jié)果。因?yàn)榘凑照Z(yǔ)句的寫法,會(huì)強(qiáng)制比較age和name兩個(gè)參數(shù)。
我們將其中一個(gè)參數(shù)設(shè)置為空字符串試試看。
不出意料?,F(xiàn)在我可以就該方法做調(diào)整,參數(shù)判斷然后替換select語(yǔ)句,為了更優(yōu)雅的實(shí)現(xiàn),我去官方文檔再找找。
查詢寫法2
查詢方法2
package com.hy.fmp.service.Impl; import cn.hutool.core.util.StrUtil; import com.hy.fmp.dto.req.TestFluentMybatisQueryReq; import com.hy.fmp.fluent.dao.intf.TestFluentMybatisDao; import com.hy.fmp.fluent.entity.TestFluentMybatisEntity; import com.hy.fmp.fluent.helper.TestFluentMybatisMapping; import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper; import com.hy.fmp.fluent.wrapper.TestFluentMybatisQuery; import com.hy.fmp.service.IBaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; /** @Author huyi @Date 2021/10/20 17:10 @Description: 基礎(chǔ)操作接口實(shí)現(xiàn) */ @Slf4j @Service public class BaseServiceImpl implements IBaseService { @Autowired private TestFluentMybatisDao testFluentMybatisDao; @Autowired private TestFluentMybatisMapper testFluentMybatisMapper; @Override public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) { testFluentMybatisDao.saveOrUpdate(param); return param; } @Override public List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq) { return testFluentMybatisMapper.listEntity( new TestFluentMybatisQuery() .selectAll() .where .age() .eq(queryReq.getAge()) .and .name() .eq(queryReq.getName()) .end()); } @Override public List<TestFluentMybatisEntity> query2(TestFluentMybatisQueryReq queryReq) { return testFluentMybatisMapper.listByMap( true, new HashMap<String, Object>() { { if (!StrUtil.hasEmpty(queryReq.getAge())) { this.put(TestFluentMybatisMapping.age.column, queryReq.getAge()); } if (!StrUtil.hasEmpty(queryReq.getName())) { this.put(TestFluentMybatisMapping.name.column, queryReq.getName()); } } }); } }
代碼說(shuō)明
我對(duì)比了一下官方文檔的寫法,發(fā)現(xiàn)我這個(gè)版本的fm該listByMap方法多一個(gè)isColumn的布爾型參數(shù)。所以我追了一下源碼。
只影響錯(cuò)誤打印,主要就是設(shè)置的map參數(shù)必須要是列名或者實(shí)體對(duì)象內(nèi)的參數(shù)名。就不管了。
驗(yàn)證一下
沒(méi)什么問(wèn)題,還是可以查出來(lái)。
新問(wèn)題
但是按照這個(gè)查詢方法,如果兩個(gè)值都傳空字符串會(huì)查出全表數(shù)據(jù)嗎?
驗(yàn)證一下
咳咳,報(bào)錯(cuò)了。
所以我還是老老實(shí)實(shí)先把代碼參數(shù)判空優(yōu)化一下。
package com.hy.fmp.service.Impl; import cn.hutool.core.util.StrUtil; import com.hy.fmp.dto.req.TestFluentMybatisQueryReq; import com.hy.fmp.fluent.dao.intf.TestFluentMybatisDao; import com.hy.fmp.fluent.entity.TestFluentMybatisEntity; import com.hy.fmp.fluent.helper.TestFluentMybatisMapping; import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper; import com.hy.fmp.fluent.wrapper.TestFluentMybatisQuery; import com.hy.fmp.service.IBaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; /** @Author huyi @Date 2021/10/20 17:10 @Description: 基礎(chǔ)操作接口實(shí)現(xiàn) */ @Slf4j @Service public class BaseServiceImpl implements IBaseService { @Autowired private TestFluentMybatisDao testFluentMybatisDao; @Autowired private TestFluentMybatisMapper testFluentMybatisMapper; @Override public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) { testFluentMybatisDao.saveOrUpdate(param); return param; } @Override public List<TestFluentMybatisEntity> query1(TestFluentMybatisQueryReq queryReq) { return testFluentMybatisMapper.listEntity( new TestFluentMybatisQuery() .selectAll() .where .age() .eq(queryReq.getAge()) .and .name() .eq(queryReq.getName()) .end()); } @Override public List<TestFluentMybatisEntity> query2(TestFluentMybatisQueryReq queryReq) { if (StrUtil.hasEmpty(queryReq.getAge()) && StrUtil.hasEmpty(queryReq.getName())) { return testFluentMybatisMapper.listEntity(new TestFluentMybatisQuery().selectAll()); } return testFluentMybatisMapper.listByMap( true, new HashMap<String, Object>() { { if (!StrUtil.hasEmpty(queryReq.getAge())) { this.put(TestFluentMybatisMapping.age.column, queryReq.getAge()); } if (!StrUtil.hasEmpty(queryReq.getName())) { this.put(TestFluentMybatisMapping.name.column, queryReq.getName()); } } }); } }
驗(yàn)證一下
刪
添加通過(guò)ID刪除數(shù)據(jù)的接口方法
/** * 刪除接口 * * @param id id */ void deleteById(Integer id);
實(shí)現(xiàn)接口方法
@Override public void deleteById(Integer id) { testFluentMybatisMapper.deleteById(id); }
驗(yàn)證一下
刪除成功
總結(jié)
這兩篇文章主要是將之前的項(xiàng)目進(jìn)行工程化改造,增加了文檔、接口等一些列常規(guī)化操作。實(shí)現(xiàn)了數(shù)據(jù)庫(kù)表的基本增刪改查功能。其他的功能會(huì)在之后慢慢更新,fm融合了很多其他orm框架的東西,需要慢慢摸索摸索。
如果本文對(duì)你有幫助,請(qǐng)點(diǎn)個(gè)贊支持一下吧。
到此這篇關(guān)于Java Fluent Mybatis 項(xiàng)目工程化與常規(guī)操作詳解流程篇 下的文章就介紹到這了,更多相關(guān)Java Fluent Mybatis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java Fluent Mybatis實(shí)戰(zhàn)之構(gòu)建項(xiàng)目與代碼生成篇上
- Java Fluent Mybatis實(shí)戰(zhàn)之構(gòu)建項(xiàng)目與代碼生成篇下
- Fluent Mybatis實(shí)現(xiàn)環(huán)境隔離和租戶隔離
- Fluent Mybatis 批量更新的使用
- springboot 整合fluent mybatis的過(guò)程,看這篇夠了
- Fluent MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL
- Fluent Mybatis快速入門詳細(xì)教程
- Fluent Mybatis零xml配置實(shí)現(xiàn)復(fù)雜嵌套查詢
- Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一
相關(guān)文章
一篇文章帶你玩轉(zhuǎn)Spring bean的終極利器
這篇文章主要給大家介紹了關(guān)于玩轉(zhuǎn)Spring bean的終極利器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring bean具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05淺析java實(shí)現(xiàn)數(shù)據(jù)加密問(wèn)題
本文通過(guò)實(shí)例代碼給大家介紹了java實(shí)現(xiàn)數(shù)據(jù)加密問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-11-11SpringBoot配置Redis自定義過(guò)期時(shí)間操作
這篇文章主要介紹了SpringBoot配置Redis自定義過(guò)期時(shí)間操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Springboot+WebSocket+Netty實(shí)現(xiàn)在線聊天/群聊系統(tǒng)
這篇文章主要實(shí)現(xiàn)在好友添加、建群、聊天對(duì)話、群聊功能,使用Java作為后端語(yǔ)言進(jìn)行支持,界面友好,開(kāi)發(fā)簡(jiǎn)單,文章中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-08-08使用Mybatis-Plus實(shí)現(xiàn)對(duì)象屬性自動(dòng)填充功能
這篇文章主要介紹了如何使用Mybatis-Plus實(shí)現(xiàn)對(duì)象屬性自動(dòng)填充功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,感興趣的朋友們下面隨著小編來(lái)一起來(lái)學(xué)習(xí)吧2024-01-01SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空的問(wèn)題
這篇文章主要介紹了SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10springMVC發(fā)送郵件的簡(jiǎn)單實(shí)現(xiàn)
本篇文章主要介紹了springMVC發(fā)送郵件的簡(jiǎn)單實(shí)現(xiàn) ,主要是利用利用javax.mail發(fā)送郵件,圖片與附件都可發(fā)送,有興趣的可以了解一下2017-04-04