springboot + mybatis配置分页插件
一:使用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配置分页插件的更多相关文章
- SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页
SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...
- SpringBoot集成MyBatis的分页插件 PageHelper
首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...
- SpringBoot集成MyBatis的分页插件PageHelper--详细步骤
1.pom中添加依赖包 <!--pageHelper基本依赖 --> <dependency> <groupId>com.github.pagehelper< ...
- Mybatis的分页插件PageHelper
Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschina. ...
- Mybatis 的分页插件PageHelper-4.1.1的使用
Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschin ...
- Springboot 使用PageHelper分页插件实现分页
一.pom文件中引入依赖 二.application.properties中配置以下内容(二选一方案) 第一种:pagehelper.helper-dialect=mysqlpagehelper.re ...
- Mybatis之分页插件pagehelper的简单使用
最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...
- mybatis pageHelper 分页插件使用
转载大神 https://blog.csdn.net/qq_33624284/article/details/72828977 把插件jar包导入项目(具体上篇有介绍http://blog.csdn. ...
- maven项目创7 配置分页插件
页面编写顺序 首先确定是否拥有想要的pojo(对象实体类)———>dao层mybatis配置——>service层的接口及实现类——>controller(web下) 分页插件作 ...
随机推荐
- Installation Guide Ubuntu 16.04
Beside the installation guide on the main page, here is a guide to install GenieACS off a freshly in ...
- mysql的安装和配置
1.mydql的安装 重装wind7系统之后,mysql软件自动卸载了.现在学习需要使用mysql,还是要安装mysql.我首先通过下载安装包的方式安装,按照教程,但是出现缺失.dll文件,我下载安装 ...
- F - Fibonacci again and again
任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)=2; F(n)=F(n-1)+F(n-2)(n>=3); 所以,1, ...
- PAT甲级1013-1014-1015
题目:1013 Battle Over Cities 思路:城市数也就1000, 对于每次询问暴力bfs一下看一下有多少连通块就行了.答案就是联通块数减一. #include<stdio.h&g ...
- 架构师如何借鉴他人经验快速成长? | 2018GIAC上海站日程上线!
随着网络技术的迅猛发展,越来越多的企业需要紧跟技术发展潮流以应对层出不穷的业务场景变化.如今多“语言”开发百花齐放,选择何种语言才能在合适的场景中发挥最大价值?互联网业务架构经过了长年的发展,已然朝着 ...
- Steeltoe之Service Discovery篇
在前文一窥Spring Cloud Eureka中,已经构建了基于Eureka的服务端与客户端,可用于实现服务注册与发现功能.而借助Steeltoe的类库,可以在.NET生态系统中使用Spring C ...
- sql语句 isnull(列名,'')='' /STUFF的意思
(1) SELECT SYXH,ZYHM,YEXH,ISNULL(YETZ,'') AS YETZ ,RYKSMC,RYBQMC,HZXM FROM YG_BRSYK 如果列名数据等于NULL,那么 ...
- [No0000103]JavaScript-基础课程3
在 JavaScript 中,函数的参数是比较有意思的,比如,你可以将任意多的参数传递给一个函数,即使这个函数声明时并未制定形式参数 function adPrint(str, len, option ...
- Vue SSR 配合Java的Javascript引擎j2v8实现服务端渲染3配置webpack支持ssr
安装 cross-env yarn add -D cross-env 安装 html-webpack-plugin yarn add -D html-webpack-plugin 安装 webpack ...
- ArcGIS拓扑检查
对于拓扑检查中的等级参数一直不理解,经过参考资料才明白过来: 注:如果有两个要素参与到拓扑,在修复拓扑错误时会优先移动拓扑级别低的要素来满足匹配拓扑规则要求. 参考资料: https://wenku. ...