Mybatis-plus使用wrapper多表内连接左连接查询

文章展示了如何利用mybatis-plus的注解进行多表联查,包括DAO层的SQL编写,Service层的条件构造,以及Controller层的数据返回。同时,文章提出了在不使用注解的情况下,如何在XML文件中处理查询条件的问题,提到了可能的解决方案。
摘要由CSDN通过智能技术生成

一.先放成功的方法

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>

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus 中,可以使用 QueryWrapper 来进行自定义查询表。QueryWrapperMyBatis-Plus 提供的一个查询条件构造器,可以帮助我们快速构建查询条件。 以下是一个示例代码,演示如何使用 QueryWrapper 进行自定义查询表: ```java // 引入必要的类 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.core.metadata.IPage; // 创建 QueryWrapper 对象 QueryWrapper<User> queryWrapper = new QueryWrapper<>(); // 添加查询条件 queryWrapper.eq("age", 25); // 年龄等于 25 queryWrapper.like("name", "Tom"); // 名字包含 "Tom" // 自定义查询表 queryWrapper.table("custom_table"); // 执行查询 List<User> userList = userMapper.selectList(queryWrapper); // 打印结果 for (User user : userList) { System.out.println(user); } ``` 在上面的示例中,我们首先创建了一个 QueryWrapper 对象,并通过 `eq` 和 `like` 方法添加了两个查询条件。然后通过 `table` 方法指定了自定义的查询表名为 "custom_table"。最后,使用 selectList 方法执行查询,并将结果打印出来。 需要注意的是,QueryWrapper 还提供了很多其他的查询方法,如 `ne`、`gt`、`lt`、`ge`、`le` 等,可以根据具体需求选择使用。另外,还可以通过链式调用的方式添加多个查询条件。 希望以上信息能对您有所帮助!如有更多问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值