Mybatis-plus使用wrapper多表內(nèi)連接左連接查詢方式
一、先放成功的方法
jar包:mybatis-plus-extenaion-3.4.0
<!--引入MyBatisPlus依賴-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>1.DAO層,使用注解方式
用inner join [表名] on [條件]關(guān)聯(lián)多表,用${ew.customSqlSegment}表示wrapper的where條件
public interface WReqMstMapper extends BaseMapper<WReqMst>{
@Select("SELECT mst.*,wh1.whname as REQWHNAME,wh2.whname as BYREQWHNAME ,comp.companyname as DEPTNAME FROM w_req_mst mst inner join gen_company comp on mst.deptid=comp.companyid left join gen_wh_def wh1 on mst.reqwhid=wh1.whid inner join gen_wh_def wh2 on mst.byreqwhid=wh2.whid ${ew.customSqlSegment}")
IPage<WReqMst> mlist(IPage<WReqMst> page,@Param(Constants.WRAPPER)Wrapper<WReqMst>wrapper);
}2.Service層
public interface IWReqMstService extends IService<WReqMst> {
IPage<WReqMst> getPageList(WReqMstListDto pageListDto);
}這里可以不用lambda(),可以直接new QueryWrapper<>()來(lái)添加條件。
@Service
public class WReqMstServiceImpl extends BaseService<WReqMstMapper, WReqMst> implements IWReqMstService {
private IWReqDtlService wReqDtlService;
@Resource
private WReqMstMapper getWReqMstMapper;
@Override
public IPage<WReqMst> getPageList(WReqMstListDto pageListDto) {
Wrapper<WReqMst> wrapper = Wrappers.<WReqMst>query().lambda()
.like(!StringUtil.isEmpty(pageListDto.getHISREQNO()), WReqMst::getHisreqno, pageListDto.getHISREQNO())
.ge(!StringUtil.isEmpty(pageListDto.getINPUTDATE_Start()), WReqMst::getInputdate, DateTimeUtil.minForTime(pageListDto.getINPUTDATE_Start(), "datetime"))
.le(!StringUtil.isEmpty(pageListDto.getINPUTDATE_End()), WReqMst::getInputdate, DateTimeUtil.maxForTime(pageListDto.getINPUTDATE_End(), "datetime"))
.eq(!StringUtil.isEmpty(pageListDto.getREQTYPE()), WReqMst::getReqtype, pageListDto.getREQTYPE());
return getWReqMstMapper.mlist(ConventPage.getPage(pageListDto), wrapper);//this.page(ConventPage.getPage(pageListDto), wrapper);
}
}3.controller層
@GetMappin
public Response<PageOutput<WReqMstListVo>> getPageList(WReqMstListDto listDto) {
IPage<WReqMst> page = wReqMstService.getPageList(listDto);
List<WReqMstListVo> records = BeanUtil.copyList(page.getRecords(), WReqMstListVo.class);//分頁(yè)。wrapper自帶
return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
}二、未解決
如果不用注解方式,查列表數(shù)據(jù)和有條件的查數(shù)據(jù)我沒(méi)法同時(shí)存在,我不知道如何同時(shí)實(shí)現(xiàn)。
先用association進(jìn)行了一對(duì)一表連接,其他代碼和上面一樣。但是xml文件按方法只能無(wú)條件查詢,wrapper加上的條件沒(méi)有進(jìn)入xml文件。。。
如果在下面的<select>中加上${ew.customSqlSegment},則可以有條件的查詢,但是沒(méi)有使用wrapper添加條件,則結(jié)果為空。
是不是要加上<if test=".....">${ew.customSqlSegment}</if>?
<resultMap id="wReqMstResultMap" type="WReqMst">
<id column="REQMSTID" property="reqmstid"/>
<result column="INPUTDATE" property="inputdate"/>
<result column="DEPTID" property="deptid"/>
<result column="REQWHID" property="reqwhid"/>
<result column="BYREQWHID" property="byreqwhid"/>
<result column="MEMO" property="memo"/>
<association property="deptname" column="DEPTID" select="com.xjrsoft.module.customerTwo.AppManage.genCompany.mapper.GenCompanyMapper.selectNameById"/>
<association property="reqwhname" column="REQWHID" select="com.xjrsoft.module.customerTwo.AppManage.genWh.mapper.GenWhDefMapper.selectNameById"/>
<association property="byreqwhname" column="BYREQWHID" select="com.xjrsoft.module.customerTwo.AppManage.genWh.mapper.GenWhDefMapper.selectNameById"/>
</resultMap>
<select id="wReqMstList" resultMap="wReqMstResultMap">
select *
from w_req_mst
</select>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
全面詳解java代碼重構(gòu)與設(shè)計(jì)模式
這篇文章主要為大家介紹了全面詳解java代碼重構(gòu)與設(shè)計(jì)模式的全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
springmvc使用REST出現(xiàn):Request?method?'PUT'?not?sup
這篇文章主要介紹了springmvc使用REST出現(xiàn):Request?method?'PUT'?not?supported問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏
這篇文章主要介紹了Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Java實(shí)現(xiàn)堆排序(Heapsort)實(shí)例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)堆排序(Heapsort)實(shí)例代碼,有需要的朋友可以參考一下2013-12-12
Apache POI導(dǎo)出Excel遇NoClassDefFoundError的原因分析與解決方案
在日常的Java開(kāi)發(fā)中,我們經(jīng)常需要實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到Excel的功能,本文將簡(jiǎn)單介紹Apache POI導(dǎo)出Excel遇NoClassDefFoundError錯(cuò)誤的原因與解決,希望對(duì)大家有所幫助2025-10-10
IDEA創(chuàng)建Servlet編寫HelloWorldServlet頁(yè)面詳細(xì)教程(圖文并茂)
在學(xué)習(xí)servlet過(guò)程中參考的教程是用eclipse完成的,而我在練習(xí)的過(guò)程中是使用IDEA的,在創(chuàng)建servlet程序時(shí)遇到了挺多困難,在此記錄一下,這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建Servlet編寫HelloWorldServlet頁(yè)面詳細(xì)教程的相關(guān)資料,需要的朋友可以參考下2023-10-10

