一:使用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. Github安卓开源项目编译运行

    转:http://blog.csdn.net/laihuan99/article/details/9054985 很多新手朋友朋友在Github搜索开源项目时,有时候不明白怎么导入eclipse.的确 ...

  2. css3整理--Animation

    animation语法: 1.动画的定义 @keyframes IDENT { from { Properties:Properties value; } Percentage { Propertie ...

  3. POJ 3249 Test for Job

    Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13457 Accepted: 3100 Descrip ...

  4. [实战]MVC5+EF6+MySql企业网盘实战(4)——上传头像

    写在前面 最近又开始忙了,工期紧比较赶,另外明天又要去驾校,只能一个功能一个功能的添加了,也许每次完成的功能确实不算什么,等将功能都实现了,然后在找一个好点的ui对前端重构一下. 系列文章 [EF]v ...

  5. 部署不能产生class文件的问题

    项目clean和重新部署项目之后,还是不能产生class文件:查看“Problem”视图,是lib路径有问题,右击项目→“Build Path”→“Configure Build Path”,Libr ...

  6. 通过Rabbitmq从ipone手机传输imu和相机数据到电脑端

    https://github.com/tomas789/iOSmsg_client https://github.com/tomas789/iOSmsg 通过xcode工具把iosmsg打包发布到ip ...

  7. Ubuntu 16.04 编译OpenCV 问题解决stdlib.h: No such file or directory

    https://blog.csdn.net/xinyu391/article/details/72867510 https://ask.csdn.net/questions/365969

  8. tensorflow 的tf.where详解

    最近在用到数据筛选,观看代码中有tf.where()的用法,不是很常用,也不是很好理解.在这里记录一下 tf.where( condition, x=None, y=None, name=None ) ...

  9. dos基本指令

    目录操作 dir  操作磁盘文件目录 md / mkdir <folder name> 创建子目录make directory cd  改变当前工作目录,返回上一级 cd .. rd  删 ...

  10. AngularJs 常用指令标签

    1.ng-app:告诉Angular他应该管理页面的那一部分,可以放在html元素上也可以放在div等标签上 例:<html ng-app="problem"> 2.n ...