關于Mybatis-Plus?Wrapper是否應該出現(xiàn)在Servcie類中
一、問題
最近在做代碼重構,代碼工程采用了Controller/Service/Dao分層架構,Dao層使用了Mybatis-Plus框架。
在查看Service層時發(fā)現(xiàn)如下代碼:
@Service public class SampleServiceImpl implements SampleService { @Resource private SampleMapper sampleMapper; @Override public SampleTo findById(Long id) { Sample sample = this.sampleMapper.selectOne(Wrappers.<Sample>lambdaQuery() //僅查詢指定的column .select(Sample::getId, Sample::getName, Sample::getDate) //查詢條件 id = #{id} .eq(Sample::getId, id) ); return (SampleTo) BaseAssembler.populate(sample, new SampleTo()); } @Override public PageInfo<SampleTo> findListByDto(SampleQueryDto sampleQueryDto) { //開啟分頁 PageHelperUtil.startPage(sampleQueryDto); //查詢分頁列表 List<Sample> sampleList = this.sampleMapper.selectList(Wrappers.<Sample>lambdaQuery() //查詢條件 id = #{id} .eq(Objects.nonNull(sampleQueryDto.getId()), Sample::getId, sampleQueryDto.getId()) //查詢條件 name like concat('%', #{name}, '%') .like(StringUtils.hasText(sampleQueryDto.getName()), Sample::getName, sampleQueryDto.getName()) //查詢條件 type = #{type} .eq(StringUtils.hasText(sampleQueryDto.getType()), Sample::getType, sampleQueryDto.getType()) //查詢條件 date >= #{startDate} .ge(Objects.nonNull(sampleQueryDto.getStartDate()), Sample::getDate, sampleQueryDto.getStartDate()) //查詢條件 date <= #{endDate} .le(Objects.nonNull(sampleQueryDto.getEndDate()), Sample::getDate, sampleQueryDto.getEndDate())); //轉換分頁結果 return PageHelperUtil.convertPageInfo(sampleList, SampleTo.class); } @Override public List<SampleTo> findListByCondition(String name, String type) { List<Sample> sampleList = this.sampleMapper.selectList(Wrappers.<Sample>lambdaQuery() //查詢條件 name like concat('%', #{name}, '%') .like(StringUtils.hasText(name), Sample::getName, name) //查詢條件 type = #{type} .eq(StringUtils.hasText(type), Sample::getType, type) ); return BaseAssembler.populateList(sampleList, SampleTo.class); } @Override public SampleDetailTo findDetailById(Long id) { return this.sampleMapper.findDetail(id); } @Override public Integer add(SampleAddDto sampleAddDto) { Sample sample = new Sample(); BaseAssembler.populate(sampleAddDto, sample); return this.sampleMapper.insert(sample); } }
dao層代碼:
public interface SampleMapper extends BaseMapper<Sample> { //SQL腳本通過XML進行定義 SampleDetailTo findDetail(@Param("id") Long id); }
如上Service代碼中直接使用Mybatis-Plus框架提供的Wrapper構造器,寫的時候是挺爽,不用再單獨為SampleMapper接口寫XML腳本了,直接在Service類中都完成了,但是我不推薦這種寫法。
分層架構的本意是通過分層來降低、隔離各層次的復雜度,
各層間僅通過接口進行通信,層間僅依賴抽象接口,不依賴具體實現(xiàn),
只要保證接口不變可以自由切換每層的實現(xiàn)。
上述Service類中直接引用了Dao層實現(xiàn)框架Mybatis-Plus中的Wrappers類,盡管Servcie層依賴了Dao層的Mapper接口,但是Mapper接口中的參數(shù)Wrapper卻是Dao層具體實現(xiàn)Mybatis-Plus所獨有的,試想我們現(xiàn)在Dao層用的Mybatis-Plus實現(xiàn),后續(xù)如果想將Dao層實現(xiàn)切換為Spring JPA,那Mybatis-Plus中Wrapper是不都要替換,那Servcie層中的相關Wrapper引用也都要進行替換,我們僅是想改變Dao實現(xiàn),卻不得不把Servcie層也進行修改。同時Service層本該是寫業(yè)務邏輯代碼的地方,但是卻耦合進了大量的Wrapper構造邏輯,代碼可讀性差,難以捕捉到核心業(yè)務邏輯。
二、優(yōu)化建議
那是不是Mybatis-Plus中的Wrapper就不能用了呢?我的答案是:能用,只是方式沒用對。
Wrapper絕對是個好東西,方便我們構造Sql,也可以將我們從繁瑣的XML腳本中解救出來,但是不能跨越層間界限。
優(yōu)化建議如下:
- 移除Servcie中的Wrapper使用
- Java8+之后接口提供了默認方法的支持,可通過給Dao層Mapper接口添加default方法使用Wrapper
- 單表相關的操作 - 通過Dao層Mapper接口的default方法直接使用Wrapper進行實現(xiàn),提高編碼效率
- 多表關聯(lián)的復雜操作 - 通過Dao層Mapper接口和XML腳本的方式實現(xiàn)
優(yōu)化后的Service層代碼如下:
@Service public class SampleServiceImpl implements SampleService { @Resource private SampleMapper sampleMapper; @Override public SampleTo findById(Long id) { Sample sample = this.sampleMapper.findInfoById(id); return (SampleTo) BaseAssembler.populate(sample, new SampleTo()); } @Override public SampleDetailTo findDetailById(Long id) { return this.sampleMapper.findDetail(id); } @Override public PageInfo<SampleTo> findListByDto(SampleQueryDto sampleQueryDto) { //開啟分頁 PageHelperUtil.startPage(sampleQueryDto); //查詢分頁列表 List<Sample> sampleList = this.sampleMapper.findList(sampleQueryDto); //轉換分頁結果 return PageHelperUtil.convertPageInfo(sampleList, SampleTo.class); } @Override public List<SampleTo> findListByCondition(String name, String type) { List<Sample> sampleList = this.sampleMapper.findListByNameAndType(name, type); return BaseAssembler.populateList(sampleList, SampleTo.class); } @Override public Integer add(SampleAddDto sampleAddDto) { Sample sample = new Sample(); BaseAssembler.populate(sampleAddDto, sample); return this.sampleMapper.insert(sample); } }
優(yōu)化后的Dao層代碼:
public interface SampleMapper extends BaseMapper<Sample> { default Sample findInfoById(Long id) { return this.selectOne(Wrappers.<Sample>lambdaQuery() //僅查詢指定的column .select(Sample::getId, Sample::getName, Sample::getDate) //查詢條件 id = #{id} .eq(Sample::getId, id) ); } default List<Sample> findList(SampleQueryDto sampleQueryDto) { return this.selectList(Wrappers.<Sample>lambdaQuery() //查詢條件 id = #{id} .eq(Objects.nonNull(sampleQueryDto.getId()), Sample::getId, sampleQueryDto.getId()) //查詢條件 name like concat('%', #{name}, '%') .like(StringUtils.hasText(sampleQueryDto.getName()), Sample::getName, sampleQueryDto.getName()) //查詢條件 type = #{type} .eq(StringUtils.hasText(sampleQueryDto.getType()), Sample::getType, sampleQueryDto.getType()) //查詢條件 date >= #{startDate} .ge(Objects.nonNull(sampleQueryDto.getStartDate()), Sample::getDate, sampleQueryDto.getStartDate()) //查詢條件 date <= #{endDate} .le(Objects.nonNull(sampleQueryDto.getEndDate()), Sample::getDate, sampleQueryDto.getEndDate()) ); } default List<Sample> findListByNameAndType(String name, String type) { return this.selectList(Wrappers.<Sample>lambdaQuery() //查詢條件 name like concat('%', #{name}, '%') .like(StringUtils.hasText(name), Sample::getName, name) //查詢條件 type = #{type} .eq(StringUtils.hasText(type), Sample::getType, type) ); } //SQL腳本通過XML進行定義) SampleDetailTo findDetail(@Param("id") Long id); }
優(yōu)化后的Servcie層完全移除了對Wrapper的依賴,將Servcie層和Dao層實現(xiàn)進行解耦,同時Dao層通過Java8+接口的默認方法同時支持Wrapper和XML的使用,整合編碼和XML腳本的各自優(yōu)勢。
三、Repository模式
經過優(yōu)化過后,Service層代碼確實清爽了許多,移除了Mybatis-Plus的Wrapper構造邏輯,使得Service層可以更專注于業(yè)務邏輯的實現(xiàn)。但是細心的小伙伴還是會發(fā)現(xiàn)Servcie層仍舊依賴了Mybatis的分頁插件PageHelper中的PageHelper類、PageInfo類,PageHelper插件也是技術綁定的(強綁定到Mybatis),既然我們們之前強調了Servcie層與Dao層間的界限,如此在Servcie層使用PageHelper也是越界了,例如后續(xù)如果切換Spring JPA,那PageHelper在Servcie層的相關的引用也都需要調整。
真正做到業(yè)務和技術解耦,可以參考DDD中的Repository(倉庫、資源庫)模式:
- 單獨定義通用的分頁查詢參數(shù)DTO、分頁查詢結果DTO(與具體技術解耦)
- 定義Repository接口,僅依賴聚合、通用分頁查詢參數(shù)DTO、分頁查詢結果DTO
- 定義Repository接口的實現(xiàn)類,具體實現(xiàn)可依賴如Mybatis、JPA等Dao框架,在Repository的具體實現(xiàn)類中完成轉換:
- 領域模型(聚合)<==> 數(shù)據(jù)實體
- 通用分頁查詢參數(shù)DTO、結果DTO <==> Dao框架的分頁參數(shù)、結果(如PageHelper、IPage等)
DDD映射到代碼層面,改動還是比較大的,所以在這次重構代碼的過程中并沒有真正采用DDD Repository模式,
而是僅從Servcie中移除Mybatis-Plus Wrapper便結束了,雖沒有完全將Service層與Dao層實現(xiàn)(PageHelper)解耦,
但在Service層移除Wrapper構造邏輯后,使得Service層代碼更清爽,可讀性更好了,重構過程的代碼改動量也在可接收的范圍內。
到此這篇關于Mybatis-Plus Wrapper應該出現(xiàn)在Servcie類中嗎?的文章就介紹到這了,更多相關Mybatis-Plus Wrapper內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java下3中XML解析 DOM方式、SAX方式和StAX方式
目前我知道的JAVA解析XML的方式有:DOM, SAX, StAX;如果選用這幾種,感覺還是有點麻煩;如果使用:JAXB(Java Architecture for XML Binding),個人覺得太方便了2013-04-04SpringCloud?服務注冊中的nacos實現(xiàn)過程
這篇文章主要介紹了SpringCloud?服務注冊之nacos實現(xiàn)過程,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03解決SpringBoot運行Test時報錯:SpringBoot Unable to find
這篇文章主要介紹了SpringBoot運行Test時報錯:SpringBoot Unable to find a @SpringBootConfiguration,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10spring boot項目沒有mainClass如何實現(xiàn)打包運行
這篇文章主要介紹了spring boot項目沒有mainClass如何實現(xiàn)打包運行,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Knife4j?3.0.3?整合SpringBoot?2.6.4的詳細過程
本文要講的是?Knife4j?3.0.3?整合SpringBoot?2.6.4,在SpringBoot?2.4以上的版本和之前的版本還是不一樣的,這個也容易導致一些問題,本文就這兩個版本的整合做一個實戰(zhàn)介紹2022-09-09