mybatis plus join 处理(ew.sqlSegment)连表查询

业务场景:

任务-分组表 tms_task_customer_group :task, group_id

分组表 cms_customer_group :id, group_name

返回:task_id, group_id, group_name

xml 配置

    <resultMap id="ExtendResultMap" type="com.fx.cloud.tiny.modules.tms.model.TmsTaskCustomerGroup">
        <id column="id" property="id" />
        <result column="merchant_id" property="merchantId" />
        <result column="task_id" property="taskId" />
        <result column="customer_group_id" property="customerGroupId" />
        <result column="create_time" property="createTime" />
        <result column="group_name" property="name"/>
    </resultMap>

    <sql id="Extend_Column_List">
        tms_task_customer_group.id, tms_task_customer_group.merchant_id, task_id, customer_group_id, tms_task_customer_group.create_time, group_name
    </sql>

    <select id="getDetailByTaskId" parameterType="List" resultMap="ExtendResultMap">
        select
        <include refid="Extend_Column_List"/>
        from tms_task_customer_group left join cms_customer_group on tms_task_customer_group.customer_group_id=cms_customer_group.id
        <where>
            ${ew.sqlSegment}
        </where>
    </select>

mapper

    List<TmsTaskCustomerGroup> getDetailByTaskId(@Param(Constants.WRAPPER) QueryWrapper wrapper);

service  和  serviceImpl

    @Override
    public List<TmsTaskCustomerGroup> getDetailByTaskId(Long taskId) {
        if(null == taskId)
            return null;

        QueryWrapper<TmsTaskCustomerGroup> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TmsTaskCustomerGroup::getTaskId, taskId);
        return tmsTaskCustomerGroupMapper.getDetailByTaskId(wrapper);
    }

entity

@Data
@EqualsAndHashCode(callSuper = false)
@TableName("tms_task_device_group")
@ApiModel(value="TmsTaskDeviceGroup对象", description="任务设备组表")
public class TmsTaskDeviceGroup implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "商家ID")
    private Long merchantId;

    @ApiModelProperty(value = "任务ID")
    private Long taskId;

    @ApiModelProperty(value = "设备组ID")
    private Long deviceGroupId;

    private Date createTime;

    @TableField(exist = false)
    private String name;

}

连表查询

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页