springmvc+mybatis 利用pagehelper分页

首先,在pom.xml文件中加入:

		
  			  com.github.pagehelper
   			  pagehelper
  			  4.1.4
		

然后,在resources中创建一个mybatis.config.xml文件:
  
  
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    


然后再配置mybatis,如:

	
	
		
		
		
		
	

这里配置和ssm整合是一样的,就是在这里加多一句


就这样子整合完成了。

然后写一个实体类:

package com.terry.entity;

/**
 * 分页查询
 * @author KamTo Hung
 *
 */
public class PageTerry {
			private int limit = 10;//每页数据的数量,默认为十条
			private int offset = 1;//第几页,默认为第一页
			public int getLimit() {
				return limit;
			}
			public void setLimit(int limit) {
				this.limit = limit;
			}
			public int getOffset() {
				return offset;
			}
			public void setOffset(int offset) {
				this.offset = offset;
			}
			@Override
			public String toString() {
				return "PageTerry [limit=" + limit + ", offset=" + offset + "]";
			}		
}

在mapping中:





	    select cinema_id as cinemaId,cinema_name as cinemaName,cinema_address as cinemaAddress,cinema_phone as cinemaPhone,cinema_message as cinemaMessage,cinema_score as cinemaScore from cinema 



这里和你自己原本查询的方法是一样的,没什么改变;

public interface Service {
	 List findList(Object query);
}

这里接口也没变化;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml",
		"classpath:spring-mybatis.xml" })
public class TestUserService extends AbstractJUnit4SpringContextTests {
	
	
	@Resource(name="cinemaService")
	private CinemaService cinemaService;
	
	private static final Logger LOGGER = Logger.getLogger(TestUserService.class);

	@Test
	public void test08(){
		PageTerry pageTerry = new PageTerry();
		PageHelper.startPage(pageTerry.getOffset(),pageTerry.getLimit());
		List list = cinemaService.findList(new Cinema());
		LOGGER.info("++++++++++++++++++++++++++++++++++++++++"+JSON.toJSONString(list));
	}
}

这里是测试类,重要的是
PageHelper.startPage(pageTerry.getOffset(),pageTerry.getLimit());

这条语句,第一个参数是第几页,第二个参数是每页显示的数据的数量;

结果为:

++++++++++++++++++++++++++++++++++++++++[{"cinemaAddress":"增城区增江街沿江东三路15号1978创意园B区1号","cinemaId":1,"cinemaMessage":"15:45,17:45,18:25,19:10,19:45,20:25,21:10,21:45,22:25","cinemaName":"1978电影城","cinemaPhone":"123456","cinemaScore":5},{"cinemaAddress":"番禺区亚运大道1号永旺梦乐城4F","cinemaId":2,"cinemaMessage":"15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45","cinemaName":"CGV星聚汇影城(永旺店)","cinemaPhone":"123456","cinemaScore":5},{"cinemaAddress":"白云区尚景街1号白云尚城文化活动中心 ","cinemaId":3,"cinemaMessage":"15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45","cinemaName":"IDC国际影城(白云尚城店) ","cinemaPhone":"123456","cinemaScore":5},{"cinemaAddress":"海珠区江燕路108号燕汇广场4楼 ","cinemaId":4,"cinemaMessage":"15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45,15:45","cinemaName":"SFC上影影城(海珠燕汇店)","cinemaPhone":"123456","cinemaScore":5}]

只有前五条数据,实现了分页。

在controller中,也是加入

PageHelper.startPage(pageTerry.getOffset(),pageTerry.getLimit());
就能实现分页。

至于要计算一共有多少页,可以先获取数据的总数,然后

int total;//获取总数
int size = 10;//每页显示10条数据
int page = total % size == 0 ? total / size : total / size +1;//如果能除尽,则刚好,否则加1页

我会把page(页数)的值传到前端,跟着列出页数。

当点击某一页的时候,就会把当前的页数传到后台,根据:

PageHelper.startPage(pageTerry.getOffset(),pageTerry.getLimit());//pageTerry.getOffset()为某一页页数的值

至于上一页和下一页的实现,就获取当前某一页页数的值加一即可。

具体的后台ssm框架利用pagehelper实现分页就这样子~

前端的分页明天再说~



你可能感兴趣的:(java)