Mybatis plus paging plugin and auto-fill
Configuration details
@EnableTransactionManagement
@Configuration
@MapperScan("com.itoyoung.dao")
public class MybatisPlusConfig {
/**
* Page Break Plugin
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setLimit(-1);
return paginationInterceptor;
}
/**
* Automatic filling function
* @return
*/
@Bean
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setMetaObjectHandler(new MetaHandler());
return globalConfig;
}
}
@Component
@Slf4j
public class MetaHandler implements MetaObjectHandler {
/**
* New Data Execution
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime", new Date(), metaObject);
this.setFieldValByName("updateTime", new Date(), metaObject);
}
/**
* Update data execution
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
application
Add an annotation to the entity class @ tablefield (value = create)_ time", fill = FieldFill.INSERT )
, the time is automatically filled in when inserting or updating
@Data
@Accessors(chain = true)
public class Channel implements Serializable {
@TableId(type = IdType.AUTO)
private Long id;
/**
* Channel Code
*/
@NotEmpty(message = "Channel code cannot be null")
private String channelCode;
/**
* Channel Name
*/
@NotEmpty(message = "Channel name cannot be empty")
private String channelName;
/**
* creat time
*/
@TableField(value = "create_time", fill = FieldFill.INSERT)
private Date createTime;
/**
* update time
*/
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
Paging query
@Service(version = "${provider.service.version}")
@Slf4j
@Component
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements IChannelService {
@Override
public Page<Channel> selectChannelPageByQuery(ChannelListGetQuery query) {
Page<Channel> channelPage = new Page<>();
channelPage.setCurrent(query.getCurrentPage());
channelPage.setSize(query.getPageSize());
QueryWrapper<Channel> queryWrapper = new QueryWrapper<>();
if (StringUtil.isNotBlank(query.getChannelName())) {
queryWrapper.lambda().like(Channel::getChannelName, query.getChannelName());
}
queryWrapper.lambda().orderByDesc(Channel::getId);
IPage<Channel> page = page(channelPage, queryWrapper);
channelPage.setTotal(page.getTotal());
channelPage.setRecords(page.getRecords());
return channelPage;
}
}
Read More:
- How to Solve mybatis-plus Paging Plug-in PaginationInnerInterceptor error
- [Solved] Mybatis.generator error: Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2
- [Solved] Mybatis integrates PageHelper and uses sqlserver paging error
- Mybatis sets the primary key Auto-Increment error: No setter found for the keyProperty
- Maven plugin development report error- plugin:3.2 :descriptor fai
- [Solved] Mybatis Error: Could not find resource mybatis-conf.xml
- [Solved] Tk-Mybatis Error: tk.mybatis.mapper.MapperException:
- [Solved] ‘build.plugins.plugin.version‘ for org.springframework.boot:spring-boot-maven-plugin is missing.
- [Solved] IDEA Import springboot Project Error: Cannot resolve plugin org.springframework.boot:spring-boot-maven-plugin:<unknown>
- [Solved] mybatis plus Insert Error: mybatis plus Error setting null for parameter #1 with JdbcType OTHER
- SpringBoot—Error starting ApplicationContext. To display the auto-configuration report re-run your a
- Spring Boot Error starting ApplicationContext. To display the auto-configuration report re-run you
- Springboot integration RabbitMQ times error: Failed to check/redeclare auto-delete queue(s).
- [Solved] Mybatis Batch Modify Error: multi-statement not allow
- Failed to auto-configure a DataSource: ‘spring.datasource.url‘ is not specified and no embedded data
- Mybatisenumtypehandler upgrade error of mybatis plus
- Ruoyi-cloud Integrated mybatis-plus Error: Unknown column ‘search_value‘ in ‘field list‘
- [Solved] Mybatis Error: attempting to get column ‘XX’ from result set
- mybatis-plus Common Error and Their Solution
- JUnit tests mybatis-plus error: null pointer [How to Solve]