1、pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>springboot_pagehelper</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_pagehelper</name> <description>springboot整合pagehelper</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <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-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> <!--mysql驱动连接 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>2、application.properties
##端口号 server.port=8080 ##日志级别 logging.level.com.pbm.mapper.UserMapper=debug ##数据库url spring.datasource.url=jdbc:mysql://localhost:3306/springboot_learn?serverTimezone=UTC ##数据库用户名 spring.datasource.username=root ##数据库密码 spring.datasource.password=root ##数据库驱动 spring.datasource.driver-class-name=com.mysql.jdbc.Driver #pagehelper分页插件配置 pagehelper.helperDialect=mysql3、User.java
package com.pbm.entity; import org.apache.ibatis.type.Alias; /** * 实体类 * @author Administrator * */ @Alias("user") public class User { private int user_id; private String user_name; private String user_password; public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } }4、UserMapper.java
package com.pbm.mapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import com.github.pagehelper.Page; import com.pbm.entity.User; @Mapper //添加注解 public interface UserMapper { @Select("SELECT * FROM user") Page<User> getUserList(); }5、UserController.java
package com.pbm.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.pbm.entity.User; import com.pbm.mapper.UserMapper; @RestController public class UserController { @Autowired private UserMapper userMapper; @RequestMapping("/getUserList") public Page<User> getUserList(Integer pageNum,Integer pageSize){ PageHelper.startPage(pageNum, pageSize); Page<User> userList = userMapper.getUserList(); return userList; } }6、运行SpringbootPagehelperApplication.java
package com.pbm; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootPagehelperApplication { public static void main(String[] args) { SpringApplication.run(SpringbootPagehelperApplication.class, args); } }7、运行成功显示 http://localhost:8080/getUserList?pageNum=1&pageSize=2
想要SpringBoot使用Mybatis-PageHelper的项目源码,请关注微信公众号:没逛够,进行领取。