1.引入依赖-pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
2.在application.yml配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mybaits?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
3.在config包下面增加在里面建一个MybatisPlus配置类 返回一个分页拦截器,
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
@MapperScan("com.mk.springboot.mapper")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
4. 增加user类(我这边使用类lombok )
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "users")
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
private String username;
private String pwd;
private String trueName;
private Integer sex;
private Integer age;
private Date birthday;
}
5.UserMapper接口
@Repository
public interface UserMapper extends BaseMapper<User> {
}
6. 直接上测试代码
@Resource
private UserMapper userMapper;
@Test
public void queryUserForPage(){
IPage<User> userPage = new Page<>(2, 2);
userPage = userMapper.selectPage(userPage, null);
List<User> list = userPage.getRecords();
for(User user : list){
System.out.println(user);
}
}