Mabits的PageHelper分頁外掛的使用和Jsp分頁頁面展示(帶原始碼)
阿新 • • 發佈:2018-12-23
1.在Maven使用分頁外掛,修改pom.xml檔案 匯入包,本次使用的是4.0.0的版本
<properties> <pagehelper.version>4.0.0</pagehelper.version> </properties> <dependencies> <!-- 分頁外掛 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> </dependencies>
2.修改MyBatis配置檔案(我的配置檔名稱為SqlMapConfig.xml)
<configuration> <plugins> <!-- com.github.pagehelper為PageHelper類所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 設定資料庫型別 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫--> <property name="dialect" value="mysql"/> </plugin> </plugins> </configuration>
3.建立PageBean物件,用於封裝查詢條件
import java.util.List; public class Pagebean { private int totalCount; //總條數 private int totalPage;//總頁數 總條數/每頁條數 private int sizePage=5;//每頁顯示數 預設 private int pageCode;//第幾頁 預設 private List rows;//查詢返回的結果集 public List getRows() { return rows; } public void setRows(List rows) { this.rows = rows; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return totalCount%sizePage==0?totalCount/sizePage:totalCount/sizePage+1;//總頁數 總條數/每頁條數 } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getSizePage() { return sizePage; } public void setSizePage(int sizePage) { this.sizePage = sizePage; } public int getPageCode() { return pageCode; } public void setPageCode(int pageCode) { this.pageCode = pageCode; } }
4.Jsp頁面核心程式碼編寫
index.jsp
<li><a href="/list.action?pageCode=1&sizePage=5" target="rightFrame">分頁檢視</a></li>
list.jsp
<body>
<div class="box-positon">
<div class="rpos">當前位置: 品牌管理 - 列表</div>
<form class="ropt">
<input class="add" type="button" value="新增"
onclick="javascript:window.location.href='add.jsp'" />
</form>
<div class="clear"></div>
</div>
<div class="body-box">
<form action="v_list.shtml" id="brandForm" method="post"
style="padding-top: 5px;">
品牌名稱: <input type="text" name="name" /> <select>
<option value="1">是</option>
<option>不是</option>
</select> <input type="submit" class="query" value="查詢" />
</form>
<table cellspacing="1" cellpadding="0" border="0" width="100%"
class="pn-ltable">
<thead class="pn-lthead">
<tr>
<th width="20"><input type="checkbox"
onclick="checkBox('ids',this.checked)" /></th>
<th>品牌ID</th>
<th>品牌名稱</th>
<th>品牌圖片</th>
<th>品牌描述</th>
<th>排序</th>
<th>是否可用</th>
<th>操作選項</th>
</tr>
</thead>
<tbody class="pn-ltbody">
<c:forEach items="${page.rows}" var="brand">
<tr bgcolor="#ffffff" onmouseover="this.bgColor='#eeeeee'"
onmouseout="this.bgColor='#ffffff'">
<td><input type="checkbox" name="ids" value="${brand.id }" /></td>
<td align="center">${brand.id }</td>
<td align="center">${brand.name }</td>
<td align="center"><img src="${brand.imgUrl}" width="40"
height="40" /></td>
<td align="center">${brand.description}</td>
<td align="center">${brand.sort}</td>
<td align="center"><c:if test="${brand.isDisplay == 1 }">可用</c:if>
<c:if test="${brand.isDisplay == 0 }">不可用</c:if></td>
<td align="center"><a href="javascript:void(0);"
class="pn-opt">修改</a> | <a href=" javascript:void(0);"
onclick="if(!confirm('您確定刪除嗎?')) {return false;}" class="pn-opt">刪除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!--分頁部分-->
<tr align="left">
<td><sapn id=pagelink>
<div style="line-height: 20px;height: 20px;text-align: center;font-size: 18px ">
共 [ ${page.totalCount} ] 條記錄,共 [ ${page.totalPage} ] 頁
<c:if test="${ page.pageCode > 1 }">
[ <A href="/brand/list.action?pageCode=${page.pageCode-1}&sizePage=5">前一頁</A> ]
</c:if>
<c:if test="${ page.pageCode <=1 }">
[ 前一頁 ]
</c:if>
,當前第 [ ${page.pageCode} ] 頁
<c:if test="${ page.pageCode < page.totalPage }">
,[ <A href="/brand/list.action?pageCode=${page.pageCode+1}&sizePage=5">後一頁</A> ]
</c:if>
<c:if test="${ page.pageCode >= page.totalPage }">
[ 後一頁 ]
</c:if>
</div>
</span></td>
</tr>
<div style="margin-top: 15px;">
<input class="del-button" type="button" value="刪除"
onclick="optDelete();" />
</div>
</div>
</body>
5.Controller類編寫
@Controller
public class BrandController {
@Autowired
private BrandService brandService;
@RequestMapping("/list")
public String list(int pageCode, int sizePage,ModelMap model){
//建立一個傳遞引數的物件
BbsBrand brand = new BbsBrand();//這個是一個查詢返回的物件
//設定顯示或不顯示的查詢條件 1:顯示 0: 不顯示
brand.setName("name");//通過name查詢
//service層方法處理,分頁進行查詢
Pagebean page = brandService.getBrandList(pageCode, sizePage, brand);
model.addAttribute("page",page);
//將結果返回頁面 ,並取名 brands
//model.addAttribute("brands", brands);
return "list";
}
}
6.寫Service層
在service類中PageHelper.startPage(pageCode, sizePage);方法,第一個引數為查詢頁數,第二個為每頁條數
介面:
public interface BrandService {
public Pagebean getBrandList(int pageCode,int sizePage,BbsBrand brand);
}
實現類:
@Service
public class BrandServiceImpl implements BrandService{
@Autowired
private BbsBrandMapper brandMapper;
//分頁對品牌進行查詢
public Pagebean getBrandList(int pageCode, int sizePage,BbsBrand brand) {
System.out.println("service"+pageCode);
PageHelper.startPage(pageCode, sizePage);
List<BbsBrand> list = brandMapper.selectBrandList(brand);//呼叫dao進行查詢
//建立返回結果集
Pagebean bean=new Pagebean();
bean.setRows(list);//將結果封裝
//獲取總記錄數
PageInfo<BbsBrand> pageInfo=new PageInfo<BbsBrand>(list);
bean.setTotalCount((int)pageInfo.getTotal());//返回總條數
bean.setSizePage(sizePage);//設定每頁條數
bean.setPageCode(pageCode);//設定第幾頁
return bean;
}
}
7.Mapper的建立
public interface BbsBrandMapper {
public List<BbsBrand> selectBrandList(BbsBrand brand);
}
<mapper namespace="com.lhzlx.dao.BbsBrandMapper" >
<!--返回結果 與 javaBean物件中屬性 對映關係 -->
<resultMap id="brand" type="com.lhzlx.pojo.BbsBrand">
<result property="id" column="id" />
<result property="name" column="name" />
<result property="description" column="description" />
<result property="imgUrl" column="img_url" />
<result property="sort" column="sort" />
<result property="isDisplay" column="is_display" />
</resultMap>
<!-- 查詢所有品牌的所有屬性-->
<select id="selectBrandList" parameterType="com.lhzlx.pojo.BbsBrand" resultMap="brand">
select id,name,description,img_url,sort,is_display from bbs_brand where is_display = #{isDisplay}
</select>
</mapper>
8.建立資料庫 插入資料
CREATE TABLE `bbs_brand` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`name` varchar(40) NOT NULL COMMENT '名稱',
`description` varchar(80) DEFAULT NULL COMMENT '描述',
`img_url` varchar(80) DEFAULT NULL COMMENT '圖片Url',
`sort` int(11) DEFAULT NULL COMMENT '排序:最大最排前',
`is_display` tinyint(1) DEFAULT NULL COMMENT '是否可見 1:可見 0:不可見',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='品牌';
insert into `bbs_brand`(`id`,`name`,`description`,`img_url`,`sort`,`is_display`) values (1,'依琦蓮','依琦蓮','image/123.jpg',1,1),
(2,'凱速(KANSOON)','凱速(KANSOON)','image/123.jpg',2,1),(3,'梵歌納(vangona)','梵歌納(vangona)','image/123.jpg',3,1),
(4,'伊朵蓮','伊朵蓮','image/123.jpg',4,1),(5,'菩媞','菩媞','image/123.jpg',5,1),(6,'丹璐斯','丹璐斯','image/123.jpg',6,1),
(7,'喜悅瑜伽','喜悅瑜伽','image/123.jpg',7,1),(8,'金樂樂','金樂樂','image/123.jpg',8,1);