springboot配置pagehelper5.0和4.0版本的区别

springboot配置pagehelper5.0和4.0版本的区别

pagehelper5.0使用的拦截器是PageInterceptor,pagehelper4.0使用的拦截器是PageHelper;而且5.0的版本去除了sqlsource模块的好几个类OrderByStaticSqlSource、PageSqlSource、PageRawSqlSource、PageStaticSqlSource、PageDynamicSqlSource、PageProviderSqlSource,pagehelper4.0版本切到pagehelper5.0需要注意这个问题。下面分别讲解springboot配置pagehelper5.0和4.0的版本,详细的配置见码云上的代码工程:freemarkerDemo

springboot配置pagehelper5.0

maven配置

引入pagehelper-spring-boot-starter采用的是pagehelper5.0版本

        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.5
        

controller类测试方法

    //localhost:8080/list?pageNum=1&pageSize=11
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);//pageNum页码,pageSize每页大小
        List list = userService.selectAllUsers();
        PageInfo page = new PageInfo(list);
        model.addAttribute("page", page);
        return "content";
    }

springboot配置pagehelper4.0

maven配置

        
            com.github.pagehelper
            pagehelper
            4.1.5
        

配置pagehelper拦截器

方式一:使用mybatis配置文件

第一步:application.properties文件增加一条配置

mybatis.config-location= classpath:mybatis-config.xml

第二步:在mybatis-config.xml配置拦截器




    
        
    

    
    
        
            
            
        
    

方式二:直接在启动类App.java类里通过编程式配置拦截器

@SpringBootApplication
@ComponentScan(basePackages = {"com.pingan.core"})
@Slf4j
public class App {
    public static void main(String[] args) throws Exception {
       SpringApplication.run(App.class, args);
    }

    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("reasonable","true");
        properties.setProperty("dialect","oracle");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }
}

controller类测试方法(同上)

    //localhost:8080/list?pageNum=1&pageSize=11
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);//pageNum页码,pageSize每页大小
        List list = userService.selectAllUsers();
        PageInfo page = new PageInfo(list);
        model.addAttribute("page", page);
        return "content";
    }

你可能感兴趣的:(springboot)