Mybatis分页插件介绍

背景

作为一名java后端开发工程师,免不了要使用数据库,也免不了会用到mybatis框架,使用mybatis框架又免不了使用选择一个用于处理mybatis分页功能的插件,本文主要介绍两个用于处理分页的插件。

插件1 Mybatis-plus

依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-extension</artifactId>
    <version>3.0.2</version>
#注意里面的mybatis-spring要一样,不然版本会不兼容

mapper

public interface JijingMapper extends BaseMapper<Jijing> {

}

分页代码
//参数一是当前页,参数二是每页个数

IPage<User> userPage = new Page<>(2, 2);
userPage = userMapper.selectPage(userPage, null);

配置类

@Configuration
public class MybatisConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        return page;
    }
    @Bean
    public ISqlInjector sqlInjector() {
        return new DefaultSqlInjector();
    }

}

实体类

@TableName("uwb_device_type")
public class DeviceTypeEntity extends AbstractBase {
    @TableLogic(value = "1",delval = "0")  //走 Update value = "" 默认的原值 delval = "" 删除后的值
    protected Byte deletedFlg; #不能为private
    @TableField(fill = FieldFill.INSERT)
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @TableField(exist=false)
    }

配置文件

mybatis-plus.mapperLocations = classpath*:mapper/*.xml
mybatis-plus.type-aliases-package = com.adong.adongauthority.mvc.model
mybatis-plus.global-config.db-config.id-type = AUTO
mybatis-plus.global-config.db-config.db-type = MYSQL
mybatis-plus.global-config.db-config.field-strategy = NOT_NULL
mybatis-plus.global-config.db-config.table-underline = true
mybatis-plus.global-config.db-config.logic-delete-value = 0
mybatis-plus.global-config.db-config.logic-not-delete-value = 1
mybatis-plus.global-config.sql-parser-cache = true
mybatis-plus.configuration.map-underscore-to-camel-case = true
mybatis-plus.configuration.cache-enabled = false
mybatis-plus.configuration.jdbc-type-for-null = NULL
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

启动类

@MapperScan("com.adong.adongauthority.dao")

自动补充插入或更新时的值

@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
 
 
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createBy", CookieUtil.getLoginUser(),metaObject);
        this.setFieldValByName("createTime", new Date(),metaObject);
        this.setFieldValByName("updateBy", CookieUtil.getLoginUser(),metaObject);
        this.setFieldValByName("updateTime", new Date(),metaObject);
        this.setFieldValByName("version", Long.valueOf(1),metaObject);
        this.setFieldValByName("appSecret", UUID.randomUUID().toString().replaceAll("-",""),metaObject);
    }
 
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateBy", CookieUtil.getLoginUser(),metaObject);
        this.setFieldValByName("updateTime", new Date(),metaObject);
        this.setFieldValByName("version", this.getFieldValByName("version",metaObject),metaObject);
    }
}

插件2 pagehelper

依赖

<dependency>    
       <groupId>com.github.pagehelper</groupId>    
        <artifactId>pagehelper</artifactId>    
        <version>5.1.2</version>
 </dependency>

使用

//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(queryDTO.getPage(), queryDTO.getListRows(),"create_time desc");
//紧跟着的第一个select方法会被分页
List<DeviceTypeEntity> list = deviceTypeDao.queryPageByEntity(queryDTO);
PageInfo page = new PageInfo(list);
return ResultVO.ok(page);
Example example = new Example(Person.class);
Example.Criteria criteria = example.createCriteria();
criteria.andIn("personCode", personCodes)
        .andIsNotNull("tagId");
List<Person> personList = personDao.selectByExample(example);

配置

package com.paic.ocss.gateway.dao.config;

import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.github.pagehelper.PageHelper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.Properties;

@Configuration
@MapperScan("com.paic.ocss.gateway.dao.mapper*")
@Import(value = { com.paic.ocss.monitor.cat.mybatis.SpringCloudCatMybatisConfig.class })
public class MybatisConfig {

    @Bean
    public GlobalConfiguration globalConfiguration() {
        GlobalConfiguration global = new GlobalConfiguration();
        global.setDbType("mysql");
        return global;
    }

    /**
     * 配置mybatis的分页插件pageHelper
     * @return
     */
    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("reasonable","true");
        //配置mysql数据库的方言
        properties.setProperty("dialect","mysql");
        pageHelper.setProperties(properties);
        return pageHelper;
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值