天天看點

Mybatis-Plus分頁查詢配置及實作

在使用Mybatis-plus過程中,有許多步驟不是很明确,需要整理下,留着日後自用

1.依賴

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>${mybatis-plus.version}</version>
</dependency>

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus</artifactId>
  <version>${mybatis-plus.version}</version>
</dependency>           

2.配置Config

如下是3.4+版本的新版配置項
@Configuration
public class MybatisPlusConfig {
    //分頁查詢 mybatis-plus
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
}
           

datasource配置

@Configuration
@MapperScan(basePackages = MysqlDataSource.PACKAGE_NAME ,sqlSessionFactoryRef = "streamBookSqlSessionFactory")
public class MysqlDataSource {
     static final String PACKAGE_NAME="com.xxxx.xxx.dao";
    private static final String STREAMBOOK_MAPPER_LOCATION = "classpath:mappers/*.xml";

    @Autowired
    private MybatisPlusInterceptor mybatisPlusInterceptor;

    @Primary
    @Bean("streamBookDataSourceProperties")
    @Qualifier("streamBookDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    public DataSourceProperties streamBookDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Primary
    @Bean("streamBookDataSource")
    @Qualifier("streamBookDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    public DataSource streamBookDataSource() {
        return new DruidDataSource();
    }

    @Primary
    @Bean("streamBookTransactionManager")
    @Qualifier("streamBookTransactionManager")
    public DataSourceTransactionManager streamBookTransactionManager(@Qualifier("streamBookDataSource")
                                                                             DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean("streamBookSqlSessionFactory")
    @Qualifier("streamBookSqlSessionFactory")
    public SqlSessionFactory streamBookSqlSessionFactory(@Qualifier("streamBookDataSource") DataSource dataSource)
            throws Exception {
        final MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();

        initMybatisPlusConfiguration(sqlSessionFactoryBean);
        sqlSessionFactoryBean.setDataSource(dataSource);
        //分頁插件注冊
        sqlSessionFactoryBean.setPlugins(mybatisPlusInterceptor);
        sqlSessionFactoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources(STREAMBOOK_MAPPER_LOCATION));
        return sqlSessionFactoryBean.getObject();
    }

    private void initMybatisPlusConfiguration(MybatisSqlSessionFactoryBean factoryBean) {
        // mybatis plus 配置, 參見{@link https://mp.baomidou.com/config/#configuration-2}
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        factoryBean.setConfiguration(configuration);

        // 全局預設,參見{@link https://mp.baomidou.com/config/#dbconfig-2}
        GlobalConfig globalConfig = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setIdType(IdType.AUTO);
        globalConfig.setDbConfig(dbConfig);
        factoryBean.setGlobalConfig(globalConfig);
    }

    @Primary
    @Bean("streamBookSqlSessionTemplate")
    @Qualifier("streamBookSqlSessionTemplate")
    public SqlSessionTemplate streamBookSqlSessionTemplate(@Qualifier("streamBookSqlSessionFactory")
                                                                   SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
           

3.代碼編寫

mapper.xml

<select id="selectUserPage" resultType="com.xx.xxx.model.pojo.xxxx">
  select
  <include refid="Base_Column_List"/>
  from t_data_exploration_info ${ew.customSqlSegment}
</select>
           

dao.java

//自定義sql分頁
IPage<xxx> selectUserPage(Page<xxx> page, @Param(Constants.WRAPPER) Wrapper<xxx> wrapper);
           

impl.java

Page<xxx> infoPage = new Page<>(currentPage, pageSize);
QueryWrapper<xxx> queryWrapper = new QueryWrapper<>();


if (StringUtils.isNotEmpty(expName)){
    if (expName.matches("\\d+")){
        long id = Long.parseLong(expName);
        queryWrapper.eq("id", id);
    }else {
        queryWrapper.eq("exploration_name", expName);
    }
}

if (StringUtils.isNotEmpty(dataSource)){
    queryWrapper.eq("data_source",dataSource);
}

if (StringUtils.isNotEmpty(userId)){
    queryWrapper.eq("user_id",userId);
}

IPage<xxx> xxxx = mapper.selectUserPage(infoPage, queryWrapper);
System.out.println("總條數: " + xxx.getTotal());
System.out.println("資料條數: " + xxx.getRecords());
xxVo xxvo = new xxVo();
xxvo.setCurrentPage(xx.getCurrent());
xxvo.setPageSize(xx.getSize());
xxvo.setTotalSize(xx.getPages());
xxvo.setRecords(xx.getTotal());
xxvo.setInfoList(xxx.getRecords());
return xxvo;
           

4.結果

Mybatis-Plus分頁查詢配置及實作