idea整合mybatis实现简单分页
数据库 mysql 5.6.17
pagehelper 1.2.10
jdk 1.8
最简单分页教
以上就是分页的效果图
下面是代码
CREATE TABLE `sys_log` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', `operate_type` varchar(200) DEFAULT NULL COMMENT '网站操作类型', `ip_address_host` varchar(200) DEFAULT NULL COMMENT '操作者的地址', `operate_time` varchar(200) DEFAULT NULL COMMENT '操作时间', `operate_user` varchar(200) DEFAULT NULL COMMENT '操作人', `address` varchar(200) DEFAULT NULL COMMENT '真实地址', `log_signd` varchar(20) DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8;
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.fenye</groupId> <artifactId>mybatis-fenye</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.10</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--springboot整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <!-- mybatis pagehelper 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>5.7.0</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.7.2</version> </dependency> <!--解决单元测试启动出现的问题结束--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
server.port=3001 logging.level.web=debug logging.com.fenye=debug spring.devtools.add-properties=false spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/yourheart-test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=123 spring.datasource.hikari.connection-timeout=600000 spring.datasource.hikari.maximum-pool-size=500 spring.datasource.hikari.minimum-idle=20 mybatis.mapper-locations=classpath:mapping/*.xml mybatis.configuration.map-underscore-to-camel-case=true pagehelper.auto-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true
下面是完整代码
package com.fenye; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Description * @Author qiuxie * @Date 2021/11/29 21:50 */ @SpringBootApplication public class FenYeApplication { public static void main(String[] args) { SpringApplication.run(FenYeApplication.class,args); } } /** * Project Name:tec * File Name:SysLog.java * Package Name:com.java.bean * Date:下午10:22:16 * Copyright (c) 2020, bluemobi All Rights Reserved. * */ package com.fenye.bean; import lombok.Data; /** * Description: <br/> * Date: 下午10:22:16 <br/> * * @author 喵星人 * @version * @see */ @Data public class SysLog { private Integer id; private String operateType; private String ipAddressHost; private String operateTime; private String operateUser; private String address; } package com.fenye.mapper; import com.fenye.bean.SysLog; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; /** * @Description * @Author qiuxie * @Date 2021/11/29 22:05 */ @Mapper public interface SysLogMapper { @Select("SELECT * FROM sys_log ORDER BY operate_time DESC") List<SysLog> getSysLogList(); } package com.fenye.controller.front; import com.fenye.bean.SysLog; import com.fenye.mapper.SysLogMapper; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @Description * @Author qiuxie * @Date 2021/11/29 22:16 */ @RestController public class SysLogController { @Autowired private SysLogMapper sysLogMapper; @RequestMapping("/getSysLogList") public PageInfo<SysLog> getSysLogList(int pageSize, int pageNum) { PageHelper.startPage(pageNum,PageSize); List<SysLog> sysLogList = sysLogMapper.getSysLogList(); // PageInfo<SysLog> sysLogPageInfo = PageInfo.of(sysLogList); // return sysLogPageInfo; PageInfo<SysLog> pageInfo = new PageInfo<>(sysLogList); return pageInfo; } }
其中pageNum为哪一页,pageZise为每一页的记录数