MyBatis學習筆記(使用阿里的分頁插件PageHelper)

一、項目環境:參考: MyBatis分頁實現

部分環境沒有列出,有需要請通過上面的連接查看 ↑

1. 阿里分頁原理

二、下載pagehelper的jar並導包

1. 導包

在這裏插入圖片描述

2. 修改mybatis.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入頭文件 -->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<properties resource="db.properties"></properties>
	
	<settings>
		<setting name="logImpl" value="LOG4J"/>
	</settings>

	<!-- ********************************************************** 註冊MyBatis Plugin -->
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
	</plugins>
	<!-- ********************************************************** -->
	
	<!-- 配置數據庫的連接
		 default:默認使用哪一個數據庫連接
	 -->
	<environments default="mysql">
		<environment id="mysql">
			<!-- 事務管理 使用JDBC的事務 -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- 使用什麼數據庫連接池 durid -->
			<dataSource type="POOLED">
				<property name="driver" value="${dirver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
			</dataSource>
		</environment>
	</environments>
	<!-- 配置映射 -->
	<mappers>
		<mapper resource="com/lasing/mapping/UserMapping.xml"/>
	</mappers>

</configuration>

3. 創建UserMapper

public interface UserMapper {
	public List<User> queryForPage(User user);
}

4. 創建UserMapping.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="com.lasing.mapper.UserMapper">

	<!-- 阿里模糊查詢 -->
	<select id="queryForPage" resultType="com.lasing.domain.User">
		select * from user
	</select>
	
</mapper>

5. 工具類PageBean

package com.lasing.utils;

public class PageBean {

	private Integer currentPage;
	private Integer startIndex;
	private Integer pageSize;
	private Integer totalCount;
	private Integer totalPage;
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
		this.startIndex = (this.currentPage-1)*this.pageSize;
	}
	public Integer getStartIndex() {
		return startIndex;
	}
	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
		//計算總頁數
		this.totalPage=(int) Math.ceil(this.totalCount*1.0/pageSize);
	}
	public Integer getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
}

6. 工具類MybatisUtil

package com.lasing.utils;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisUtil {

	static InputStream is = MybatisUtil.class.getResourceAsStream("/mybatis.cfg.xml");
	//1.得到sqlSessionFactory
	static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is );
	/**
	 * 得到Session
	 * @retrun
	 * */
	public static SqlSession openSession() {
		return factory.openSession();
	}
	/**
	 * 提交併關閉
	 * */
	public static void closeSession(SqlSession session) {
		session.commit();
		session.close();
	}
}

7. 測試MyBatisTest

package com.lasing.test;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lasing.domain.User;
import com.lasing.mapper.UserMapper;
import com.lasing.utils.MybatisUtil;
import com.lasing.utils.PageBean;

public class MyBatisTest {
	public static void main(String[] args) {
		SqlSession session = MybatisUtil.openSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		//使用阿里的分頁插件
		User user = new User();
		PageBean bean = new PageBean();
		bean.setPageSize(5);
		bean.setCurrentPage(1);
		/**
		 * 參數1:要查詢第幾頁
		 * 參數2:每頁條數
		 * 參數3:是否查詢總條數 默認爲true
		 * */
		Page<User> page = PageHelper.startPage(bean.getCurrentPage(), bean.getPageSize());
		List<User> list = mapper.queryForPage(user);
		for (User user2 : list) {
			System.out.println(user2.toString());
		}
		System.out.println("總條數" + page.getTotal());
		MybatisUtil.closeSession(session);
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章