springBoot+mybatis分页插件的简单使用

1.添加maven以来

		<dependency>
			<groupId>com.github.pagehelpergroupId>
			<artifactId>pagehelperartifactId>
			<version>4.1.0version>
		dependency>

2.加入拦截

@EnableAutoConfiguration
@ComponentScan
@MapperScan("com.frw.kw.mapper")
@SpringBootApplication
@EnableScheduling
@DubboComponentScan(basePackages = "com.frw.kw.gw")
@ImportResource("classpath:dubbo.xml")
public class Application {

    private static Logger logger = Logger.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }


    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return new org.apache.tomcat.jdbc.pool.DataSource();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        //sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
        //添加插件
        
        /* 添加分页插件拦截器 开始 */
        PageHelper pageHelper = new PageHelper();
        Properties props = new Properties();
        props.setProperty("dialect", "postgresql");
        pageHelper.setProperties(props);
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
        /* 添加分页插件拦截器 结束 */

        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}

完成以上两步后就可以开始使用了

3.在代码中使用

        PageHelper.startPage(1,10);
        List<Map<String,Object>> provinces = addressMapper.listProvinces();
        PageInfo pageInfo = new PageInfo(provinces);

你可能感兴趣的:(mybatis)