Mybatis-plus 多表查询

  ActivitySignServiceImpl.java
@Service
@Slf4j
public class ActivitySignServiceImpl extends ServiceImpl<ActivitySignMapper, ActivitySign> implements IActivitySignService {
@Autowired(required = false)
private ActivitySignMapper signMapper;

/**
 * 获取我的活动 - 未签到活动列表
 */
@Override
public ApiResponse getMyActivityListNoSign(ActivityStatusBo bo) {
    if(ObjectUtil.isNull(bo)){
        return ApiResponse.ofFailMessage(COMMON_FAILURE);
    }
    User user = ShiroUtils.getCurrentUser();
    Page<ActivitySignVo> page = signMapper.getPageActivityNoSign(new Page(bo.getPageNum(),bo.getPageSize()),user.getUid(), LocalDateTime.now());
    List<ActivitySignVo> records = page.getRecords();
    //没有查到满足条件的活动
    if(CollectionUtil.isEmpty(records)){
        return ApiResponse.ofSuccess(records);
    }

    List<ActivitySignVo> voList = records.stream()
            .map(any -> BeanUtil.toBean(any, ActivitySignVo.class))
            .collect(Collectors.toList());

    return ApiResponse.ofSuccess(voList);
}
}

 

 

ActivitySignMapper.java 此处有两种方式实现数据库操作 1、是用@Select方式,不用写xml了 2、是用xml方式 这两种方式xml都要写对应查出的数据字段绑定
public interface ActivitySignMapper extends BaseMapper<ActivitySign> {
//    @Select("SELECT t1.sign_flag,t2.* FROM activity_sign t1 left join activity_info t2 on t1.activity_uid = t2.uid\n" +
//            "where t1.customer_uid = #{customerUid} and t2.state = '1'  and t2.end_time > #{nowDateTime} and t1.sign_flag = '0' ")
//    @ResultMap("activitySignVo")
    Page<ActivitySignVo> getPageActivityNoSign(Page<ActivitySignVo> ipage, @Param("customerUid") String customerUid, @Param("nowDateTime") LocalDateTime nowDateTime);

//    @Select("SELECT t1.sign_flag,t2.* FROM activity_sign t1 left join activity_info t2 on t1.activity_uid = t2.uid \n" +
//            "where  t1.customer_uid = #{customerUid} and (t1.sign_flag = '1' or (t2.sign_flag = '0' and t2.end_time < #{nowDateTime}) )")
//    @ResultMap("activitySignVo")
    Page<ActivitySignVo> getPageActivityFinish(Page<ActivitySignVo> ipage , @Param("customerUid") String customerUid, @Param("nowDateTime") LocalDateTime nowDateTime);
}

 

 

ActivitySignMapper.xml

<mapper namespace="com.xypopo.market.mapper.ActivitySignMapper">
    <resultMap id="activitySignVo" type="com.xypopo.market.pojos.vo.ActivitySignVo">
        <!-- 主键字段 -->
        <id property="signFlag" column="sign_flag"></id>
        <result property="uid" column="uid"></result>
        <result property="startTime" column="start_time" javaType="java.time.LocalDateTime"></result>
        <result property="endTime" column="end_time" javaType="java.time.LocalDateTime"></result>
        <result property="storeUid" column="store_uid"></result>
        <result property="title" column="title"></result>
        <result property="cover" column="cover"></result>
        <result property="address" column="address"></result>
        <result property="state" column="state"></result>
    </resultMap>


    <select id="getPageActivityFinish" resultMap="activitySignVo">
        SELECT t1.sign_flag,t2.* FROM activity_sign t1 left join activity_info t2 on t1.activity_uid = t2.uid
        where  t1.customer_uid = #{customerUid} and (t1.sign_flag = '1' or (t2.sign_flag = '0' and t2.end_time &lt; #{nowDateTime}))
    </select>
    <select id="getPageActivityNoSign" resultMap="activitySignVo">
        SELECT t1.sign_flag,t2.* FROM activity_sign t1 left join activity_info t2 on t1.activity_uid = t2.uid
           where t1.customer_uid = #{customerUid} and t2.state = '1'  and t2.end_time &gt; #{nowDateTime} and t1.sign_flag = '0'
    </select>
</mapper>

 

  绑定接收查出的数据字段的对象 ActivitySignVo.java
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("activity_sign")
public class ActivitySignVo implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("签到状态:0-未签到 ; 1-已签到")
    private String signFlag;

    @ApiModelProperty("活动ID")
    private String uid;

    @ApiModelProperty("活动名称")
    private String name;

    @ApiModelProperty("活动开始时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;

    @ApiModelProperty("活动结束时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;

    @ApiModelProperty("活动门店id")
    private String storeUid;

    @ApiModelProperty("活动标题")
    private String title;

    @ApiModelProperty("活动封面图")
    private String cover;

    @ApiModelProperty("活动地址")
    private String address;

    @ApiModelProperty("活动状态:1-正常 ; 1-结束 ; 3-提前结束")
    private String state;
}

 

此处功能是实现接口查出我的活动未签到和已结束活动,用到了分页插件,需要关联用户报名的活动记录表和活动详情表查询出活动的详情和用户签到的标识,用联表查询实现          
上一篇:JWT学习


下一篇:PTA 6-2 多项式求值 (15 分)本题要求实现一个函数,计算阶数为n,系数为a[0] ... a[n]的多项式f(x)=∑ i=0n​ (a[i]×x i ) 在x点的值。