• 财富平台项目日记1:spring boot + mybatis 实现分页查询


    controller层:@RestController

    @RequestMapping("/userInfo")
    public class UserApplyInfoController extends BaseController
    {
    @Autowired
    private UserCRUDEService userCRUDEService;

    /**
    * 未完成的
    *
    * @param map
    * @return
    */
    @PostMapping("/queryNotCompleted")
    @ResponseBody
    @RsaSecurityParameter
    public Result queryNotCompleted(@RequestBody Map map)
    {
    Integer page = Integer.valueOf(map.get("currPage").toString());
    Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
    PageHelper.startPage(page, pageSize);
    List<UserApplyInfo> userApplyInfos = userCRUDEService.queryDateByArray(map);
    PageInfo pageInfo = new PageInfo(userApplyInfos);
    return success(pageInfo);
    }
    }
    代码解释:
      Result返回类型是自定的统一API封装:
      代码如下:
      
    package com.pro.utils;

    import com.alibaba.fastjson.JSON;

    /**
    * 统一API响应结果封装
    */
    public class Result<T> {
    private int code;
    private String message;
    private T data;
    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";

    public Result setCode(int code) {
    this.code = code;
    return this;
    }
    public int getCode() {
    return code;
    }

    public String getMessage() {
    return message;
    }

    public Result setMessage(String message) {
    this.message = message;
    return this;
    }
    public T getData() {
    return data;
    }
    public Result setData(T data) {
    this.data = data;
    return this;
    }
    @Override
    public String toString() {
    return JSON.toJSONString(this);
    }
    }

    还有继承的BaseController类:
    package com.pro.utils;

    import org.apache.commons.lang3.StringUtils;

    import javax.servlet.http.HttpServletRequest;
    import java.util.ArrayList;
    import java.util.List;

    public class BaseController {

    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";

    public List<PropertFilter> BuildPropertFilter(HttpServletRequest request){
    List<PropertFilter> filters=new ArrayList<PropertFilter>();
    List<String> keys = new ArrayList<String>(request.getParameterMap().keySet());
    for(String key:keys){
    String[] strs=key.split("_");
    if("filter".equals(strs[0])){
    if(StringUtils.isNotEmpty(request.getParameter(key))) {
    PropertFilter filter=new PropertFilter();
    filter.setQueryType(strs[1]);
    filter.setField(strs[2]);
    filter.setValue(request.getParameter(key));
    filters.add(filter);
    }
    }
    }
    return filters;
    }

    public Result success() {
    return new Result()
    .setCode(ResultCode.SUCCESS.code())
    .setMessage(DEFAULT_SUCCESS_MESSAGE);
    }

    public <T> Result<T> success(T data) {
    return new Result()
    .setCode(ResultCode.SUCCESS.code())
    .setMessage(DEFAULT_SUCCESS_MESSAGE)
    .setData(data);
    }

    public Result fail(String message) {
    return new Result()
    .setCode(ResultCode.FAIL.code())
    .setMessage(message);
    }
    }
    是封装了Result类的返回类型和一些通用返回方法的。

    其他的都是一些通用方法,其中PageHelp是mybatis自带的分页插件,直接传入分页参数即可。

    service层:
    public interface UserCRUDEService
    {
        List<UserApplyInfo> queryDateByArray(Map map);
    
    }

    serviceImpl层:

    public class UserCRUDServiceImpl implements UserCRUDEService
    {
        @Autowired
        private UserApplyInfoCrudDao userApplyInfoCrud;
    
        @Override
        public List<UserApplyInfo> queryDateByArray(Map map)
        {
            return userApplyInfoCrud.selectAll(map);
        }
    }

    代码解释:

      直接调Dao层的查询方法即可

    Dao层:

    import com.pro.model.UserApplyInfo;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    import java.util.Map;
    
    @Mapper
    public interface UserApplyInfoCrudDao
    {
        List<UserApplyInfo> selectAll(Map map);
    }

    pojo类:

    public class UserApplyInfo
    {
        private Integer id;
    
        private String name;
    
        private String idnumber;
    
        private String phone;
    
        private String bank;
    
        private String banknumber;
    
        private Double money;
    
        private String serialcode;
    
        private Integer state;
    
        private Date systemtime;
    
        private String ishangup;
    
        private Integer isCashangup;
    
        private String historyhangup;
    
        //手续费
        private Double servicecharge;
    
        //总金额
        private Double totalmoney;
    
        private List<UserDetailed> userDetailedList;
    
        public List<UserDetailed> getUserDetailedList()
        {
            return userDetailedList;
        }
    
        public void setUserDetailedList(List<UserDetailed> userDetailedList)
        {
            this.userDetailedList = userDetailedList;
        }
    
        public Double getServicecharge()
        {
            return servicecharge;
        }
    
        public void setServicecharge(Double servicecharge)
        {
            this.servicecharge = servicecharge;
        }
    
        public Double getTotalmoney()
        {
            return totalmoney;
        }
    
        public void setTotalmoney(Double totalmoney)
        {
            this.totalmoney = totalmoney;
        }
    
        public Integer getId()
        {
            return id;
        }
    
        public void setId(Integer id)
        {
            this.id = id;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public String getIdnumber()
        {
            return idnumber;
        }
    
        public void setIdnumber(String idnumber)
        {
            this.idnumber = idnumber;
        }
    
        public String getPhone()
        {
            return phone;
        }
    
        public void setPhone(String phone)
        {
            this.phone = phone;
        }
    
        public String getBank()
        {
            return bank;
        }
    
        public void setBank(String bank)
        {
            this.bank = bank;
        }
    
        public String getBanknumber()
        {
            return banknumber;
        }
    
        public void setBanknumber(String banknumber)
        {
            this.banknumber = banknumber;
        }
    
        public Double getMoney()
        {
            return money;
        }
    
        public void setMoney(Double money)
        {
            this.money = money;
        }
    
        public String getSerialcode()
        {
            return serialcode;
        }
    
        public void setSerialcode(String serialcode)
        {
            this.serialcode = serialcode;
        }
    
        public Integer getState()
        {
            return state;
        }
    
        public void setState(Integer state)
        {
            this.state = state;
        }
    
        public Date getSystemtime()
        {
            return systemtime;
        }
    
        public void setSystemtime(Date systemtime)
        {
            this.systemtime = systemtime;
        }
    
        public String getIshangup()
        {
            return ishangup;
        }
    
        public void setIshangup(String ishangup)
        {
            this.ishangup = ishangup;
        }
    
        public Integer getIsCashangup()
        {
            return isCashangup;
        }
    
        public void setIsCashangup(Integer isCashangup)
        {
            this.isCashangup = isCashangup;
        }
    
        public String getHistoryhangup()
        {
            return historyhangup;
        }
    
        public void setHistoryhangup(String historyhangup)
        {
            this.historyhangup = historyhangup;
        }
    }

    mapper.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.pro.dao.UserApplyInfoCrudDao">
        <sql id="demo">
            id,name,idnumber,phone,bank,banknumber,money,serialcode,state,systemtime,isCashangup,historyhangup,servicecharge,totalmoney
        </sql>
    
        <select id="selectAll" parameterType="map" resultType="com.pro.model.UserApplyInfo">
            SELECT
            <include refid="demo"/>
            FROM user_apply_info
            where
            1=1
            <if test="id != null and id != ''">
                and id=#{id}
            </if>
            <if test="name != null and name != ''">
                and name=#{name}
            </if>
            <if test="state != null and state != ''">
                and state=#{state}
            </if>
            <if test="isCashangup != null and isCashangup != ''">
                and isCashangup=#{isCashangup}
            </if>
            <if test="historyhangup != null and historyhangup != ''">
                and historyhangup=#{historyhangup}
            </if>
            <if test="servicecharge != null and servicecharge != ''">
                and servicecharge=#{servicecharge}
            </if>
            <if test="totalmoney != null and totalmoney != ''">
                and totalmoney=#{totalmoney}
            </if>
        </select>
    </mapper>

    代码解释:

      可以查询出需要的信息



  • 相关阅读:
    《深度剖析CPython解释器》16. Python函数机制的深度解析(第三部分): 闭包的底层实现以及调用
    《深度剖析CPython解释器》15. Python函数机制的深度解析(第二部分): 函数在底层是如何被调用的
    《深度剖析CPython解释器》14. Python函数机制的深度解析(第一部分): 函数在底层的数据结构、以及它的创建方式
    《深度剖析CPython解释器》13. 剖析Python的流程控制语句(if、for、while),以及异常捕获机制
    QT: QObject::connect()剖析
    C++ 点滴 (二)
    飞书有时黑屏的解决办法
    QT:显示器数量检测
    使用WinRar 制作自定义安装包
    QT-Mac:QT的SetText()在有些Mac OS系统上未显示
  • 原文地址:https://www.cnblogs.com/xuehu666/p/11435712.html
Copyright © 2020-2023  润新知