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下) 分页插件作 ...
随机推荐
- c++ union内存
看一个例子: #include <iostream> #include <stdio.h> using namespace std; union A { int a; stru ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验六:数码管模块
实验六:数码管模块 有关数码管的驱动,想必读者已经学烂了 ... 不过,作为学习的新仪式,再烂的东西也要温故知新,不然学习就会不健全.黑金开发板上的数码管资源,由始至终都没有改变过,笔者因此由身怀念. ...
- 解决Maven build 慢的问题
extends:http://www.cnblogs.com/gmq-sh/p/4742698.html ,http://www.cnblogs.com/rainy-shurun/p/5726758. ...
- 关于web标准的一些想法
关于web标准的一些想法 页面结构,表现,行为的关系.应该是各自分离又紧密联系的关系.从代码上分离出来.各自完成各自的功能,方便以后维护.紧密联系是指他们是相互依赖的.结构是核心,虽然从理论上讲不管什 ...
- MacTex TexStudio Configuration 配置
在Mac上使用Latex的话主流是安装MacTex,对于IDE的选择有很多,像什么自带的TexShop,或者是TexStudio,Latexian,Texpad,Texmaker等,甚至可以直接使用一 ...
- STL学习笔记--排序算法
排序算法 C++ STL 的排序算法(Sorting algorithms)是一组将无序序列排列成有序序列的模板函数或与排序相关的模板函数,提供了排序.折半搜索.归并.集合操作.堆操作.最值求解.字典 ...
- - Fractal(3.4.1)
C - Fractal(3.4.1) Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Su ...
- [No0000168]Excle只允许用户输入纯文本,禁止用户修改单元格样式、格式等
背景:自己的模板给别人,让他填完信息上传到系统里,但别人经常不按模板的格式来填写,导致无法程序自动化.能不能在模板上把格式锁住,只允许输入纯文本,但不能改格式? 方法: 步骤一,创建你要的模板 其中, ...
- [No0000164]C#,科学计数法的哽
NewString = Decimal.Parse(OldString, System.Globalization.NumberStyles.Float).ToString(); //Convert. ...
- wpf(windos窗体)
在windos窗体中可以放置各种控件,以及为控件定义事件等等,而窗体的显示可以通过show方法以及showdialog方法.他们的区别是 show:运行程序的时候弹出新窗体,而该新窗体会一闪而过,最小 ...