SpringBoot6 -- 集成PageHelper分页插件

多读书,知道了还有一个类型安全的配置,因此后面把分页插件pagehelper的配置修改了下


创建PageHelperProperties

/**
 * @author Ami
 * @description
 * @date Created in 2018/4/23 15:11
 * @modified
 */
@ConfigurationProperties(prefix = "mybatis.page-helper.properties")
public class PageHelperProperties {

    private String reasonable = "true";
    private String offsetAsPageNum = "true";
    private String rowBoundsWithCount = "true";
    private String dialect = "mysql";

    public String getReasonable() {
        return reasonable;
    }

    public void setReasonable(String reasonable) {
        this.reasonable = reasonable;
    }

    public String getOffsetAsPageNum() {
        return offsetAsPageNum;
    }

    public void setOffsetAsPageNum(String offsetAsPageNum) {
        this.offsetAsPageNum = offsetAsPageNum;
    }

    public String getRowBoundsWithCount() {
        return rowBoundsWithCount;
    }

    public void setRowBoundsWithCount(String rowBoundsWithCount) {
        this.rowBoundsWithCount = rowBoundsWithCount;
    }

    public String getDialect() {
        return dialect;
    }

    public void setDialect(String dialect) {
        this.dialect = dialect;
    }
}

创建PageHelperAutoConfiguration


/**
*@description 分页插件自动配置
*@author Ami
*@date 2018/4/23  15:27
*/
@Configuration
@EnableConfigurationProperties(PageHelperProperties.class)
@ConditionalOnClass(SqlSessionFactoryBean.class)
@ConditionalOnProperty(prefix = "mybatis.page-helper.properties",value = "enabled",matchIfMissing = true)
public class PageHelperAutoConfiguration {

    @Autowired
    private PageHelperProperties pageHelperProperties;

    /**
     * 分页插件设置,已测试配置生效
     * @return
     */
    @Bean
    public PageHelper getPageHelper() {
        // 配置分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", pageHelperProperties.getReasonable());
        properties.setProperty("offsetAsPageNum", pageHelperProperties.getOffsetAsPageNum());
        properties.setProperty("rowBoundsWithCount", pageHelperProperties.getRowBoundsWithCount());
        properties.setProperty("dialect", pageHelperProperties.getDialect());
        pageHelper.setProperties(properties);
        return pageHelper;
    }

}

这样在application.properties文件中配置

mybatis.page-helper.properties.reasonable=true
mybatis.page-helper.properties.offsetAsPageNum=true
mybatis.page-helper.properties.rowBoundsWithCount=true
mybatis.page-helper.properties.dialect=mysql

当设置mybatis.page-helper.properties.enabled = true或者不设置时就会触发自动配置

当设置mybatis.page-helper.properties.enabled = false就不会触发自动配置


****************************************写给自己的****************************************

由此引申的是其他的配置,因为这些思想都是通用的,而且SpringBoot的自动配置大体就是这样,所有的自动配置在spring-boot-autoconfigure-xxx.RELEASE.jar!\META-INF\spring.factories文件中都可以找到,如果自定义的还需要在项目里新建一个\META-INF\spring.factories写上自定义的

猜你喜欢

转载自blog.csdn.net/qq_36781718/article/details/80051324