PageHelper和Mybatis-plus分页的使用
一、PageHelper
① 准备sql
CREATE TABLE `t_user` (
`id` int NOT NULL COMMENT '主键id',
`name` varchar(255) DEFAULT NULL COMMENT '名字',
`age` int DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
② 创建springboot项目
③ 编写pom文件
<dependencies>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
主要是导入 pagehelper-spring-boot-starter
依赖
④ 编写application.yml 文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/zhuicat?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: 123456
server:
port: 8854
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
logging:
level:
com.example: debug
org.springframework: warn
⑤ 创建entity实体类User
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_user")
public class User {
@TableId
private Integer id;
private String name;
private Integer age;
}
创建 mapper文件 UserMapper
public interface UserMapper extends BaseMapper<User> {}
创建service文件 UserService
public interface UserService extends IService<User> {}
创建service实现类 UserServiceImpl
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}
创建controller层 UserController
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@GetMapping
public List<User> list(Integer page,Integer rows) {
PageHelper.startPage(page,rows);
List<User> userList = userService.list();
PageInfo<User> pageInfo = new PageInfo<>(userList);
long total = pageInfo.getTotal();
int pages = pageInfo.getPages();
return userList;
}
}
Soringboot主启动类 Application
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
⑥ 测试:
二、Mybatis-plus
我们在上面搭建的项目进行
① 之前的依赖已经导入了
主要就是:mybatis-plus-boot-starter
② application.yml
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
③ controller
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@GetMapping("/list")
public List<User> list2(Integer page,Integer rows) {
Page<User> pageUser = new Page<>(page,rows);
userService.page(pageUser,null);
List<User> userList = pageUser.getRecords();
long total = pageUser.getTotal();
return userList;
}
}
注意:这里的 Page 导入的包应该是 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
④:测试:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)