Mybatis-plus分页插件使用(新)

Mybatis-Plus分页插件使用

  • Config配置
  • Mapper
  • Service
  • Controller
  • 测试

Config配置

@Configuration
@EnableTransactionManagement
@MapperScan("com.liyue.music.dao")
public class ApplicationConfig {
	    /**
     * 插件主体
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

Mapper

public interface SingerMapper extends BaseMapper<Singer> {
}

Service

public interface SingerService {
    /**
     * 分页查询歌手
     * @param pageNum
     * @param pageSize
     * @return
     */
    Page<Singer> getSingerWithPage(Integer pageNum,Integer pageSize);
}
@Service
public class SingerServiceImpl implements SingerService {
    @Override
    public Page<Singer> getSingerWithPage(Integer pageNum,Integer pageSize) {
        Page<Singer> page = new Page<Singer>(pageNum, pageSize);
        Page<Singer> singerPage = singerMapper.selectPage(page, null);
        return singerPage;
    }
}

Controller

@RestController
@Slf4j
public class SingerController {
    @Autowired
    private SingerService singerService;
    }
    
    @GetMapping("/singers")
    public CommonResult getSingers(@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
                                   @RequestParam(name = "pageSize") Integer pageSize ){
        Map<String,Object> map = new HashMap<>(2);
        Page<Singer> singerWithPage = singerService.getSingerWithPage(pageNum,pageSize);
        //获取歌手信息
        List<Singer> singers = singerWithPage.getRecords();
        //获取总数
        long total = singerWithPage.getTotal();
        log.info("查询到歌手总数:" + total);
        map.put("list",singers);
        map.put("total",total);
        CommonResult result = new CommonResult();
        result.hander(ResultEnum.SUCCESS,map);
        return result;
    }
}

测试

在这里插入图片描述

你可能感兴趣的:(Mybatis,spring,boot,java,java,intellij-idea,spring,boot,mybatis)