pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>isea533.mybatis</groupId> <artifactId>page</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>PageHelperSample</name> <url>http://git.oschina.net/free/Mybatis-Sample</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- web --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.7.4</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.5</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>utf-8</encoding> </configuration> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.0.0.M3</version> </plugin> </plugins> </build> </project>
sql:
drop table country if exists; create table country ( id integer, countryname varchar(32), countrycode varchar(2) ); insert into country (id, countryname, countrycode) values(1,‘Angola‘,‘AO‘); insert into country (id, countryname, countrycode) values(2,‘Afghanistan‘,‘AF‘); insert into country (id, countryname, countrycode) values(3,‘Albania‘,‘AL‘); insert into country (id, countryname, countrycode) values(4,‘Algeria‘,‘DZ‘); insert into country (id, countryname, countrycode) values(5,‘Andorra‘,‘AD‘); insert into country (id, countryname, countrycode) values(6,‘Anguilla‘,‘AI‘); insert into country (id, countryname, countrycode) values(7,‘Antigua and Barbuda‘,‘AG‘); insert into country (id, countryname, countrycode) values(8,‘Argentina‘,‘AR‘); insert into country (id, countryname, countrycode) values(9,‘Armenia‘,‘AM‘); insert into country (id, countryname, countrycode) values(10,‘Australia‘,‘AU‘); insert into country (id, countryname, countrycode) values(11,‘Austria‘,‘AT‘); insert into country (id, countryname, countrycode) values(12,‘Azerbaijan‘,‘AZ‘); insert into country (id, countryname, countrycode) values(13,‘Bahamas‘,‘BS‘); insert into country (id, countryname, countrycode) values(14,‘Bahrain‘,‘BH‘); insert into country (id, countryname, countrycode) values(15,‘Bangladesh‘,‘BD‘); insert into country (id, countryname, countrycode) values(16,‘Barbados‘,‘BB‘); insert into country (id, countryname, countrycode) values(17,‘Belarus‘,‘BY‘); insert into country (id, countryname, countrycode) values(18,‘Belgium‘,‘BE‘); insert into country (id, countryname, countrycode) values(19,‘Belize‘,‘BZ‘); insert into country (id, countryname, countrycode) values(20,‘Benin‘,‘BJ‘); insert into country (id, countryname, countrycode) values(21,‘Bermuda Is.‘,‘BM‘); insert into country (id, countryname, countrycode) values(22,‘Bolivia‘,‘BO‘); insert into country (id, countryname, countrycode) values(23,‘Botswana‘,‘BW‘); insert into country (id, countryname, countrycode) values(24,‘Brazil‘,‘BR‘); insert into country (id, countryname, countrycode) values(25,‘Brunei‘,‘BN‘); insert into country (id, countryname, countrycode) values(26,‘Bulgaria‘,‘BG‘); insert into country (id, countryname, countrycode) values(27,‘Burkina-faso‘,‘BF‘); insert into country (id, countryname, countrycode) values(28,‘Burma‘,‘MM‘); insert into country (id, countryname, countrycode) values(29,‘Burundi‘,‘BI‘); insert into country (id, countryname, countrycode) values(30,‘Cameroon‘,‘CM‘); insert into country (id, countryname, countrycode) values(31,‘Canada‘,‘CA‘); insert into country (id, countryname, countrycode) values(32,‘Central African Republic‘,‘CF‘); insert into country (id, countryname, countrycode) values(33,‘Chad‘,‘TD‘); insert into country (id, countryname, countrycode) values(34,‘Chile‘,‘CL‘); insert into country (id, countryname, countrycode) values(35,‘China‘,‘CN‘); insert into country (id, countryname, countrycode) values(36,‘Colombia‘,‘CO‘); insert into country (id, countryname, countrycode) values(37,‘Congo‘,‘CG‘); insert into country (id, countryname, countrycode) values(38,‘Cook Is.‘,‘CK‘); insert into country (id, countryname, countrycode) values(39,‘Costa Rica‘,‘CR‘); insert into country (id, countryname, countrycode) values(40,‘Cuba‘,‘CU‘); insert into country (id, countryname, countrycode) values(41,‘Cyprus‘,‘CY‘); insert into country (id, countryname, countrycode) values(42,‘Czech Republic‘,‘CZ‘); insert into country (id, countryname, countrycode) values(43,‘Denmark‘,‘DK‘); insert into country (id, countryname, countrycode) values(44,‘Djibouti‘,‘DJ‘); insert into country (id, countryname, countrycode) values(45,‘Dominica Rep.‘,‘DO‘); insert into country (id, countryname, countrycode) values(46,‘Ecuador‘,‘EC‘); insert into country (id, countryname, countrycode) values(47,‘Egypt‘,‘EG‘); insert into country (id, countryname, countrycode) values(48,‘EI Salvador‘,‘SV‘); insert into country (id, countryname, countrycode) values(49,‘Estonia‘,‘EE‘); insert into country (id, countryname, countrycode) values(50,‘Ethiopia‘,‘ET‘); insert into country (id, countryname, countrycode) values(51,‘Fiji‘,‘FJ‘); insert into country (id, countryname, countrycode) values(52,‘Finland‘,‘FI‘); insert into country (id, countryname, countrycode) values(53,‘France‘,‘FR‘); insert into country (id, countryname, countrycode) values(54,‘French Guiana‘,‘GF‘); insert into country (id, countryname, countrycode) values(55,‘Gabon‘,‘GA‘); insert into country (id, countryname, countrycode) values(56,‘Gambia‘,‘GM‘); insert into country (id, countryname, countrycode) values(57,‘Georgia‘,‘GE‘); insert into country (id, countryname, countrycode) values(58,‘Germany‘,‘DE‘); insert into country (id, countryname, countrycode) values(59,‘Ghana‘,‘GH‘); insert into country (id, countryname, countrycode) values(60,‘Gibraltar‘,‘GI‘); insert into country (id, countryname, countrycode) values(61,‘Greece‘,‘GR‘); insert into country (id, countryname, countrycode) values(62,‘Grenada‘,‘GD‘); insert into country (id, countryname, countrycode) values(63,‘Guam‘,‘GU‘); insert into country (id, countryname, countrycode) values(64,‘Guatemala‘,‘GT‘); insert into country (id, countryname, countrycode) values(65,‘Guinea‘,‘GN‘); insert into country (id, countryname, countrycode) values(66,‘Guyana‘,‘GY‘); insert into country (id, countryname, countrycode) values(67,‘Haiti‘,‘HT‘); insert into country (id, countryname, countrycode) values(68,‘Honduras‘,‘HN‘); insert into country (id, countryname, countrycode) values(69,‘Hongkong‘,‘HK‘); insert into country (id, countryname, countrycode) values(70,‘Hungary‘,‘HU‘); insert into country (id, countryname, countrycode) values(71,‘Iceland‘,‘IS‘); insert into country (id, countryname, countrycode) values(72,‘India‘,‘IN‘); insert into country (id, countryname, countrycode) values(73,‘Indonesia‘,‘ID‘); insert into country (id, countryname, countrycode) values(74,‘Iran‘,‘IR‘); insert into country (id, countryname, countrycode) values(75,‘Iraq‘,‘IQ‘); insert into country (id, countryname, countrycode) values(76,‘Ireland‘,‘IE‘); insert into country (id, countryname, countrycode) values(77,‘Israel‘,‘IL‘); insert into country (id, countryname, countrycode) values(78,‘Italy‘,‘IT‘); insert into country (id, countryname, countrycode) values(79,‘Jamaica‘,‘JM‘); insert into country (id, countryname, countrycode) values(80,‘Japan‘,‘JP‘); insert into country (id, countryname, countrycode) values(81,‘Jordan‘,‘JO‘); insert into country (id, countryname, countrycode) values(82,‘Kampuchea (Cambodia )‘,‘KH‘); insert into country (id, countryname, countrycode) values(83,‘Kazakstan‘,‘KZ‘); insert into country (id, countryname, countrycode) values(84,‘Kenya‘,‘KE‘); insert into country (id, countryname, countrycode) values(85,‘Korea‘,‘KR‘); insert into country (id, countryname, countrycode) values(86,‘Kuwait‘,‘KW‘); insert into country (id, countryname, countrycode) values(87,‘Kyrgyzstan‘,‘KG‘); insert into country (id, countryname, countrycode) values(88,‘Laos‘,‘LA‘); insert into country (id, countryname, countrycode) values(89,‘Latvia‘,‘LV‘); insert into country (id, countryname, countrycode) values(90,‘Lebanon‘,‘LB‘); insert into country (id, countryname, countrycode) values(91,‘Lesotho‘,‘LS‘); insert into country (id, countryname, countrycode) values(92,‘Liberia‘,‘LR‘); insert into country (id, countryname, countrycode) values(93,‘Libya‘,‘LY‘); insert into country (id, countryname, countrycode) values(94,‘Liechtenstein‘,‘LI‘); insert into country (id, countryname, countrycode) values(95,‘Lithuania‘,‘LT‘); insert into country (id, countryname, countrycode) values(96,‘Luxembourg‘,‘LU‘); insert into country (id, countryname, countrycode) values(97,‘Macao‘,‘MO‘); insert into country (id, countryname, countrycode) values(98,‘Madagascar‘,‘MG‘); insert into country (id, countryname, countrycode) values(99,‘Malawi‘,‘MW‘); insert into country (id, countryname, countrycode) values(100,‘Malaysia‘,‘MY‘); insert into country (id, countryname, countrycode) values(101,‘Maldives‘,‘MV‘); insert into country (id, countryname, countrycode) values(102,‘Mali‘,‘ML‘); insert into country (id, countryname, countrycode) values(103,‘Malta‘,‘MT‘); insert into country (id, countryname, countrycode) values(104,‘Mauritius‘,‘MU‘); insert into country (id, countryname, countrycode) values(105,‘Mexico‘,‘MX‘); insert into country (id, countryname, countrycode) values(106,‘Moldova, Republic of‘,‘MD‘); insert into country (id, countryname, countrycode) values(107,‘Monaco‘,‘MC‘); insert into country (id, countryname, countrycode) values(108,‘Mongolia‘,‘MN‘); insert into country (id, countryname, countrycode) values(109,‘Montserrat Is‘,‘MS‘); insert into country (id, countryname, countrycode) values(110,‘Morocco‘,‘MA‘); insert into country (id, countryname, countrycode) values(111,‘Mozambique‘,‘MZ‘); insert into country (id, countryname, countrycode) values(112,‘Namibia‘,‘NA‘); insert into country (id, countryname, countrycode) values(113,‘Nauru‘,‘NR‘); insert into country (id, countryname, countrycode) values(114,‘Nepal‘,‘NP‘); insert into country (id, countryname, countrycode) values(115,‘Netherlands‘,‘NL‘); insert into country (id, countryname, countrycode) values(116,‘New Zealand‘,‘NZ‘); insert into country (id, countryname, countrycode) values(117,‘Nicaragua‘,‘NI‘); insert into country (id, countryname, countrycode) values(118,‘Niger‘,‘NE‘); insert into country (id, countryname, countrycode) values(119,‘Nigeria‘,‘NG‘); insert into country (id, countryname, countrycode) values(120,‘North Korea‘,‘KP‘); insert into country (id, countryname, countrycode) values(121,‘Norway‘,‘NO‘); insert into country (id, countryname, countrycode) values(122,‘Oman‘,‘OM‘); insert into country (id, countryname, countrycode) values(123,‘Pakistan‘,‘PK‘); insert into country (id, countryname, countrycode) values(124,‘Panama‘,‘PA‘); insert into country (id, countryname, countrycode) values(125,‘Papua New Cuinea‘,‘PG‘); insert into country (id, countryname, countrycode) values(126,‘Paraguay‘,‘PY‘); insert into country (id, countryname, countrycode) values(127,‘Peru‘,‘PE‘); insert into country (id, countryname, countrycode) values(128,‘Philippines‘,‘PH‘); insert into country (id, countryname, countrycode) values(129,‘Poland‘,‘PL‘); insert into country (id, countryname, countrycode) values(130,‘French Polynesia‘,‘PF‘); insert into country (id, countryname, countrycode) values(131,‘Portugal‘,‘PT‘); insert into country (id, countryname, countrycode) values(132,‘Puerto Rico‘,‘PR‘); insert into country (id, countryname, countrycode) values(133,‘Qatar‘,‘QA‘); insert into country (id, countryname, countrycode) values(134,‘Romania‘,‘RO‘); insert into country (id, countryname, countrycode) values(135,‘Russia‘,‘RU‘); insert into country (id, countryname, countrycode) values(136,‘Saint Lueia‘,‘LC‘); insert into country (id, countryname, countrycode) values(137,‘Saint Vincent‘,‘VC‘); insert into country (id, countryname, countrycode) values(138,‘San Marino‘,‘SM‘); insert into country (id, countryname, countrycode) values(139,‘Sao Tome and Principe‘,‘ST‘); insert into country (id, countryname, countrycode) values(140,‘Saudi Arabia‘,‘SA‘); insert into country (id, countryname, countrycode) values(141,‘Senegal‘,‘SN‘); insert into country (id, countryname, countrycode) values(142,‘Seychelles‘,‘SC‘); insert into country (id, countryname, countrycode) values(143,‘Sierra Leone‘,‘SL‘); insert into country (id, countryname, countrycode) values(144,‘Singapore‘,‘SG‘); insert into country (id, countryname, countrycode) values(145,‘Slovakia‘,‘SK‘); insert into country (id, countryname, countrycode) values(146,‘Slovenia‘,‘SI‘); insert into country (id, countryname, countrycode) values(147,‘Solomon Is‘,‘SB‘); insert into country (id, countryname, countrycode) values(148,‘Somali‘,‘SO‘); insert into country (id, countryname, countrycode) values(149,‘South Africa‘,‘ZA‘); insert into country (id, countryname, countrycode) values(150,‘Spain‘,‘ES‘); insert into country (id, countryname, countrycode) values(151,‘Sri Lanka‘,‘LK‘); insert into country (id, countryname, countrycode) values(152,‘St.Lucia‘,‘LC‘); insert into country (id, countryname, countrycode) values(153,‘St.Vincent‘,‘VC‘); insert into country (id, countryname, countrycode) values(154,‘Sudan‘,‘SD‘); insert into country (id, countryname, countrycode) values(155,‘Suriname‘,‘SR‘); insert into country (id, countryname, countrycode) values(156,‘Swaziland‘,‘SZ‘); insert into country (id, countryname, countrycode) values(157,‘Sweden‘,‘SE‘); insert into country (id, countryname, countrycode) values(158,‘Switzerland‘,‘CH‘); insert into country (id, countryname, countrycode) values(159,‘Syria‘,‘SY‘); insert into country (id, countryname, countrycode) values(160,‘Taiwan‘,‘TW‘); insert into country (id, countryname, countrycode) values(161,‘Tajikstan‘,‘TJ‘); insert into country (id, countryname, countrycode) values(162,‘Tanzania‘,‘TZ‘); insert into country (id, countryname, countrycode) values(163,‘Thailand‘,‘TH‘); insert into country (id, countryname, countrycode) values(164,‘Togo‘,‘TG‘); insert into country (id, countryname, countrycode) values(165,‘Tonga‘,‘TO‘); insert into country (id, countryname, countrycode) values(166,‘Trinidad and Tobago‘,‘TT‘); insert into country (id, countryname, countrycode) values(167,‘Tunisia‘,‘TN‘); insert into country (id, countryname, countrycode) values(168,‘Turkey‘,‘TR‘); insert into country (id, countryname, countrycode) values(169,‘Turkmenistan‘,‘TM‘); insert into country (id, countryname, countrycode) values(170,‘Uganda‘,‘UG‘); insert into country (id, countryname, countrycode) values(171,‘Ukraine‘,‘UA‘); insert into country (id, countryname, countrycode) values(172,‘United Arab Emirates‘,‘AE‘); insert into country (id, countryname, countrycode) values(173,‘United Kiongdom‘,‘GB‘); insert into country (id, countryname, countrycode) values(174,‘United States of America‘,‘US‘); insert into country (id, countryname, countrycode) values(175,‘Uruguay‘,‘UY‘); insert into country (id, countryname, countrycode) values(176,‘Uzbekistan‘,‘UZ‘); insert into country (id, countryname, countrycode) values(177,‘Venezuela‘,‘VE‘); insert into country (id, countryname, countrycode) values(178,‘Vietnam‘,‘VN‘); insert into country (id, countryname, countrycode) values(179,‘Yemen‘,‘YE‘); insert into country (id, countryname, countrycode) values(180,‘Yugoslavia‘,‘YU‘); insert into country (id, countryname, countrycode) values(181,‘Zimbabwe‘,‘ZW‘); insert into country (id, countryname, countrycode) values(182,‘Zaire‘,‘ZR‘); insert into country (id, countryname, countrycode) values(183,‘Zambia‘,‘ZM‘);
mapper:
package com.heli.mybatis.page.mapper;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import com.heli.mybatis.page.model.Country;
public interface CountryMapper {
List<Country> selectAll();
List<Country> selectAll(RowBounds rowBounds);
}
model
package com.heli.mybatis.page.model;
/**
* Description: Country
* Author: liuzh
* Update: liuzh(2014-06-06 13:38)
*/
public class Country {
private int id;
private String countryname;
private String countrycode;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCountryname() {
return countryname;
}
public void setCountryname(String countryname) {
this.countryname = countryname;
}
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
}
servlet
package com.heli.mybatis.page.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heli.mybatis.page.MybatisHelper;
import com.heli.mybatis.page.mapper.CountryMapper;
import com.heli.mybatis.page.model.Country;
public class CountryServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
queryCountry(req, resp);
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
/**
* 查询国家
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
private void queryCountry(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String str_pageNum = req.getParameter("pageNum");
String str_pageSize = req.getParameter("pageSize");
int pageNum = 0,
pageSize = 0;
try {
pageNum = Integer.parseInt(str_pageNum);
} catch (NumberFormatException e) {
req.setAttribute("err", "页码只能是大于0的整数,请重新输入!");
return;
}
try {
pageSize = Integer.parseInt(str_pageSize);
} catch (NumberFormatException e) {
req.setAttribute("err", "页面大小只能是大于0的整数,请重新输入!");
return;
}
SqlSession sqlSession = MybatisHelper.getSqlSession();
CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
try {
PageHelper.startPage(pageNum, pageSize);
List<Country> list = countryMapper.selectAll();
PageInfo<Country> page = new PageInfo(list);
req.setAttribute("page", page);
} catch (Exception e) {
req.setAttribute("err", "查询出错:" + e.getMessage());
}
}
}
MybatisHelper
package com.heli.mybatis.page;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
/**
* Description: MybatisHelper
* Author: liuzh
* Update: liuzh(2014-06-06 13:33)
*/
public class MybatisHelper {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//创建SqlSessionFactory
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
//创建数据库
SqlSession session = null;
try {
session = sqlSessionFactory.openSession();
Connection conn = session.getConnection();
// reader = Resources.getResourceAsReader("CreateDB.sql");
// ScriptRunner runner = new ScriptRunner(conn);
// runner.setLogWriter(null);
// runner.runScript(reader);
// reader.close();
} finally {
if (session != null) {
session.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取Session
* @return
*/
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="cacheEnabled" value="true" /> <setting name="lazyLoadingEnabled" value="false" /> <setting name="aggressiveLazyLoading" value="true" /> <setting name="logImpl" value="LOG4J" /> </settings> <typeAliases> <package name="com.heli.mybatis.page.model" /> </typeAliases> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql" /> <property name="pageSizeZero" value="true" /> <property name="reasonable" value="true" /> </plugin> </plugins> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="" value="" /> </transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/user" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/CountryMapper.xml" /> </mappers> </configuration>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>country</servlet-name> <servlet-class>com.heli.mybatis.page.servlet.CountryServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>country</servlet-name> <url-pattern>/country</url-pattern> </servlet-mapping> </web-app>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Mybatis分页插件 - 测试页面</title>
<style type="text/css">
* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -155px;
}
.footer, .push {
height: 155px;
}
table.gridtable {
font-family: verdana, arial, sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
margin: 5px auto;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
.middle {
text-align: center;
margin: 0 auto;
width: 600px;
height: auto;
}
.info {
font-size: 12px;
text-align: center;
line-height: 20px;
padding: 40px;
}
.info a {
margin: 0 10px;
text-decoration: none;
color: green;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="middle">
<c:if test="${page==null}">
<h1 style="padding: 0px 0 10px;">查询国家列表</h1>
</c:if>
<c:if test="${page!=null}">
<h1 style="padding: 0px 0 10px;">查询国家列表</h1>
</c:if>
<c:if test="${err!=null}">
<h3 style="color:red;">${err}</h3>
</c:if>
<form action="country" method="post">
<table class="gridtable" style="width:100%;">
<tr>
<th>页码:</th>
<td><input type="text" name="pageNum"/></td>
<th>页面大小:</th>
<td><input type="text" name="pageSize"/></td>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
</form>
<c:if test="${page!=null}">
<table class="gridtable" style="width:100%;">
<tr>
<th colspan="2">分页信息</th>
</tr>
<tr>
<th style="width: 300px;">当前页号</th>
<td>${page.pageNum}</td>
</tr>
<tr>
<th>页面大小</th>
<td>${page.pageSize}</td>
</tr>
<tr>
<th>起始行号(>)</th>
<td>${page.startRow}</td>
</tr>
<tr>
<th>终止行号(<=)</th>
<td>${page.endRow}</td>
</tr>
<tr>
<th>总结果数</th>
<td>${page.total}</td>
</tr>
<tr>
<th>总页数</th>
<td>${page.pages}</td>
</tr>
<tr>
<th>第一页</th>
<td>${page.firstPage}</td>
</tr>
<tr>
<th>前一页</th>
<td>${page.prePage}</td>
</tr>
<tr>
<th>下一页</th>
<td>${page.nextPage}</td>
</tr>
<tr>
<th>最后一页</th>
<td>${page.lastPage}</td>
</tr>
<tr>
<th>是否为第一页</th>
<td>${page.isFirstPage}</td>
</tr>
<tr>
<th>是否为最后一页</th>
<td>${page.isLastPage}</td>
</tr>
<tr>
<th>是否有前一页</th>
<td>${page.hasPreviousPage}</td>
</tr>
<tr>
<th>是否有下一页</th>
<td>${page.hasNextPage}</td>
</tr>
</table>
<table class="gridtable" style="width:100%;">
<thead>
<tr>
<th colspan="3">查询结果</th>
</tr>
<tr>
<th>ID</th>
<th>国家名</th>
<th>国家代码</th>
</tr>
</thead>
<tbody>
<c:forEach items="${page.list}" var="country">
<tr>
<td>${country.id}</td>
<td>${country.countryname}</td>
<td>${country.countrycode}</td>
</tr>
</c:forEach>
</tbody>
</table>
<table class="gridtable" style="width:100%;text-align: center;">
<tr>
<c:if test="${page.hasPreviousPage}">
<td><a href="${pageContext.request.contextPath}/country?pageNum=${page.prePage}&pageSize=${page.pageSize}">前一页</a></td>
</c:if>
<c:forEach items="${page.navigatepageNums}" var="nav">
<c:if test="${nav == page.pageNum}">
<td style="font-weight: bold;">${nav}</td>
</c:if>
<c:if test="${nav != page.pageNum}">
<td><a href="${pageContext.request.contextPath}/country?pageNum=${nav}&pageSize=${page.pageSize}">${nav}</a></td>
</c:if>
</c:forEach>
<c:if test="${page.hasNextPage}">
<td><a href="${pageContext.request.contextPath}/country?pageNum=${page.nextPage}&pageSize=${page.pageSize}">下一页</a></td>
</c:if>
</tr>
</table>
</c:if>
</div>
<div class="push"></div>
</div>
<div class="footer">
<div class="info">
<a href="http://git.oschina.net/free/Mybatis_PageHelper">Mybatis分页插件</a>
<a href="http://git.oschina.net/free/Mybatis-Sample">Mybatis-Sample</a>
<br/>
作者:<a href="http://blog.csdn.net/isea533" style="margin: 0;">@Isea533/abel533</a>
</div>
</div>
</body>
</html>
原文:http://my.oschina.net/ydsakyclguozi/blog/466054