背景
作为一名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
