本文例程下载: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--

agumaster 分页方案的更多相关文章

  1. ES 分页方案

    ES 中,存在三种常见的分页方案: FROM, SIZE Search-After Scroll 下面将依次比较三种方案之间的 trede-off,并给出相应建议的应用场景. 常见分页,FROM, S ...

  2. mysql高效分页方案及原理

    很久以前的一次面试中,被面试官问到这个问题,由于平时用到的分页方法不多,只从索引.分表.使用子查询精准定位偏移以外,没有使用到其它方法. 后来在看其它博客看到了一些不同的方案,也一直没有整理.今天有时 ...

  3. Vue Element Tabe Pager 分页方案

    表格和分页分离的,但是使用中,却是结合在一起的. 分析 有以下方式触发查询: mounted 加载数据. 查询按钮 加载数据. pager 变化加载数据 加载数据函数: loadData 问题 mou ...

  4. Mysql 千万级快速查询|分页方案

    1.简单的 直接查主键id SELECT id FROM tblist WHERE LIMIT 500000,10 2对于有where 条件,又想走索引用limit的,必须创建一个索引,将where  ...

  5. SQL分页语句三方案

    方法一: SELECT TOP 页大小 * FROM table1 WHERE id NOT IN ( SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id ...

  6. MongoDB 分页查询的方法及性能

    最近有点忙,本来有好多东西可以总结,Redis系列其实还应该有四.五.六...不过<Redis in Action>还没读完,等读完再来总结,不然太水,对不起读者. 自从上次Redis之后 ...

  7. 浅谈SQL Server数据库分页

    数据库分页是老生常谈的问题了.如果使用ORM框架,再使用LINQ的话,一个Skip和Take就可以搞定.但是有时由于限制,需要使用存储过程来实现.在SQLServer中使用存储过程实现分页的已经有很多 ...

  8. oracle,mysql,SqlServer三种数据库的分页查询的实例。

    MySql: MySQL数据库实现分页比较简单,提供了 LIMIT函数.一般只需要直接写到sql语句后面就行了.LIMIT子 句可以用来限制由SELECT语句返回过来的数据数量,它有一个或两个参数,如 ...

  9. SQL分页语句

    有关分页 SQL 的资料很多,有的使用存储过程,有的使用游标.本人不喜欢使用游标,我觉得它耗资.效率低:使用存储过程是个不错的选择,因为存储过程是经过预编译的,执行效率高,也更灵活.先看看单条 SQL ...

随机推荐

  1. 9、Bridge 桥梁模式 将类的功能层次结构与实现层结构分离 结构型设计模式

    1.何为桥接模式 桥接模式是一种将类的功能层次和实现层次分离的技术,所谓类的功能层次指的是类要实现什么功能,要定义多少个函数还进行处理,在功能之中我们会用到继承来定义新的方法同时也能使用父类的方法,这 ...

  2. 刚体验完RabbitMQ?一文带你SpringBoot+RabbitMQ方式收发消息

    人生终将是场单人旅途,孤独之前是迷茫,孤独过后是成长. 楔子 这篇是消息队列RabbitMQ的第二弹. 上一篇的结尾我也预告了本篇的内容:利用RabbitTemplate和注解进行收发消息,还有一个我 ...

  3. 如何实现数据库CDP,即数据库连续数据保护

    备份可以分为定期备份和实时备份.定期备份与实时备份相比存在两大劣势:一是备份需要时间窗口,对于很多24小时业务运行的机构,线上业务不允许有过多的业务系统停机去进行数据备份:二是定期备份无法保证数据丢失 ...

  4. notepad++ 设置支持golang语法高亮

    see https://stackoverflow.com/questions/27747457/golang-plugin-on-notepad

  5. 他们都说JVM能实际使用的内存比-Xmx指定的少?这是为什么呢

    这确实是个挺奇怪的问题,特别是当最常出现的几种解释理由都被排除后,看来JVM并没有耍一些明显的小花招: -Xmx和-Xms是相等的,因此检测结果并不会因为堆内存增加而在运行时有所变化. 通过关闭自适应 ...

  6. HTML基础-04

    定位 定位:通过定位可以将元素摆放在页面中任意位置 语法:position属性设置元素的定位 可选值:static:默认值,开启定位 relative开启相对定位 absolute开启绝对定位 fix ...

  7. span和input布局时对不齐

    如图 在span和input的css里各添加一行代码: vertical-align:top; (span和input在同一个盒子里)

  8. day2 变量

    变量是在程序中表现为不重复的名字,只需定义一个名字,给这个名字变量赋值即可  作用  在内存中开辟一块空间.起了一个别名,用了访问和存储空间中的数据 在编写 Python 程序过程中, 经常需要给标识 ...

  9. 浏览器自动化的一些体会11 webclient的异步操作

    原来的代码大致如下: private void foo(string url) { using (WebClient client = new WebClient()) { client.Downlo ...

  10. jmeter性能测试入门使用参数化

    我经常使用jmeter进行接口测试,这个工具还是很好用的.昨天收到一个需求,需要压测一下接口,jmeter进行接口测试,使用cvs文件进行多个数据参数化. 临时准备了一下发现忘记怎么做参数化了,自己百 ...