LiuShuaiDong: MybatisPlus的基本使用及分页插件的使用

1、先引入MybatisPlus所需要的依赖:

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>

2、在application.yml中配置数据源(数据库的基本信息)

spring:
  datasource:
    url: jdbc:mysql:///database?useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root



# 可以配置mybatis的mapper映射文件位置和别名的信息
mybatis:
  mapper-locations: classpath:mappers/*.xml
  type-aliases-package: com.lld.springbootproject.pojo


3、给配置类添加@MapperScan注解扫描接口位置,或者给每个接口添加@Mapper注解

@Configuration
@MapperScan("com.lls.springbootproject.mappers")
public class ProConfig {


    
}

4、Mapper接口(BaseMapper中含有现成的对于数据库单表操作的基本语句,继承之后直接调用即可)

public interface UserMapper extends BaseMapper<User> {

}

5、Service接口

@Service
public interface UserService extends IService<User> {

}

6、Service实现类

/*UserMapper指定用哪个mapper,User指定结果类型*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

7、Controller调用方法

 @Autowired
    UserService userService;
    
    
    @GetMapping("/getUserDetails")
    @ResponseBody
    public List<User> getUserDetails(){

        List<User> list = userService.list();
        return list;
    }

二、分页插件的使用

1、在configuration配置类中编写此方法

@Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor pagination = new PaginationInnerInterceptor();
        pagination.setOverflow(true);
        pagination.setMaxLimit(500L);
        interceptor.addInnerInterceptor(pagination);
        return interceptor;
    }

2、在业务代码中使用分页

/*设置pn第几页,2(一页显示几个)*/
Page<User> userPage = new Page<>(pn,2);

/*分页后的结果给page*/
Page<User> page = userService.page(userPage, null);

3、分页基本的属性含义

page.current:当前页

page.pages:总页数

page.total:总记录数

page.records:分页后的记录

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值