一:使用pagehelper配置分页插件

1:首先配置springboot +mybatis框架  参考:http://www.cnblogs.com/liyafei/p/7911549.html

2:创建配置类MybatisConfig,对分页插件进行配置。将mybatis-config.xml移动到classpath路径下。

  

package com.liyafei.util.pagehelper;

import java.util.Properties;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
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 //添加这个注解相当于配置文件
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {
 //MybatisConfig将会映射到classpath下的mybaits-config.xml,功能和xml下面配置类似
@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});
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
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中使用分页插件pagehelper,分页插件只能配置一个,不能多用。调用PageHelper.startPage之后下一行(只作用于这一行)的查询结果将会自动调用分页插件
  

    @RequestMapping("/query/{page}/{pageSize}")
public PageInfo query(@PathVariable Integer page, @PathVariable Integer pageSize) {
if(page!= null && pageSize!= null){
System.out.println("page"+page+"pageSize"+pageSize);
PageHelper.startPage(page, pageSize);
}
List<User> userList = userService.getUserList(); for(User user:userList){
System.out.print(user.getId());
System.out.println(user.getUsername()); } return new PageInfo(userList);
}

4:测试成功

  

二:使用mybatis自带的RowBounds分页参数

  1:首先向UserMapper.java中添加一个查询函数,RowBounds作为参数

  

	public List<User> findByRowBounds(RowBounds rowBounds);

2:在UserMapper.xml配置查询函数的sql语句,如红色部分

  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liyafei.dao.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.liyafei.dao.pojo.User" >  //id是resultMapd的标示,type代表使用哪个类作为映射的类
<id column="id" property="id" jdbcType="INTEGER" />  //id代表resultMap的主键,result代表其属性
<result column="username" property="username" jdbcType="VARCHAR" />  //column代表sql的列名,property代表POJO的属性,jdbcType代表sql的类型
<result column="age" property="age" jdbcType="INTEGER" />
<result column="ctm" property="ctm" jdbcType="TIMESTAMP"/>
</resultMap> <sql id="Base_Column_List" >
id, username, age, ctm
</sql>

   <!-- resultMap是上面定义的映射集,使用BaseResultMap作为映射规则,不是结果类型-->
<select id="getUserList" resultMap="BaseResultMap" >
SELECT
<include refid="Base_Column_List" />
FROM tb_user
</select> <select id="findByRowBounds" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM tb_user
</select> <select id="getUserById" parameterType="java.lang.Integer" resultMap="BaseResultMap" >
SELECT
<include refid="Base_Column_List" />
FROM tb_user
WHERE id = #{id}
</select> <insert id="add" parameterType="com.liyafei.dao.pojo.User" >
INSERT INTO
tb_user
(username,age,ctm)
VALUES
(#{username}, #{age}, now())
</insert> <update id="update" parameterType="java.util.Map" >
UPDATE
tb_user
SET
username = #{user.username},age = #{user.age}
WHERE
id = #{id}
</update> <delete id="delete" parameterType="java.lang.Integer" >
DELETE FROM
tb_user
WHERE
id = #{id}
</delete>
</mapper>

3:创建查询参数,在UserController中调用查询函数,并将查询产数作为查询函数的参数。

    @RequestMapping("/rowquery/{page}/{pageSize}")
public PageInfo findByRowBounds(@PathVariable Integer page,@PathVariable Integer pageSize){
RowBounds rowBounds=new RowBounds(page,pageSize);
List<User> userList = userMapper.findByRowBounds(rowBounds); return new PageInfo(userList);
}

4:分页查询成功

springboot + mybatis配置分页插件的更多相关文章

  1. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  2. SpringBoot集成MyBatis的分页插件 PageHelper

    首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...

  3. SpringBoot集成MyBatis的分页插件PageHelper--详细步骤

    1.pom中添加依赖包 <!--pageHelper基本依赖 --> <dependency> <groupId>com.github.pagehelper< ...

  4. Mybatis的分页插件PageHelper

    Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper  文档地址:http://git.oschina. ...

  5. Mybatis 的分页插件PageHelper-4.1.1的使用

    Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper  文档地址:http://git.oschin ...

  6. Springboot 使用PageHelper分页插件实现分页

    一.pom文件中引入依赖 二.application.properties中配置以下内容(二选一方案) 第一种:pagehelper.helper-dialect=mysqlpagehelper.re ...

  7. Mybatis之分页插件pagehelper的简单使用

    最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...

  8. mybatis pageHelper 分页插件使用

    转载大神 https://blog.csdn.net/qq_33624284/article/details/72828977 把插件jar包导入项目(具体上篇有介绍http://blog.csdn. ...

  9. maven项目创7 配置分页插件

    页面编写顺序   首先确定是否拥有想要的pojo(对象实体类)———>dao层mybatis配置——>service层的接口及实现类——>controller(web下) 分页插件作 ...

随机推荐

  1. android 中 webview 怎么用 localStorage? 我在 android里面 使用html5的 localStorage 为什么存不进去也读不出来呀? 网上搜了好多都没效果

    解决方案: mWebView.getSettings().setDomStorageEnabled(true); mWebView.getSettings().setAppCacheMaxSize(1 ...

  2. NIO相关概念之Buffer

    Buffer的定义: 概念上,缓冲区是包在一个对象内的基本数据元素数组.Buffer类相比一个简单数组的优点是它将关于数据的数据内容和信息包含在一个单一的对象中.Buffer类以及它专有的子类定义了一 ...

  3. solus 系统 - 编译安裝 ibus-rime 中文输入法(附:小鹤双拼双形配置文件)

    編譯方法參見官網 - https://github.com/rime/home/wiki/RimeWithIBus 安装依赖:列出几个可能用到的命令 #安裝cmake gcc等开发工具 sudo eo ...

  4. eclipse中一个项目引用另一个项目的方法(申明:来源于网络)

    eclipse中一个项目引用另一个项目的方法(申明:来源于网络) 地址:http://blog.csdn.net/a942980741/article/details/39990699

  5. nginx js、css、图片 及 一些静态文件中出现 http://upstreamname:port 导致部分网页样式显示不正常

    nginx js.css.图片 及 一些静态文件中出现 http://upstreamname:port 导致部分网页样式显示不正常 http://upstreamname:port/....../. ...

  6. Lucene.net(4.8.0) 学习问题记录一:分词器Analyzer的构造和内部成员ReuseStategy

    前言:目前自己在做使用Lucene.net和PanGu分词实现全文检索的工作,不过自己是把别人做好的项目进行迁移.因为项目整体要迁移到ASP.NET Core 2.0版本,而Lucene使用的版本是3 ...

  7. hdu4300 Clairewd’s message【next数组应用】

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. 泡泡一分钟:Towards real-time unsupervised monocular depth estimation on CPU

    Towards real-time unsupervised monocular depth estimation on CPU Matteo Poggi , Filippo Aleotti , Fa ...

  9. tensorflow的ckpt文件总结

    1.TensorFlow的模型文件 --checkpoint_dir | |--checkpoint | |--MyModel.meta | |--MyModel.data-00000-of-0000 ...

  10. [dev][https] 非PFS协商的https的流量的解码

    经过基础调研之后,目前准备确认实现方案,完成对https的解码. 之前的调研,传送门: http://www.cnblogs.com/hugetong/p/6670083.html 1. 需求: 以旁 ...