1. 程式人生 > >java maven mysql分頁查詢

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。