mybatis+pageHelper实现分页

导入jar包

mybatis+pageHelper实现分页_第1张图片

配置mybatis.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="database.properties"></properties>

    <typeAliases>
        <typeAlias type="com.java.entity.User" alias="User"></typeAlias>

        <typeAlias type="com.java.entity.Role" alias="Role"></typeAlias>

        <typeAlias type="com.java.entity.One" alias="One"></typeAlias>

        <typeAlias type="com.java.entity.Two" alias="Two"></typeAlias>

        <typeAlias type="com.java.entity.Goods" alias="Goods"></typeAlias>
    </typeAliases>

    <!--配置pageHelper分页(注意: 一定要在typeAliases之后)-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper"></plugin>
    </plugins>

    <environments default="mysql">

        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"></property>
                <property name="url" value="${url}"></property>
                <property name="username" value="${username}"></property>
                <property name="password" value="${password}"></property>
            </dataSource>
        </environment>

    </environments>

    <mappers>
        <mapper resource="com/java/dao/User/UserDao.xml"></mapper>
        <mapper resource="com/java/dao/Role/RoleDao.xml"></mapper>
        <mapper resource="com/java/dao/One/OneDao.xml"></mapper>
        <mapper resource="com/java/dao/Two/TwoDao.xml"></mapper>
        <mapper resource="com/java/dao/Goods/GoodsDao.xml"></mapper>
    </mappers>

</configuration>

编写映射文件

<?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.java.dao.Goods.GoodsDao">

    <!--page_helper-->
    <select id="selectPageHelper" resultType="Goods" parameterType="Goods">
        SELECT * FROM goods
    </select>
</mapper>

编写实现类

package com.java.dao.Goods;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.entity.Goods;
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 org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsDao {
    private SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
        sqlSession = build.openSession();
    }

    @Test
    public void selectPageHelper() {
        PageHelper.startPage(2, 10);
        List<Goods> goodsList = sqlSession.selectList("com.java.dao.Goods.GoodsDao.selectPageHelper");
        PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsList);
        long total = pageInfo.getTotal();
        int pageNum = pageInfo.getPageNum();
        int pageSize = pageInfo.getPageSize();
        System.out.println("共有:" + total + " 条数据");
        System.out.println("当前第:" + pageNum + " 页");
        System.out.println("每页:" + pageSize + " 条数据");
        List<Goods> list = pageInfo.getList();
        list.forEach(temp -> System.out.println(temp));
    }
}

实现效果

mybatis+pageHelper实现分页_第2张图片

你可能感兴趣的:(mybatis,mybatis,mysql,java)