Mybatis分页插件介绍

Mybatis分页插件介绍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; } } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/178218.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午5:46
下一篇 2026年3月26日 下午5:46


相关推荐

  • Android学习笔记(四十):Preference的使用

    Android学习笔记(四十):Preference的使用

    2021年12月8日
    41
  • Windows Longhorn_Windows优化

    Windows Longhorn_Windows优化一、系统优化设置。1、删除Windows强加的附件:1)用记事本NOTEPAD修改/winnt/inf/sysoc.inf,用查找/替换功能,在查找框中输入,hide(一个英文逗号紧跟hide),将“替换为”框设为空。并选全部替换,这样,就把所有的,hide都去掉了,2)存盘退出,3)再运行“添加-删除程序”,就会看见“添加/删除Windows组件”中多出了好几个选项;这样你可以删

    2022年10月21日
    5
  • 软件开发视频资源分享[通俗易懂]

    软件开发视频资源分享[通俗易懂]资源分享资源均来源于网络,在自学/开公众号的时候收集而来。如果侵权请联系我,会第一时间删除。如果链接已失效(我也无办法,很多链接我是没有保存在自已的网盘中的,见谅)。Java基础Java马士兵:链接:https://pan.baidu.com/s/1jJRvxGi密码:v3xbJava毕向东:可以到往B站找,已上传到B站Java刘意:链接:https://pan.bai…

    2022年5月21日
    47
  • SSH客户端常用工具SecureCRT操作

    SSH客户端常用工具SecureCRT操作1 1SecureCRT 工具介绍 SecureCRT 是一款支持 SSH SSH1 和 SSH2 协议的终端仿真软件 常被用来运行于 Windows 下远程登录 UNIX 或 Linux 服务器 SecureCRT 软件功能强大 不仅仅支持 SSH 协议 同时还支持 Telnet RLogin Serial 和 TAPI 等协议 它有非常多的功能

    2026年3月26日
    1
  • 手把手带你学扣子Coze之画板插件

    手把手带你学扣子Coze之画板插件

    2026年3月12日
    3
  • ELK搭建开源日志系统(window版本)—图文详细

    ELK搭建开源日志系统(window版本)—图文详细ELK搭建开源日志系统(window版本)—图文详细日志对于排查错误非常重要,使用linux命令awksedgrepfind等命令查询日志非常麻烦,而且很难做数据分析,使用免费开源的ELK可以支撑大规模的日志检索,本文将一步步教怎么快速搭建一个window版本的ELK日志收集系统。下载elasticsearch、logstash、kibana、filebeat注意同一系列的版本要一样,防止出现版本不兼容问题,本文使用7.16.0版本,在window系统演示下载elasticsearch访问地

    2022年8月30日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号