Mybatis plus paging Plugin and Auto-fill

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: