Logstash Apache 自承式光缆 post silverlight webview 网页后台模板 jquery遍历对象 jquery事件绑定方法 jq获取最后一个子元素 两个正态分布相乘 map删除指定元素 mser算法 mysql分区表优劣分析 python面向对象 java中的数据结构 java定义变量 java文件读写 变量的类型 java异常处理 java文件复制 火牛软件 微信助力软件 图解设计模式 微信客户管理系统 思源黑体cn 魔兽七个人 medcalc 幽灵行动多少钱 重复文件查找软件 死从天降成就 类似迅雷的下载软件 winrar无广告版 达芬奇resolve vba下载 mw3 百小度 标准色 老人手机哪款最好 如何查看路由器密码
当前位置: 首页 > 学习教程  > 编程语言

分页插件 Mybatis-PageHelper

2021/1/28 22:39:16 文章标签:

MyBatis 的分页插件 Mybatis-PageHelper 官方文档&#xff1a; https://github.com/pagehelper/Mybatis-PageHelper/ 看下如何使用。 1、Maven 依赖 <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-ins…

MyBatis 的分页插件 Mybatis-PageHelper

官方文档:

https://github.com/pagehelper/Mybatis-PageHelper/

 

看下如何使用。

1、Maven 依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>constxiong</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>mybatis-pageHelper</artifactId>

    <dependencies>
        <!-- MySQL 8.0.21 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>

        <!-- MyBatis -->
        <dependency>
		  <groupId>org.mybatis</groupId>
		  <artifactId>mybatis</artifactId>
		  <version>3.5.5</version>
		</dependency>

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

    </dependencies>

    <build>

    </build>
</project>

 

2、MyBatis 的配置文件 mybatis-config.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>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <property name="helperDialect" value="mysql"/>
        </plugin>
    </plugins>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://172.31.32.184:3306/constxiong?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="constxiong@123"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="constxiong/mapper/UserMapper.xml"/>
    </mappers>
</configuration>

 

3、Mapper 接口 UserMapper

package constxiong.mapper;

import constxiong.po.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * UserMapper 接口,映射对数据库的操作
 */
public interface UserMapper {

    List<User> selectUsers();

    int insertUser(User user);

    int deleteUsers();
}

 

4、Mapper 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="constxiong.mapper.UserMapper">

    <select id="selectUsers" resultType="constxiong.po.User">
        select * from user
    </select>

    <insert id="insertUser" parameterType="constxiong.po.User">
        insert into user values(#{id}, #{name})
    </insert>

    <delete id="deleteUsers">
      delete from user
    </delete>

</mapper>

 

5、User po 类

package constxiong.po;

/**
 * 用户表模型
 */
public class User {
    private Integer id;

    private String name;

    private String mc;

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMc() {
        return mc;
    }

    public void setMc(String mc) {
        this.mc = mc;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", mc='" + mc + '\'' +
                '}';
    }
}

 

6、测试代码

package constxiong;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import constxiong.mapper.UserMapper;
import constxiong.po.User;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.*;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 测试 Mybatis 分页插件
 */
public class Test {

    public static void main(String[] args) throws IOException {
        testXmlConfigAndPager();
    }

    /**
     * 测试 xml 配置方式分页
     */
    private static void testXmlConfigAndPager() throws IOException {
        String resource = "mybatis-config.xml";//xml配置文件路径
        InputStream inputStream = Resources.getResourceAsStream(resource);//读取配置
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//构建 SqlSessionFactory
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//通过 SqlSessionfactory 获取 SqlSession

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);//通过 SqlSession 获取 Mapper 接口

        //删除 user
        userMapper.deleteUsers();

        //插入 user
        for (int i = 1; i <= 35; i++) {
            userMapper.insertUser(new User(i, "ConstXiong" + i));
        }

        //查询所有 user
        List<User> users = userMapper.selectUsers();
        System.out.println(users);

        //未打开 mybatis-config.xml 中 plugins 标签使用内存分页;打开标签使用插件的物理分页
        int pageNo = 2;
        int pageSize = 10;
        users = sqlSession.selectList("constxiong.mapper.UserMapper.selectUsers", null, new RowBounds((pageNo - 1) * pageSize, pageSize));
        System.out.println(users);

        //使用静态方法 startPage
        PageHelper.startPage(pageNo, pageSize);
        users = userMapper.selectUsers();
        System.out.println(users);

        //使用静态方法 offsetPage
        PageHelper.offsetPage(pageNo, pageSize);
        users = userMapper.selectUsers();
        System.out.println(users);

        //匿名类
        Page<User> page = PageHelper.startPage(pageNo, pageSize).doSelectPage(new ISelect() {
            @Override
            public void doSelect() {
                userMapper.selectUsers();
            }
        });
        System.out.println(page);
        // lambda
        page = PageHelper.startPage(pageNo, pageSize).doSelectPage(() -> {
            userMapper.selectUsers();
        });
        System.out.println(page);

        //仅合计
        long count = PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                userMapper.selectUsers();
            }
        });
        System.out.println(count);
        //lambda
        count = PageHelper.count(() -> {
            userMapper.selectUsers();
        });
        System.out.println(count);

        //关闭 SqlSession
        sqlSession.close();
    }

}

 

7、打印

[User{id=1, name='ConstXiong1', mc='null'}, User{id=2, name='ConstXiong2', mc='null'}, User{id=3, name='ConstXiong3', mc='null'}, User{id=4, name='ConstXiong4', mc='null'}, User{id=5, name='ConstXiong5', mc='null'}, User{id=6, name='ConstXiong6', mc='null'}, User{id=7, name='ConstXiong7', mc='null'}, User{id=8, name='ConstXiong8', mc='null'}, User{id=9, name='ConstXiong9', mc='null'}, User{id=10, name='ConstXiong10', mc='null'}, User{id=11, name='ConstXiong11', mc='null'}, User{id=12, name='ConstXiong12', mc='null'}, User{id=13, name='ConstXiong13', mc='null'}, User{id=14, name='ConstXiong14', mc='null'}, User{id=15, name='ConstXiong15', mc='null'}, User{id=16, name='ConstXiong16', mc='null'}, User{id=17, name='ConstXiong17', mc='null'}, User{id=18, name='ConstXiong18', mc='null'}, User{id=19, name='ConstXiong19', mc='null'}, User{id=20, name='ConstXiong20', mc='null'}, User{id=21, name='ConstXiong21', mc='null'}, User{id=22, name='ConstXiong22', mc='null'}, User{id=23, name='ConstXiong23', mc='null'}, User{id=24, name='ConstXiong24', mc='null'}, User{id=25, name='ConstXiong25', mc='null'}, User{id=26, name='ConstXiong26', mc='null'}, User{id=27, name='ConstXiong27', mc='null'}, User{id=28, name='ConstXiong28', mc='null'}, User{id=29, name='ConstXiong29', mc='null'}, User{id=30, name='ConstXiong30', mc='null'}, User{id=31, name='ConstXiong31', mc='null'}, User{id=32, name='ConstXiong32', mc='null'}, User{id=33, name='ConstXiong33', mc='null'}, User{id=34, name='ConstXiong34', mc='null'}, User{id=35, name='ConstXiong35', mc='null'}]
Page{count=false, pageNum=2, pageSize=10, startRow=10, endRow=20, total=-1, pages=1, reasonable=false, pageSizeZero=false}[User{id=11, name='ConstXiong11', mc='null'}, User{id=12, name='ConstXiong12', mc='null'}, User{id=13, name='ConstXiong13', mc='null'}, User{id=14, name='ConstXiong14', mc='null'}, User{id=15, name='ConstXiong15', mc='null'}, User{id=16, name='ConstXiong16', mc='null'}, User{id=17, name='ConstXiong17', mc='null'}, User{id=18, name='ConstXiong18', mc='null'}, User{id=19, name='ConstXiong19', mc='null'}, User{id=20, name='ConstXiong20', mc='null'}]
Page{count=true, pageNum=2, pageSize=10, startRow=10, endRow=20, total=35, pages=4, reasonable=false, pageSizeZero=false}[User{id=11, name='ConstXiong11', mc='null'}, User{id=12, name='ConstXiong12', mc='null'}, User{id=13, name='ConstXiong13', mc='null'}, User{id=14, name='ConstXiong14', mc='null'}, User{id=15, name='ConstXiong15', mc='null'}, User{id=16, name='ConstXiong16', mc='null'}, User{id=17, name='ConstXiong17', mc='null'}, User{id=18, name='ConstXiong18', mc='null'}, User{id=19, name='ConstXiong19', mc='null'}, User{id=20, name='ConstXiong20', mc='null'}]
Page{count=true, pageNum=2, pageSize=10, startRow=2, endRow=12, total=35, pages=4, reasonable=false, pageSizeZero=false}[User{id=3, name='ConstXiong3', mc='null'}, User{id=4, name='ConstXiong4', mc='null'}, User{id=5, name='ConstXiong5', mc='null'}, User{id=6, name='ConstXiong6', mc='null'}, User{id=7, name='ConstXiong7', mc='null'}, User{id=8, name='ConstXiong8', mc='null'}, User{id=9, name='ConstXiong9', mc='null'}, User{id=10, name='ConstXiong10', mc='null'}, User{id=11, name='ConstXiong11', mc='null'}, User{id=12, name='ConstXiong12', mc='null'}]
Page{count=true, pageNum=2, pageSize=10, startRow=10, endRow=20, total=35, pages=4, reasonable=false, pageSizeZero=false}[User{id=11, name='ConstXiong11', mc='null'}, User{id=12, name='ConstXiong12', mc='null'}, User{id=13, name='ConstXiong13', mc='null'}, User{id=14, name='ConstXiong14', mc='null'}, User{id=15, name='ConstXiong15', mc='null'}, User{id=16, name='ConstXiong16', mc='null'}, User{id=17, name='ConstXiong17', mc='null'}, User{id=18, name='ConstXiong18', mc='null'}, User{id=19, name='ConstXiong19', mc='null'}, User{id=20, name='ConstXiong20', mc='null'}]
Page{count=true, pageNum=2, pageSize=10, startRow=10, endRow=20, total=35, pages=4, reasonable=false, pageSizeZero=false}[User{id=11, name='ConstXiong11', mc='null'}, User{id=12, name='ConstXiong12', mc='null'}, User{id=13, name='ConstXiong13', mc='null'}, User{id=14, name='ConstXiong14', mc='null'}, User{id=15, name='ConstXiong15', mc='null'}, User{id=16, name='ConstXiong16', mc='null'}, User{id=17, name='ConstXiong17', mc='null'}, User{id=18, name='ConstXiong18', mc='null'}, User{id=19, name='ConstXiong19', mc='null'}, User{id=20, name='ConstXiong20', mc='null'}]
35
35

 

完成代码:

https://github.com/ConstXiong/toy/

 

 


【Java学习资源】整理推荐

  • Java 8 Stream API Demo
  • Druid 使用
  • 借助 libreoffice,word 转 pdf 体验
  • Java 动态代理
  • openjdk jol 工具打印 Java 对象内存布局
  • Spring整合MyBatis
  • MyBatis 自定义插件
  • MyBatis 延迟加载
  • MyBatis 1对1 1对多 关联查询
  • MyBatis insert 获取自增主键
  • MyBatis 批量插入
  • 分页插件 Mybatis-PageHelper
  • MyBatis 原生使用
  • Sharding-JDBC 实现分库分表
  • JDK1.8 中的日期与时间 API
  • Sharding-JDBC 实现读写分离

 

 


【Java面试题与答案】整理推荐

  • 基础与语法
  • 集合
  • 网络编程
  • 并发编程
  • Web
  • 安全
  • 设计模式
  • 框架
  • 算法与数据结构
  • 异常
  • 文件解析与生成
  • Linux
  • MySQL
  • Oracle
  • Redis
  • Dubbo

 


本文链接: http://www.dtmao.cc/news_show_650052.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?