springboot整合mybatis-plus实现分页

创建项目,项目目录如下:
springboot整合mybatis-plus实现分页_第1张图片
添加依赖


        
            org.springframework.boot
            spring-boot-starter-web
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.2
        
        
            org.projectlombok
            lombok
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
    

修改配置文件,将配置文件后缀改为.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useEncode=true&useSSL=false&characterEncoding=utf8
    username: root
    password: root


user:
  id: 2
  name: Oliver
  age: 22
  email: [email protected]

创建一个MybatisPlus分页的拦截器MybatisPlusConfig,如果不创建,MybatisPlus就不会在sql上增加分页的语句

@Configuration
@MapperScan("com.example.mybatisplusdemo.mapper")
public class MybatisPlusConfig {
	/**
     * 分页插件
     * @return PaginationInterceptor
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

建立实体类User

@Data  //自动创建setter、getter、toString等方法
@AllArgsConstructor  //自动创建全参构造函数
@NoArgsConstructor  //自动创建无参构造函数
@Component

//采用配置文件中自动注入的方式赋值,将配置文件中的值赋给User的对应属性
@ConfigurationProperties(prefix = "user")  
@Validated   //校验
public class User {
    private Long id;
    private String name;
    private Integer age;
    @Email  //校验email是否合法
    private String email;
}

建立mapper,UserMapper继承BaseMapper,BaseMapper中有mabtis-plus已经为我们写好的一部分方法:

public interface UserMapper extends BaseMapper<User> {
}

BaseMapper源码如下:

public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);

    int deleteById(Serializable id);

    int deleteByMap(@Param("cm") Map<String, Object> columnMap);

    int delete(@Param("ew") Wrapper<T> wrapper);

    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    int updateById(@Param("et") T entity);

    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);

    T selectById(Serializable id);

    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);

    T selectOne(@Param("ew") Wrapper<T> queryWrapper);

    Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);

    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);

    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);

    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);

    IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);

    IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
}

修改启动类,添加@MapperScan注解

@SpringBootApplication
@MapperScan("com.example.mybatisplusdemo.mapper")
public class MybatisPlusDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusDemoApplication.class, args);
    }

}

建立service

public interface UserService {
    public IPage<User> selectPage(long page, long size);
}

建立serviceImpl

public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public IPage<User> selectPage(long page, long size) {
        IPage<User> iPage = userMapper.selectPage(new Page<>(page, size), null);
        return iPage;
    }
}

controller

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/userList")
    public List<User> userList(@Param("page") long page) {
        long size = 3;
        IPage<User> iPage = userService.selectPage(page, size);
        return iPage.getRecords();
    }
}

结果:
在这里插入图片描述

你可能感兴趣的:(Java)