mybatis-plus中的BaseMapper、queryMapper、主鍵生成策略、以及分頁

在MybatisPlus中, BaseMapper中定義了一些常用的CRUD方法 ,當我們自定義的Mapper接口繼承BaseMapper
後即可擁有了這些方法。
需要說明的是:這些方法僅適合單表操作

1、方法截圖

可以查看源碼,看到這些方法,
在這裏插入圖片描述

2、queryMapper

這裏的queryMapper可以理解爲是一個條件
工程目錄與上一篇博客的相同

@Test
public void testLike(){
     QueryWrapper<Account> queryWrapper = new QueryWrapper<Account>();
     queryWrapper = queryWrapper.like("name","a");
     List<Account> list = this.accountMapper.selectList(queryWrapper);
     System.out.println(list);
 }

在這裏插入圖片描述
具體的方法可查看
https://mp.baomidou.com/guide/wrapper.html

3、配置主鍵生成策略

  • 主鍵自增
public class Account {
    @TableId(value = "ID",type = IdType.AUTO)
    private long id;

4、分頁插件

在啓動類中添加

@MapperScan("com.puls.mapper")
@SpringBootApplication
public class Application {
   
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }

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

這個分頁是一個類,跟我們之前ssm寫的那些不一樣。

 	@Test
    public void testPage(){
        Page<Account> page = new Page<>(1,2);
        QueryWrapper<Account> queryWrapper = new QueryWrapper<Account>();
        //queryWrapper.eq("name","aaaa");
        IPage<Account> accountIPage = this.accountMapper.selectPage(page,null);
        System.out.println("總頁數--->"+accountIPage.getTotal());
        System.out.println("當前頁數--->"+accountIPage.getCurrent());
        System.out.println("當前每頁顯示數-->"+accountIPage.getSize());

        List<Account> list = accountIPage.getRecords();
        System.out.println(list);
    }

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章