Mybatis-plus使用wrapper多表内连接左连接查询方式
一、先放成功的方法
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 [条件]关联多表,用${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<>()来添加条件。
@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);//分页。wrapper自带
return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
}二、未解决
如果不用注解方式,查列表数据和有条件的查数据我没法同时存在,我不知道如何同时实现。
先用association进行了一对一表连接,其他代码和上面一样。但是xml文件按方法只能无条件查询,wrapper加上的条件没有进入xml文件。。。
如果在下面的<select>中加上${ew.customSqlSegment},则可以有条件的查询,但是没有使用wrapper添加条件,则结果为空。
是不是要加上<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>总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
CodeGPT + IDEA + DeepSeek如何在IDEA中引入DeepS
文章介绍了如何在IDEA中使用CodeGPT和DeepSeek插件实现AI智能开发,具体内容包括安装步骤、配置APIkey和参数设置等,本文通过图文并茂的形式给大家介绍的非常详细,感兴趣的朋友一起看看吧2025-02-02
基于SpringBoot与AES实现接口响应数据加密的代码示例
最近项目中为了保证数据安全,要求接口对关键响应数据,比如敏感信息(如用户信息、权限数据、业务关键内容等)必须加密传输,所以,本文将围绕一个实际的SpringBoot应用场景,基于Hutool工具包中的AES加密能力,封装一个 接口响应数据自动加密的解决方案2025-08-08


最新评论