mybatis分页查询

mybatis分页查询

  • 1.通过PageHelper实现分页查询
  • 分页controller
 /**
     * 查询承台根据分页或关键字
     * @param table_name 关键字
     * @param field_name 关键字字段
     * @param field_value 关键字值
     * @param pageNumber  页数
     * @param pageSize    页大小
     * @author csz
     * @date 2020/3/27
     */
    @RequestMapping("/findCommonByPageOrKey")
    public ResultCode findCommonByPageOrKey(   @RequestParam String table_name,
                                               @RequestParam String field_name,
                                               @RequestParam String field_value,
                                               @RequestParam(required = true)@NotNull Integer pageNumber,
                                               @RequestParam(required = true)@NotNull Integer pageSize) {
        if ("".equals(pageNumber) || pageNumber == null  ||  "".equals(pageSize) || pageSize == null ) {
            return ResultCode.errorParam("参数不能为空!");
        }
        PageInfo list = new PageInfo();
        try {
            list = commonService.findCommonByPageOrKey(table_name,field_name,field_value,pageNumber,pageSize);
            if (list == null ) {
                return ResultCode.errorParam("未查询到数据!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error("信息获取失败");
        }
        return ResultCode.success(list);
    }

  • 分页服务层
 /**
     * 分页或关键字查询信息
     * @return
     */
    public PageInfo findCommonByPageOrKey(String table_name, String field_name, String field_value, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        //查询对应的表信息
        String  sql = "SELECT  *  FROM  "+""+table_name+"" +" where "  + ""+field_name+"" + " like '%"+ ""+field_value+"" +"%' ";
        List<Map<String,Object>> list = commonMapper.findCommonByPageOrKey(sql);
        return new PageInfo(list);
    }
  • 2.通过limit offset关键字查询
  • controller.java
 @RequestMapping("/findFloodByPage")
    public ResultCode findFloodByPage(@RequestParam  Integer pageNumber, @RequestParam Integer pageSize) {

        if ("".equals(pageNumber) || pageNumber == null  ||  "".equals(pageSize) || pageSize == null ) {
            return ResultCode.errorParam("参数不能为空!");
        }
        List<FloodEntity> list = new ArrayList<FloodEntity>();
        try {

            list = floodService.findFloodByPage(pageNumber,pageSize);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error("信息获取失败");
        }
        return ResultCode.success(list);
    }
  • service.java
public List<FloodEntity> findFloodByPageOrKey(String key, Integer pageNumber, Integer pageSize) {

        return floodMapper.findFloodByPageOrKey(key,(pageNumber-1)*pageSize,pageSize);

    }
  • dao.java
//分页或关键字查询防洪点
    List<FloodEntity> findFloodByPageOrKey(@Param("key_word")String key_word,@Param("pageNumber") Integer pageNumber, @Param("pageSize")Integer pageSize);

  • dao.xml
 <select id="findFloodByPageOrKey" resultMap="BaseResultMap">
        SELECT *  FROM  server_fhd
        <where>
            <if test="key_word != null and key_word.length()>0">
                <bind name="bindname" value="'%' + key_word + '%'" />
                name like #{bindname}
            </if>
        </where>
            limit #{pageSize}  offset #{pageNumber}
    </select>
发布了25 篇原创文章 · 获赞 4 · 访问量 5772
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览