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.结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值