---
title: "mybatis数据库分页"
author: "mazzystar"
date: "2012-07-12T18:41:08Z"
tags: ["MyBatis", "page"]
categories: ["编程语言"]
slug: "mybatis"
draft: false
lastmod: "2012-07-12T18:43:50Z"
id: 1587434
url: "https://mazzystar.iteye.com/blog/1587434"
---

其实有两部分改动(基于官网demo做的修改)： \[list] \[\*]增加分页插件，简单易用（自我感觉良好）。 \[\*]增加一个BaseMapper，把常用的方法抽象到此接口中，避免在多个接口中重复定义（相当于常见的dao基类）。 \[/list] 基于maven并使用内存数据库hsqldb，可直接运行junit测试。 分页的使用方法：在接口中增加Page参数，并设置当前页数和每页条数。 \[code="java"] public interface BaseMapper { String PO\_KEY = "po"; T get(Serializable pk); Page getPage(@Param(PageInterceptor.PAGE\_KEY) Page p, @Param(PO\_KEY) T obj); } \[/code] 测试类： \[code="java"] @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:applicationContext.xml"}) public class ItemServiceTest { @Autowired ItemService service; @Test public void testGetItem() { System.out.println(service.getItem(null).getProduct()); } @Test public void testGetPage() { Page p = new Page(); p.setCurrentPage(1); p.setSize(10); Item item = new Item(); item.setProductId("FI-SW-01"); service.getPage(p, item); System.out.println(p.getTotal() + " " + p.getResult().size()); for(Item i : p.getResult()) { System.out.println(i.getItemId()); } } } \[/code] ps：开始喜爱mybatis基于接口的方式了。