spingboot+mybatis分页(limit分页)--R

controller参数 继承分页类:

package com.finance.cmp.ruleEngine.common.vo;

public class BaseQueryVo {
    public boolean _search;
    public String nd;
    public String sidx;
    public String sord;
    public Integer page;
    public Integer rows;

    public boolean is_search() {
        return _search;
    }

    public void set_search(boolean _search) {
        this._search = _search;
    }

    public String getNd() {
        return nd;
    }

    public void setNd(String nd) {
        this.nd = nd;
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public String getSord() {
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public Integer getPage() {
        if (page == null) {
            return 1;
        }
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRows() {
        if (rows == null) {
            return 20;
        }
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }
}

1.controller: 可不传参数,传参数(条件/rows/page)

@GetMapping("list")
    public Result selectList(RuleQueryVo ruleQueryVo) {
        log.info("入参:" + ruleQueryVo);
        Map resultMap = null;
        try {
            resultMap = itRuleService.selectByList(ruleQueryVo);
        } catch (Exception e) {
            return ResultUtil.error(ResultEnum.SERVER_ERROR, e.getMessage());
        }
        return ResultUtil.success(resultMap);
    }

2.service:
接口:

public interface ITVarBasicService {

    int getBasicIsUse(Integer factorVarId);

     //分页
    List getAll();

    List getAllValidFromCache();

    TVarBasic getByOne(TVarBasic tVarBasic);

    Map selectByList(VarBasicQueryVo vo);

    Integer insertTVarBasic(TVarBasic tVarBasic);

    Integer deleteTVarBasic(int id);

    Integer updateTVarBasic(TVarBasic tVarBasic);

}

实现类:

package com.finance.cmp.ruleEngine.service.service.impl;

import com.finance.cmp.ruleEngine.cache.content.Constant;
import com.finance.cmp.ruleEngine.common.vo.RuleQueryVo;
import com.finance.cmp.ruleEngine.dao.mapper.TRuleMapper;
import com.finance.cmp.ruleEngine.dao.model.TRule;
import com.finance.cmp.ruleEngine.dao.model.TScoreCard;
import com.finance.cmp.ruleEngine.dao.model.TValConst;
import com.finance.cmp.ruleEngine.dao.vo.DeriveVo;
import com.finance.cmp.ruleEngine.dao.vo.RuleVo;
import com.finance.cmp.ruleEngine.service.common.RedisCacheService;
import com.finance.cmp.ruleEngine.service.service.ITRuleService;
import com.finance.cmp.ruleEngine.service.util.AcStringUtils;
import com.finance.cmp.ruleEngine.service.util.MapUtils;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@Log4j
@Service
public class TRuleServiceImpl implements ITRuleService {

    @Autowired
    private TRuleMapper tRuleMapper;

    @Autowired
    private RedisCacheService redisCacheService;

    @Override
    public TRule selectById(Integer id) {
        Map temMap = new HashMap<>();
        temMap.put("id", id);
        TRule tRule = tRuleMapper.selectById(temMap);
        return tRule;
    }

    @Override
    public List getAllValidFromCache() {
        List lst;
        if (redisCacheService.hasKey(Constant.REDIS_CONSOLE_RULE_ALL)) {
            lst = (List) redisCacheService.get(Constant.REDIS_CONSOLE_RULE_ALL);
        } else {
            lst = tRuleMapper.getAllValid();
            redisCacheService.set(Constant.REDIS_CONSOLE_RULE_ALL, lst);
        }
        return lst;
    }

    @Override
    public Integer ruleIsUse(Integer ruleId) {
        Map temMap = new HashMap<>();
        temMap.put("ruleId", ruleId);
        return tRuleMapper.ruleIsUse(temMap);
    }

    @Override
    public int getTotalRuleList(String varName) {
        Map temMap = new HashMap<>();
        temMap.put("name", varName);
        return tRuleMapper.getTotalRuleList(temMap);
    }

    //分页 逻辑
    
    @Override
    public Map selectByList(RuleQueryVo vo) {
        Map resultMap = new HashMap<>();
        Map temMap = new HashMap<>();
        temMap = MapUtils.beanToMap(vo, Constant.LIST_FIELD);
        //查询总条数
        int totalNum = tRuleMapper.getTotalRuleList(temMap);
        log.info("总条数为:" + totalNum);
        if (totalNum < 1) {
            resultMap.put("total", 0);
            resultMap.put("data", null);
            return resultMap;
        }

        String pageIndex = vo.getPage().toString();
        String pageSize = vo.getRows().toString();
        temMap.put("start", 0);
        temMap.put("size", 10);
        if ((!AcStringUtils.isEmpty(pageIndex)) && (!AcStringUtils.isEmpty(pageSize))) {
            int start = ((Integer.parseInt(pageIndex)) - 1) * (Integer.parseInt(pageSize));
            int size = Integer.parseInt(pageSize);
            // int
            // end=((Integer.parseInt(pageIndex)))*(Integer.parseInt(pageSize));
            temMap.put("start", start);
            temMap.put("size", size);
        }
        //分页按条件
        List dataList = tRuleMapper.getRuleList(temMap);
        resultMap.put("pageIndex", pageIndex);
        resultMap.put("totalPages"
                , (totalNum % Integer.parseInt(pageSize) > 0
                        ? totalNum / Integer.parseInt(pageSize) + 1
                        : totalNum / Integer.parseInt(pageSize)
                )
        );
        resultMap.put("total", totalNum);
        resultMap.put("data", dataList);
        return resultMap;
    }

    @Override
    public TRule selectByRuleName(TRule tRule) {
        return tRuleMapper.selectOne(tRule);
    }

    @Override
    public Integer insertRule(TRule tRule) {
        int addRule = tRuleMapper.insert(tRule);
        if (addRule > 0) {
            redisCacheService.delete(Constant.REDIS_CONSOLE_RULE_ALL);
        }
        return addRule;
    }

    @Override
    public Integer deleteRule(int id) {
        int deleteRule = tRuleMapper.deleteByPrimaryKey(id);
        if (deleteRule > 0) {
            redisCacheService.delete(Constant.REDIS_CONSOLE_RULE_ALL);
        }
        return deleteRule;
    }

    @Override
    public Integer updateRule(TRule tRule) {
        //多条件查询
        Example example = new Example(TRule.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", tRule.getId());
        Date dt = tRule.getUpdateTime();
        criteria.andEqualTo("updateTime", dt);
        tRule.setUpdateTime(new Date());
        int update = tRuleMapper.updateByExampleSelective(tRule, example);
        if (update > 0) {
            redisCacheService.delete(Constant.REDIS_CONSOLE_RULE_ALL);
        }
        return update;
    }
}

3.mapper:

public interface TVarBasicMapper extends MyMapper {


    int getBasicIsUse(Map temMap);

    //查总条数 不分页
    int getTotalVarBasicList(Map temMap);

    //分页
    List getVarBasicList(Map temMap);

    List getAllValid();

}

4.xml :




    
        
        
        
        
        
        
        
        
        
        
    

    

    

    

    


至此分页完成。

你可能感兴趣的:(springboot)