Mybatis-分页插件(spring boot下)
来源:互联网 发布:全聚合网络电视电脑版 编辑:程序博客网 时间:2024/06/06 16:32
前提:无论你是用xml写sql语句的方式,还是再mybatis注解sql语句的方式,前提是【不分页查询】能查询到,然后再复制本篇代码加【分页查询】1.POM文件,这里用了一个国内高手写的插件<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.1</version> </dependency>2.写配置类import java.util.Properties;import javax.sql.DataSource;import org.apache.ibatis.plugin.Interceptor;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import org.springframework.transaction.annotation.TransactionManagementConfigurer;import com.github.pagehelper.PageHelper;@Configuration@EnableTransactionManagementpublic class MybatisConfig implements TransactionManagementConfigurer { @Autowired DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); PageHelper pageHelper = new PageHelper(); Properties props = new Properties(); props.setProperty("reasonable", "true"); props.setProperty("supportMethodsArguments", "true"); props.setProperty("returnPageInfo", "check"); props.setProperty("params", "count=countSql"); pageHelper.setProperties(props); bean.setPlugins(new Interceptor[] { pageHelper }); //如果spring boot的sql语句是在xml里,则需要加下面两行代码 PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); bean.setMapperLocations(resolver.getResources("classpath:com/xx/xx/xx/*.xml")); //两行代码结束,请注意,application.properties文件中不要再配置mybatis相关配置了,比如setMapperLocations这些属性什么的 try { return bean.getObject(); } catch (Exception e) { e.printStackTrace(); } return null; } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } @Bean @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); }}3.Controller中写法@ResponseBody @PostMapping("/list") public List<Entity> getLastData() { //调用startPage方法 PageHelper.startPage(0, 5, true, false);//表示当前第0页,每页五条,假如一共11页,那么超过11页,数据则显示空 PageHelper.startPage(0, 5);//表示当前第0页,每页五条,假如一共11页,那么超过11页,数据则显还是第11页 //这里直接写查询方法即可,别的不需要管 List<Entity> datas = mapper.findLastData(userId); //查询完毕之后 Page<Entity> pageMsg = (Page<Entity>) datas; pageMsg.getTotal(); pageMsg.getPages(); pageMsg.getPageSize(); return datas; }
阅读全文
0 0
- Mybatis-分页插件(spring boot下)
- Spring Boot集成MyBatis与分页插件
- Spring Boot集成MyBatis与分页插件
- spring boot和mybatis集成分页插件
- spring boot mybatis分页插件的使用
- Spring-Boot项目+Spring-Mybatis+分页插件+Spring单元测试整合
- Spring Boot系列教程十一: Mybatis使用分页插件PageHelper
- Spring Boot系列教程八: Mybatis使用分页插件PageHelper
- MyBatis基于Spring-boot集成通用Mapper以及pagehelper分页插件(含源码下载)
- spring-mybatis分页插件
- Spring Boot系列六 Spring boot集成mybatis、分页插件pagehelper
- Spring Boot+Mybatis+Pagehelper分页
- spring boot mybatis+分页配置
- Spring Boot+Mybatis+Pagehelper分页
- spring boot学习3之mybatis+druid+事务+PageHelper分页插件+sql打印插件整合
- MyBatis+Spring分页插件配置
- spring+mybatis利用pageHelper分页插件分页
- spring boot 配置MyBatis,支持多个数据源和分页插件
- 写Recipe(四)
- studio+selenium+testNG+testReport
- 安卓学习日记(一):了解安卓架构(linux内核层、系统运行库层、应用框架层、应用层)
- Boost算法库——C++11算法(is_sorted)
- samohyes的心情笔记(5)
- Mybatis-分页插件(spring boot下)
- HenCoder UI 部分 2-1 布局基础
- windows下git代码管理初体验
- Python中的函数式编程(一)
- 配置ip地址
- shopnc定时任务
- SpringBoot基于AOP实现自定义注解
- 在继承中重写方法时抛出异常的问题
- 其他类API