MyBatisPlus 自定义Sql
利用MybatisPlus的Wrapper来构建复杂的Where条件,然后自定义Sql语句中剩余的部分
1.基于Wrapper构建where条件
LambdaQueryWrapper<ShippingOrderEntity> queryWrapper = new LambdaQueryWrapper<>(); //姓名-模糊 queryWrapper.like(StringUtils.isNotBlank(shippingOrderListDTO.getUserName()),ShippingOrderEntity::getUserName,shippingOrderListDTO.getUserName()); //手机号-模糊 queryWrapper.like(StringUtils.isNotBlank(shippingOrderListDTO.getPhone()),ShippingOrderEntity::getPhone,shippingOrderListDTO.getPhone()); //配送方式-精确 queryWrapper.eq(null != shippingOrderListDTO.getTakeType(),ShippingOrderEntity::getTakeType,shippingOrderListDTO.getTakeType()); //运单状态-精确 queryWrapper.eq(null != shippingOrderListDTO.getStatus(),ShippingOrderEntity::getStatus,shippingOrderListDTO.getStatus());\
shippingOrderMapper.queryShippingOrderList(queryWrapper);
2.在mapper方法中用Param注解声明wrapper变量名称,必须是ew
List<ShippingOrderVO> queryShippingOrderList(@Param("ew") LambdaQueryWrapper<ShippingOrderEntity> queryWrapper);
3.自定义Sql,并使用Wrapper条件
<select id="queryShippingOrderList" resultType="com.fancy.portal.bean.vo.ShippingOrderVO"> select <include refid="shippingOrderColumn" /> from GZH_SHIPPING GS <where> ${ew.sqlSegment} </where> </select>