1.为什么要分页?
- 减少数据的处理量
- 使用Limit分页
select * from user limit startIndex,pageSize;
使用Mybatis实现分页,核心SQL
1.数据库文件-db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useEncoding=false&characterEncoding=UTF-8&serverTimezone=GMT
username=root
password=root
2.mybatis配置文件-mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><properties resource="db.properties"/><settings><!--标准的日志工厂实现-->
<!-- <setting name="logImpl" value="STDOUT_LOGGING"/>-->
<!-- 配置log4j为日志的实现--><setting name="logImpl" value="LOG4J" /></settings><typeAliases><typeAlias type="com.kuang.pojo.User" alias="User" /></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><mapper resource="com/kuang/dao/UserMapper.xml"/></mappers>
</configuration>
3.mybatis工具类MybatisUtils
package com.kuang.utils;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;
import java.io.InputStream;//sqlSessionFactory用来构建sqlSession
public class MybatisUtils {//提升SqlSessionFactory的作用域private static SqlSessionFactory sqlSessionFactory;static{try {
// 使用Mybatis第一步获取sqlSessionFactory对象String resource = "mybatis-config.xml";
// 通过Resources读取配置文件InputStream inputStream = Resources.getResourceAsStream(resource);
// 通过SqlSessionFactoryBuilder加载一个流,构建一个sqlSession工厂sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);}catch (IOException e){e.printStackTrace();}}// 既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。public static SqlSession getSqlSession(){return sqlSessionFactory.openSession();}
}
4. 实体类对象-User
package com.kuang.pojo;public class User {private int id;private String name;private String password;public User(int id, String name, String password) {this.id = id;this.name = name;this.password = password;}public User() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", password='" + password + '\'' +'}';}}
5.接口
- UserMapper
package com.kuang.dao;import com.kuang.pojo.User;import java.util.List;
import java.util.Map;public interface UserMapper {//分页查询用户List<User> getUserByLimit(Map<String,Integer> map);}
6.Mapper.xml
- UserMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.kuang.dao.UserMapper"><!-- <select id="getUserById" resultType="com.User" parameterType="Integer">-->
<!-- select id,name,pwd as password from user where id=#{id}-->
<!-- </select>--><!--结果集映射-->
<!-- id:对应下面的引用type:将结果集映射为什么类型,我们需要映射为一个User对象result:代表它的结果column:数据表中的字段名properties:对象中的属性名--><!-- 分页--><resultMap id="user1" type="User"><result column="pwd" property="password" /></resultMap><select id="getUserByLimit" parameterType="map" resultMap="user1" >select * from user limit #{startIndex},#{pageSize};</select></mapper>
7.测试类-UserDaoTest
package dao;import com.kuang.dao.UserMapper;
import com.kuang.pojo.User;
import com.kuang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;import java.util.HashMap;
import java.util.List;
import java.util.Map;public class UserDaoTest {@Testpublic void getUserByLimit(){SqlSession session = MybatisUtils.getSqlSession();UserMapper mapper = session.getMapper(UserMapper.class);Map<String, Integer> map = new HashMap<>();map.put("startIndex",2);map.put("pageSize",2);List<User> userByLimit = mapper.getUserByLimit(map);for (User u:userByLimit) {System.out.println(u);}session.close();}}
文件具体位置:
RowBounds方式
1.接口
//利用RowBounds分页查询用户List<User> getUserByRowBounds();
2.UserMapper.xml
<resultMap id="user1" type="User"><result column="pwd" property="password" /></resultMap><select id="getUserByRowBounds" resultMap="user1" >select * from user</select>
3.测试类
@Testpublic void getUserByRowBounds(){SqlSession session = MybatisUtils.getSqlSession();RowBounds rowBounds = new RowBounds(1, 2);List<User> list = session.selectList("com.kuang.dao.UserMapper.getUserByRowBounds",null,rowBounds);for (User u:list) {System.out.println(u);}session.close();}
分页插件
分页插件网址