Spring+mybatis+PageHelper分页插件的使用方法,实例笔记和建议

8 篇文章 0 订阅
1 篇文章 0 订阅

1.在pom.xml中添加如下:

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
</dependency>

如果导入jar包遇到问题,参考:[https://blog.csdn.net/qq_33624284/article/details/72821811]
2.在spring-mybatis.xml中添加如下:

 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:com/draeger/mapping/*.xml"></property>  
         <!--pageHelper--> 
	    <property name="plugins">
	        <array>
	            <bean class="com.github.pagehelper.PageInterceptor">
	                <property name="properties">
	                    <!--使用下面的方式配置参数,一行配置一个 -->
	                    <value>
	                        reasonable=true
	                    </value>
	                </property>
	            </bean>
	        </array>
	    </property>
    </bean>  

3.在controller中添加如下:

	//分页查询
	@RequestMapping("/pageList")
	public PageInfo<DeviceFun> pageList(int pageNo,int pageSize,String status){
		return applicationService.queryPageList(pageNo, pageSize, status);
    }

4.在Service中添加如下:

//分页查询
public ArrayList<DeviceFun> queryPageList(int pageNo,int pageSize);

5.在ServiceImpl中添加如下:

@Override
	public PageInfo<DeviceFun> queryPageList(Integer pageNo,Integer pageSize,String status){
        //页码pageNo,每页显示记录数pageSize
		 pageNo = pageNo == null?1:pageNo;
         pageSize = pageSize == null?10:pageSize;
         //测试输出
         System.out.println("pageNo:"+pageNo+";pageSize:"+pageSize);
        //设置页码和每页显示记录数
         PageHelper.startPage(pageNo, pageSize); 
        //创建List,调用查询接口
         ArrayList<DeviceFun> deviceFun = new ArrayList<DeviceFun>();
         deviceFun = applicationDao.pageList();//获取所有设备信息
        //用PageInfo对结果进行包装
         PageInfo<DeviceFun> page  = new PageInfo<DeviceFun>(deviceFun);
         return page;
    } 

6.在Dao中添加如下:

//分页查询
public ArrayList<DeviceFun> pageList();

7.在Mapper中添加如下:

<!-- 查询表单接口 -->
  <select id="pageList"  resultMap="com.×××项目名.model.DeviceFun">
		select * from [DeviceFun]
  </select> 

建议:
(1)PageHelper5.x版本4.×以及之前版本配置是有区别,例如:5.x版本在spring-mybatis.xml里面不需要配置数据库方言dialect。
(2)多表查询的分页,如果分页出现异常或重复数据,可能是SQL语句编写错误,建议先在数据库跑一遍sql语句,验证一下。
(3)如果分页异常,可以试着在sql语句追加:ORDER BY D_Id DESC。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值