zoukankan      html  css  js  c++  java
  • Rest分页接口开发

    简单描述:需求说后端写一个XX数据的分页接口,给前端口调用,其实有一个PageHelper的工具类可以直接使用但是老大不让用,得用sql写,。小Kiss啦。直接上代码

    代码:

    //Controller代码
    import com.github.pagehelper.PageInfo;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    @Api(value = "CourseController",tags = "课程查询")
    @RestController
    @RequestMapping("/client/course")
    public class CourseController extends BaseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "获取分页列表")
    @RequestMapping(value = "/queryPage", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public ResultDto queryPage(Integer page,Integer rows,String categoryId){
    PageInfo<CourseVo> pageInfo = new PageInfo<CourseVo>();
    try {
    pageInfo = courseService.queryPage(page,rows);
    if(pageInfo.getList().size()< 0){
    return ResultDto.success("返回结果无内容");
    }
    } catch (Exception e) {
    e.printStackTrace();
    return ResultDto.error();
    }
    return ResultDto.success(pageInfo);
    }

    } 
    //Service代码
    import com.github.pagehelper.PageInfo;
    import org.springframework.stereotype.Service;
    import javax.annotation.Resource;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    @Service
    public class CourseService {
    @Resource
    private CourseMapper courseMapper;

    public PageInfo<CourseVo> queryPage(Integer page,Integer rows)throws Exception{
    Map<String,Integer> param = new HashMap<String,Object>();
    Integer firstIndex = (page - 1) * rows;
    Integer lastIndex = page * rows;
    // 从第几条数据开始 int firstIndex = (currPage - 1) * pageSize;
    // 到第几条数据结束 int lastIndex = currPage * pageSize;
    param.put("page",firstIndex);
    param.put("rows",lastIndex);

    List<CourseVo> reList = courseMapper.findByPage(param);
    // 取分页信息
    PageInfo<CourseVo> pageInfo = new PageInfo<CourseVo>(reList);
    return pageInfo;
    }
    } 
    //Mapper接口
    import com.xx.xxx.xxxx.util.MyMapper;
    import java.util.List;
    import java.util.Map;

    public interface CourseMapper extends MyMapper<Course> {
    List<CourseVo> findByPage(Map<String,Integer> param);
    } 

    //Mapper.xml中的sql
    <select id="findByPage" parameterType="Map" resultMap="CourseVoResultMap">
    select * from table_course where is_del = '1' limit #{page},#{rows}
    </select> 

    <resultMap id="CourseVoResultMap" type="CourseVo" >
    <result column="course_id" property="courseId" jdbcType="CHAR" />
    <result column="course_code" property="courseCode" jdbcType="VARCHAR" />
      ``````
    </result> 

    That's all !!!   下边来说一说PageHelper的方式

    //仅仅是调用就OJBK啦 不过这种查询方式使用的是mybatis的example动态生成sql查询的,不需要你写mapper.xml中的sql了,只需要在对应的类上加上注解
    public PageInfo<CourseVo> queryPage(Integer page,Integer rows)throws Exception{
    Example example = new Example(CourseVo.class);
    example.createCriteria().andEqualTo("isDel", 1);
    PageHelper.startPage(page, rows);
    List<CourseVo> list = courseMapper.selectByExample(example);//查询
    return pageInfo;
    } 
  • 相关阅读:
    numpy.clip(a, a_min, a_max, out=None)(values < a_min are replaced with a_min, and those > a_max with a_max.)
    pytorch使用过程中遇到的一些问题
    conda管理包
    python argparse模块
    pytorch中设定使用指定的GPU
    Linux下dpkg的用法
    python pdb模块
    ubuntu SSH 连接、远程上传下载文件
    Linux中执行shell脚本命令的4种方法总结
    python linux安装anaconda
  • 原文地址:https://www.cnblogs.com/xuchao0506/p/10299453.html
Copyright © 2011-2022 走看看