在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);
}