pojo
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Announcement extends BasePojo {
private Long id;
private String title;
private String description;
}
======================================================================
AnnouncementMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.common.pojo.Announcement;
public interface AnnouncementMapper extends BaseMapper {
}
===============================================================
AnnouncementService
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.common.mapper.AnnouncementMapper;
import com.tanhua.common.pojo.Announcement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AnnouncementService {
@Autowired
private AnnouncementMapper announcementMapper;
public IPage queryList(Integer page, Integer pageSize) {
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.orderByDesc(“created”);
return this.announcementMapper.selectPage(new Page(page, pageSize), queryWrapper);
}
}
===================================================================
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AnnouncementVo {
private String id;
private String title;
private String description;
private String createDate;
}
=============================================================
controller
/**
*
* @param page
* @param pageSize
* @return
*/
@GetMapping(“announcements”)
@NoAuthorization //优化,无需进行token校验
public ResponseEntity queryMessageAnnouncementList(@RequestParam(value = “page”, defaultValue = “1”) Integer page,
@RequestParam(value = “pagesize”, defaultValue = “10”) Integer pageSize) {
try {
PageResult pageResult = this.imService.queryMessageAnnouncementList(page, pageSize);
return ResponseEntity.ok(pageResult);
} catch (Exception e) {
log.error(“查询公告列表失败~ “, e);
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
================================================================
service
public PageResult queryMessageAnnouncementList(Integer page, Integer pageSize) {
IPage announcementPage = this.announcementService.queryList(page, pageSize);
List announcementVoList = new ArrayList();
for (Announcement record : announcementPage.getRecords()) {
AnnouncementVo announcementVo = new AnnouncementVo();
announcementVo.setId(record.getId().toString());
announcementVo.setTitle(record.getTitle());
announcementVo.setDescription(record.getDescription());
announcementVo.setCreateDate(DateUtil.format(record.getCreated(), “yyyy-MM-dd HH:mm”));
announcementVoList.add(announcementVo);
}
PageResult pageResult = new PageResult();
pageResult.setPage(page);
pageResult.setPagesize(pageSize);
pageResult.setItems(announcementVoList);
return pageResult;
}