agumaster 分页方案

本文例程下载:https://files.cnblogs.com/files/xiandedanteng/agumaster20200430-1.zip

之前的分页方案有点小瑕疵,这回修正了一下。

控制类:

package com.ufo.hy.agumaster.ctrl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ufo.hy.agumaster.dao.StockMapper;
import com.ufo.hy.agumaster.entity.Stock;

@Controller
public class ActionController {
    @Autowired
    private StockMapper stockMapper=null;
    
    /**
     * 
     * @param m:model
     * @param pageNo:page number
     * @param pageSize:how many records in a page
     * @param keyword:key word
     * @return
     */
    @RequestMapping("/liststock")
    public String liststock(Model m,@RequestParam(value="pageNo",defaultValue="0") int pageNo,
                                       @RequestParam(value="pageSize",defaultValue="20") int pageSize,
                                       @RequestParam(value="keyword",defaultValue="") String keyword) {
        
        int start=pageNo*pageSize;
        int end=start+pageSize;
        List<Stock> list=stockMapper.pagedSearch(start, end,keyword);
        m.addAttribute("list", list);
        
        int count=stockMapper.getPagedSearchCount(keyword);
        int pageCount=count/pageSize;
        m.addAttribute("pageCount", pageCount);

        m.addAttribute("pageNo", pageNo);
        
        return "liststock";
    }
}

Mapper类:

package com.ufo.hy.agumaster.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import com.ufo.hy.agumaster.entity.Stock;

@Mapper
public interface StockMapper {

    /**
     * Paged search stocks
     * @param start:Start line
     * @param end: End line
     * @param keyword: Search keyword
     * @return
     */
    @SelectProvider(type=StockSql.class,method="pagedSearch")
    List<Stock> pagedSearch(int start,int end,String keyword);
    
    /**
     * get all records' count of pagedSearch
     * @param keyword
     * @return
     */
    @Select("select count(*) from hy_stock where name like '%${keyword}%' or code like '%${keyword}%' ")
    int getPagedSearchCount(@Param("keyword") String keyword);
}

分页SQL:

package com.ufo.hy.agumaster.dao;

public class StockSql {
    public String pagedSearch(int start,int end,String keyword) {
        StringBuilder sb = new StringBuilder();

        sb.append(" select b.* from                       ");
        sb.append(" (select a.*,rownum as rn from         ");
        sb.append(" (select * from hy_stock where name like '%"+keyword+"%' or code like '%"+keyword+"%' order by id) a  ");
        sb.append(" where rownum<="+end+") b               ");
        sb.append(" where b.rn>"+start+"                  ");

        return sb.toString();
    }
}

页面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>List Stocks</title>
</head>
<body>
    <div>
    共:<span th:text="${pageCount}">pageCount</span>页
    当前第:<span th:text="${pageNo}">pageIndex</span></div>

    <table border="0px" width="160px">
        <tr><td colspan="10"><input type="text" /></td></tr>
        <tr>
            <td><a th:href="@{/liststock?pageNo=1&pageSize=20}">首页</a></td>
            <td><a th:href="'/liststock?pageNo=' + ${pageCount} +'&pageSize=20' ">末页</a></td>
            <td>
                <a th:if="${pageNo ne 1}" th:href="'/liststock?pageNo=' + ${pageNo - 1} +'&pageSize=20' ">上页</a>
                <span th:if="${pageNo eq 1}">上页</span>
            </td>
            <td>
                <a th:if="${pageNo ne pageCount}" th:href="'/liststock?pageNo=' + ${pageNo + 1} +'&pageSize=20' ">下页</a>
                <span th:if="${pageNo eq pageCount}">下页</span>
            </td>
        </tr>
    </table>
    
    <table border="1px" width="160px">
        <caption>All Stocks</caption>
        <thead>
            <tr><th>id</th><th>code</th><th>name</th></tr>
        </thead>
        <tbody>
            <tr th:each="item:${list}">
                <td th:text="${item.id}">id</td>
                <td th:text="${item.code}">name</td>
                <td th:text="${item.name}">salary</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

结果:

 

本例还增加了热启动部署:

        <!-- Hot deployment -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

 

另外将 application.properties 中内容改为:

spring.profiles.active=oracle

而增加了文件application-oracle.properties,其中内容是:

spring.profiles.active=oracle

spring.datasource.url=jdbc:oracle:thin:@dev-dm-XXXXX1z.dev.jp.local:2050/SV_TRTMSAPDB
spring.datasource.username=RTMXXXXMIN2
spring.datasource.password=teXXXXXX01
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

logging.level.com.ufo.hy.agumaster.dao.StockMapper=debug
logging.config=classpath:logback.xml

这是为了未来迁移到MySQL数据库做准备,届时再增加一个application-mysql.properties,再将application.properties 中spring.profiles.active改为mysql即可。

--2020-04-30--

 

posted @ 2020-04-30 13:36  逆火狂飙  阅读(206)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东