• mybatis 全查 分页 模糊查询一体


    配置xml文件

    <resultMap id="BaseResultMap" type="com.huyuqiang.vo.ptNotice.PtNoticeVo">
        <id column="NOTICE_ID" property="noticeId" jdbcType="VARCHAR"/>
        <result column="NOTICE_TITLE" property="noticeTitle" jdbcType="VARCHAR"/>
        <result column="DATETIME" property="datetime" jdbcType="TIMESTAMP"/>
        <result column="TYPE" property="type" jdbcType="VARCHAR"/>
    </resultMap>
     <!-- 全查 分页 模糊-->
      <select id="selectAll" resultMap="BaseResultMap" parameterType="com.huyuqiang.vo.ptNotice.PtNoticeVo">
          SELECT NOTICE_ID,NOTICE_TITLE,TYPE,DATETIME FROM pt_notice
          <where>
              <if test=" ptNoticeVo.type != null and ptNoticeVo.type != '' ">
                  <bind name="fType" value=" '%' + ptNoticeVo.type + '%' "></bind>
                  type like #{fType}
              </if>
              <if test=" ptNoticeVo.noticeTitle != null and ptNoticeVo.noticeTitle != '' ">
                  <bind name="noticeTitlef" value=" '%' + ptNoticeVo.noticeTitle + '%' "></bind>
                  AND NOTICE_TITLE like #{noticeTitlef}
              </if>
          </where>
          limit #{startIndex},#{pageSize}
      </select>
      <!--统计总数-->
      <select id="count" resultType="int" parameterType="com.huyuqiang.vo.ptNotice.PtNoticeVo">
      select count(*)
      from pt_notice
          <where>
              <if test=" ptNoticeVo.type != null and ptNoticeVo.type != '' ">
                  <bind name="fType" value=" '%' + ptNoticeVo.type + '%' "></bind>
                  type like #{fType}
              </if>
              <if test=" ptNoticeVo.noticeTitle != null and ptNoticeVo.noticeTitle != '' ">
                  <bind name="noticeTitlef" value=" '%' + ptNoticeVo.noticeTitle + '%' "></bind>
                  AND NOTICE_TITLE like #{noticeTitlef}
              </if>
          </where>
    </select>
    

     

    dao

    List<PtNoticeVo> selectAll(@Param("ptNoticeVo") PtNoticeVo ptNoticeVo,
    
             @Param("startIndex") int startIndex, @Param("pageSize") int pageSize);
    
    int count(@Param("ptNoticeVo") PtNoticeVo ptNoticeVo);
    

      

    @param是必须的 不然会报错 

    在企业开发中 每个程序员在编写自己模块的时候 基本都会自己创建相应的vo 也可以叫 entity   尽可能的不去改动mybatis逆向出来的原生实体类 vo可以根据自己的需要创建属性 当然它就是一个实体类 封装 无参构造 有参构造 甚至于toString 都应该具有

    因为一个实体类被两个人改动那就会引起不必要的冲突 而且原生实体类包含了数据库表中所有字段对映的属性 可是我们在前端显示的时候往往并不需要展示出所有的属性

    尤其是在springMVC中 vo可以用来接收前端传送过来的参数 前提是vo里所包含的某个属性和前端参数中的某个属性 类型 名称 一致

    同时也可以把一个vo返回给前端

    PtNoticeVo代码如下:

    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    public class PtNoticeVo implements Iterator<Object> {
    
        private String noticeId;
        private String noticeTitle;
        private String type;
        private Date datetime;
    
        public PtNoticeVo(String noticeId, String noticeTitle, String type, Date datetime) {
    
            this.noticeId = noticeId;
            this.noticeTitle = noticeTitle;
            this.type = type;
            this.datetime = datetime;
        }
    
        public String getType() {
    
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public Date getDatetime() {
            return datetime;
        }
    
        public void setDatetime(Date datetime) {
            this.datetime = datetime;
        }
    
        public PtNoticeVo() {
        }
    
    
        public String getNoticeId() {
            return noticeId;
        }
    
        public void setNoticeId(String noticeId) {
            this.noticeId = noticeId;
        }
    
        public String getNoticeTitle() {
            return noticeTitle;
        }
    
        public void setNoticeTitle(String noticeTitle) {
            this.noticeTitle = noticeTitle;
        }
    
    
    
    
        @Override
        public boolean hasNext() {
            return false;
        }
    
        @Override
        public Object next() {
            return null;
        }
    
    }
    

     

     pageVo代码如下

    import java.util.Date;
    import java.util.List;
    
    public class PageVo<T> {
    
        private Integer code = 200;   //接口状态码
    
        // 当前页
        private Integer currentPage = 1;
        // 每页显示的总条数
        private Integer pageSize = 10;
        // 总条数
        private Integer totalNum;
        // 是否有下一页
        private Integer isMore;
        // 总页数
        private Integer totalPage;
        // 开始索引
        private Integer startIndex;
        // 分页结果
        private List<T> items;
    
        public PageVo() {
            super();
        }
    
        public PageVo(Integer currentPage, Integer pageSize, Integer totalNum) {
            super();
            if (currentPage != null && currentPage > 0) {
                this.currentPage = currentPage;
            }
            if (pageSize != null && pageSize > 0) {
                this.pageSize = pageSize;
            }
            this.totalNum = totalNum;
            this.totalPage = (this.totalNum + this.pageSize - 1) / this.pageSize;
            this.startIndex = (this.currentPage - 1) * this.pageSize;
            this.isMore = this.currentPage >= this.totalPage ? 0 : 1;
        }
    
        public Integer getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
    
        public Integer getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public Integer getTotalNum() {
            return totalNum;
        }
    
        public void setTotalNum(Integer totalNum) {
            this.totalNum = totalNum;
        }
    
        public Integer getIsMore() {
            return isMore;
        }
    
        public void setIsMore(Integer isMore) {
            this.isMore = isMore;
        }
    
        public Integer getTotalPage() {
            return totalPage;
        }
    
        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }
    
        public Integer getStartIndex() {
            return startIndex;
        }
    
        public void setStartIndex(Integer startIndex) {
            this.startIndex = startIndex;
        }
    
        public List<T> getItems() {
            return items;
        }
    
        public void setItems(List<T> items) {
            this.items = items;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
    }
    

      

    这里说一下PageVo 

    前端传送给你用来分页的出去模糊查询之外 一般只有两个参数  一个是当前页也就是要查找第几页 上面的"currentPag" 和每页显示几条的“pageSize”

    我们通过sql是可以查出来总条数的 有了总条数 “totalNum ” 就可以查出总页数 所有上面用 

    this.totalPage = (this.totalNum + this.pageSize - 1) / this.pageSize;  
    
    

    按照常理 用总条数除以每页显示几条就可以得到总页数 但是仔细想想 这是难以完成整除的 但是页数要取整 例如 45条/10条每页显示 在java中获得的整数是“4”

    所有才出现了上面的求总页数的方法

    mysql分页 是根据索引和每页显示条数 两个参数进行分页的

    所以我们要求索引 所谓索引 就是从你查询出来的数据的第几条开始显示 

    那怎么求出从第几条开始显示呢

    如果我们要查询第五页 每页显示10行

    那就是从第41个开始显示 显示到第五十个

      this.startIndex = (this.currentPage - 1) * this.pageSize;
    

      

    这行代码就解决了这个问题 当前页减去1乘以每页显示条数 就是你分页的索引值

    如下是service层代码

    @Service
    public class PtNoticeServiceImpl implements PtNoticeService {
        @Autowired
        PtNoticeMapper ptNoticeMapper;
        @Override
        public PageVo<PtNoticeVo> selectAll(PtNoticeVo ptNoticeVo,int currentpage,int pageSize) {
            int totalNum;
            if(pageSize<=0){
                totalNum = pageSize;
            }else {
                totalNum = ptNoticeMapper.count(ptNoticeVo);
            }
            PageVo<PtNoticeVo> pageVo = new PageVo<>(currentpage,pageSize,totalNum);
            pageVo.setItems(ptNoticeMapper.selectAll(ptNoticeVo,pageVo.getStartIndex(),pageSize));
            pageVo.setTotalNum(totalNum);
            return pageVo;
        }
    }
    

      

    controller层代码

    @RestController
    @RequestMapping("api/ptNotice")
    public class PtNoticeController {
        @Autowired
        PtNoticeService ptNoticeService;
    
        @RequestMapping("/selectAll")
        PageVo<PtNoticeVo> select(PtNoticeVo ptNoticeVo, int currentPag, int pageSize) {
            System.out.println(ptNoticeVo.getType());
            return ptNoticeService.selectAll(ptNoticeVo, currentPag, pageSize);
        }
    }
    

      

  • 相关阅读:
    weblogic10配置JNDI须在JDK1.5上
    JDBC大批量写入数据到SQLServer2000,记录数大于10000
    ASP递归调用:已知节点查找根节点的函数
    window.createPopup()
    Hibernate处理同名实体的方法
    windows下Apache Rewrite .htaccess 设置
    使用jsonlib将java对象转换成json字符串
    关于oracle和jdbc中rowid格式的转化
    [转]MaCfee导致Asp.net/Jmail无法发送邮件的解决办法
    javascript监测(控制 )上传文件大小
  • 原文地址:https://www.cnblogs.com/woshuyuqiang/p/9742202.html
Copyright © 2020-2023  润新知