mybatisPlus 查询最新一条数据

1、ApiBackgroundController

@RestController
@RequestMapping("/api/background")
public class ApiBackgroundController {

    @Resource
    private BackgroundService backgroundService;

    @GetMapping("/one")
    public Result getOne() {
        return backgroundService.getOnlyOne();
    }

2、BackgroundService

public interface BackgroundService extends IService<Background> {

    /**
     * 获取一条数据
     * @param wrapper
     * @return
     */
    default Background getOnly(QueryWrapper<Background> wrapper) {
        wrapper.last("limit 1");
        return this.getOne(wrapper);
    }

    Result getOnlyOne();
}

3、BackgroundServiceImpl

@Service
public class BackgroundServiceImpl extends ServiceImpl<BackgroundMapper, Background> implements BackgroundService {

	@Resource
    private BackgroundService backgroundService;

    @Override
    public Result getOnlyOne() {
        QueryWrapper<Background> wrapper = new QueryWrapper<>();
        wrapper.eq("status", Background.STATUS_WORKING)
                .orderByDesc("update_time");
        final String url = backgroundService.getOnly(wrapper).getUrl();
        return Result.ok().data("one", url);
    }

你可能感兴趣的:(java)