mybatis-plus(四)-Mybatis-plus分页

Mybatisplus分页

配置分页拦截器

@Configuration
public class MybatisplusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }
}

属性介绍

属性名 类型 默认值 描述
overflow boolean false 溢出总页数后是否进行处理(默认不处理,参见 插件#continuePage 方法)
maxLimit Long 单页分页条数限制(默认无限制,参见 插件#handlerLimit 方法)
dbType DbType 数据库类型(根据类型获取应使用的分页方言,参见 插件#findIDialect 方法)
dialect IDialect 方言实现类(参见 插件#findIDialect 方法)

使用分页

public List<BookType> findByPage(int currentPage, int rowCount) {
    IPage<BookType> page = new Page<>();
    page.setCurrent(currentPage); //设置当前页数 
    page.setSize(rowCount);       //每页显示行数
    QueryWrapper<BookType> wrapper = new QueryWrapper<>();
    IPage ps = bookTypeDao.selectPage(page, wrapper);

    System.out.println("总页数: " + ps.getPages());
    System.out.println("记录: " + ps.getRecords());
    System.out.println("总条数: " + ps.getTotal());

    return ps.getRecords();  //获取当前页(currentPage)的数据。
}

测试

 Consume Time:23 ms 2023-06-03 00:36:45
 Execute SQL:SELECT COUNT(*) AS total FROM btype_tab

 Consume Time:2 ms 2023-06-03 00:36:45
 Execute SQL:SELECT btype_id AS id,btype_name AS name,btype_createTime AS createTime,btype_updateTime AS updateTime,btype_createBy AS createBy FROM btype_tab LIMIT 10

总页数: 6
记录: [BookType(id=1, name=神话出版社, createTime=Sat Jun 03 00:17:23 CST 2023, updateTime=Sat Jun 03 00:17:23 CST 2023, createBy=admin), BookType(id=2, name=神话出版社, createTime=Sat Jun 03 00:23:59 CST 2023, updateTime=Sat Jun 03 00:23:59 CST 2023, createBy=admin), BookType(id=3, name=神话出版社, createTime=Sat Jun 03 00:24:00 CST 2023, updateTime=Sat Jun 03 00:24:00 CST 2023, createBy=admin), BookType(id=4, name=神话出版社, createTime=Sat Jun 03 00:24:00 CST 2023, updateTime=Sat Jun 03 00:24:00 CST 2023, createBy=admin), BookType(id=5, name=神话出版社, createTime=Sat Jun 03 00:24:00 CST 2023, updateTime=Sat Jun 03 00:24:00 CST 2023, createBy=admin), BookType(id=6, name=神话出版社, createTime=Sat Jun 03 00:24:00 CST 2023, updateTime=Sat Jun 03 00:24:00 CST 2023, createBy=admin), BookType(id=7, name=神话出版社, createTime=Sat Jun 03 00:24:00 CST 2023, updateTime=Sat Jun 03 00:24:00 CST 2023, createBy=admin), BookType(id=8, name=神话出版社, createTime=Sat Jun 03 00:24:00 CST 2023, updateTime=Sat Jun 03 00:24:00 CST 2023, createBy=admin), BookType(id=9, name=神话出版社, createTime=Sat Jun 03 00:24:00 CST 2023, updateTime=Sat Jun 03 00:24:00 CST 2023, createBy=admin), BookType(id=10, name=神话出版社, createTime=Sat Jun 03 00:24:00 CST 2023, updateTime=Sat Jun 03 00:24:00 CST 2023, createBy=admin)]
总条数: 51

mybatis解决批量添加解决方案

mybatisplus的InsertBatchSomeColumn方法可以进行批量添加功能。

MybatisPlus配置类

配置类在mysqlnjector对象中添加了InsertBatchSomeColumn()方法。

@Configuration
public class MybatisplusConfig {

      @Bean                                                                                                
  public DefaultSqlInjector defaultSqlInjector(){                                                      
      return new DefaultSqlInjector(){                                                                 
          @Override                                                                                    
          public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {       
              List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);           
              methodList.add(new InsertBatchSomeColumn(p->p.getFieldFill()!=FieldFill.UPDATE));        
              return methodList;                                                                       
          }                                                                                            
      };                                                                                               
  }                                                                                                    
}

接口中添加InsertBatchSomeColumn()方法

@Mapper
public interface IBookTypeDao extends BaseMapper<BookType> {

    void insertBatchSomeColumn(List<BookType> list);
}

service

@Override
public void insertBatch02(List<BookType> list) {
    long start = System.currentTimeMillis();
    bookTypeDao.insertBatchSomeColumn(list);
    long end = System.currentTimeMillis();
    log.debug("insertBatchSomeColumn耗时:{}ms", (end-start));
}

测试

package com.wnhz.mp.service.impl;

import com.wnhz.mp.entity.BookType;
import com.wnhz.mp.service.IBookTypeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.*;


@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class BookTypeServiceImplTest {

    @Autowired
    private IBookTypeService ibs;

    @Test
    public void batchAdd() {
        List<BookType> list = new ArrayList<>();
        list.add(new BookType(null,"故事书1",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书2",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书3",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书4",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书5",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书6",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书7",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书8",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书9",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书10",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书11",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书12",new Date(),new Date(),"admin"));
        list.add(new BookType(null,"故事书13",new Date(),new Date(),"admin"));

        ibs.batchAdd(list);
    }
}

image-20230629142540159

你可能感兴趣的:(mybatis-plus,mybatis)