java maven mysql分页查询

      对与我们的需求来说,有时候查询的数据比较的多,这时就要用到分页查询来实现我们的目的,分页查询这种机制对于程序来说还是很重要的。接下来就是分页查询的具体实现方法,用的是PageHelper的框架!感觉挺好的,也很简单!

      首先要先添加jar包,在pom.xml:

                <dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>4.0.0</version>
		</dependency>

spring-mybatis:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自动扫描mapping.xml文件 -->
		<property name="mapperLocations" value="classpath:ndm/miniwms/mapping/*.xml"></property>
		<property name="plugins">
			<array>
				<bean class="com.github.pagehelper.PageHelper">
					<property name="properties">
						<value>
							dialect=hsqldb
						</value>
					</property>
				</bean>
			</array>
		</property>

	</bean>



配置完之后,要完成我们的简单操作,在dao包下写调用mapper的接口TestPageMapper.java:

public interface TestPageMapper {

	List<LocationDetails> selectUserName(); 
}

之后要用写mapper包下的.xml   TestPageMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="ndm.miniwms.dao.TestPageMapper">
	<resultMap id="BaseResultMap" type="ndm.miniwms.pojo.LocationDetails">
		<id column="id" property="id" jdbcType="INTEGER" />
		<result column="warehouse_id" property="warehouseId" jdbcType="INTEGER" />
		<result column="company_id" property="companyId" jdbcType="INTEGER" />
	</resultMap>

	<select id="selectUserName" resultMap="BaseResultMap">
		select * from location_details
	</select>
</mapper>

比较的简单,就一个简单的查询语句,查询所有的表。接下来就i是关键了,前面已经写完了,下来就是测试的步骤了。

写一个测试 类  TestPage.java

package cui;

import java.util.List;

import javax.annotation.Resource;
import javax.xml.registry.infomodel.User;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import ndm.miniwms.dao.TestPageMapper;
import ndm.miniwms.pojo.LocationDetails;

@RunWith(SpringJUnit4ClassRunner.class)     //表示继承了SpringJUnit4ClassRunner类  
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})  
public class TestPage {

	@Resource
	private TestPageMapper testPageMapper;
	
	@Test
	public void testPageByName() {
		Integer pageNo = 1;
		Integer pageSize = 3;
		PageHelper.startPage(pageNo, pageSize);
	    List<LocationDetails> list = testPageMapper.selectUserName();
	    //用PageInfo对结果进行包装
	    PageInfo<LocationDetails> page = new PageInfo<LocationDetails>(list);
	    //测试PageInfo全部属性
	    System.out.println(page.getPageNum());
	    System.out.println(page.getPageSize());
	    System.out.println(page.getStartRow());
	    System.out.println(page.getEndRow());
	    System.out.println(page.getTotal());
	    System.out.println(page.getPages());
	    System.out.println(page.getFirstPage());
	    System.out.println(page.getLastPage());
	    System.out.println(page.isHasPreviousPage());
	    System.out.println(page.isHasNextPage());
	    
	    for(LocationDetails locationDetails : list){
	    	System.out.println(locationDetails.getName());
	    }
	   
	}
}
 
     很好,接下来你就可以看出你只查询出了三条语句。想看mysql执行的,看上一篇转载。不是执行了所有,是只执行三条,具体看以看下框架 https://github.com/pagehelper/Mybatis-PageHelper 。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值