【SSM 5】Mybatis分页插件的使用

 

【SSM 5】Mybatis分页插件的使用

一、添加maven依赖项

[html]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><dependency>  
  2.     <groupId>com.github.miemiedev</groupId>  
  3.     <artifactId>mybatis-paginator</artifactId>  
  4. </dependency>  
  5. <dependency>  
  6.     <groupId>com.github.pagehelper</groupId>  
  7.     <artifactId>pagehelper</artifactId>  
  8. </dependency></span>  

版本号:

[html]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><pagehelper.version>3.4.2-fix</pagehelper.version>  
  2. <mybatis.paginator.version>1.2.15</mybatis.paginator.version></span>  

二、Mybatis配置文件(SqlMapConfig.xml)增加分页插件

[html]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE configuration  
  3.     PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  4.     "http://mybatis.org/dtd/mybatis3 config.dtd">  
  5.       
  6. <configuration>  
  7.     <!-- 配置分页插件 -->  
  8.     <plugins>  
  9.         <plugin interceptor="com.github.pagehelper.PageHelper">  
  10.             <!-- 设置数据库类型Oracle,MySQL,MarinDBName,SQLite,PostareSQL六种数据库 -->  
  11.             <property name="dialect" value="mysql"/>  
  12.         </plugin>  
  13.     </plugins>  
  14. </configuration></span>  

三、spring整合

[html]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">   </bean>  
  2.     <!-- 让spring管理sqlsessionfactory-->  
  3.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  4.         <!-- 数据库连接池 -->  
  5.         <property name="dataSource" ref="dataSource" />  
  6.         <!-- 加载mybatis的全局配置文件 -->  
  7.         <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />  
  8.     </bean>  
  9.     <!-- 配置扫描包,加载mapper代理对象 -->  
  10.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  11.         <property name="basePackage" value="Angel.mapper" />  
  12.     </bean></span>  

四、service分页实现

[java]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">@Override  
  2.     public EUDataGridResult selectAll(int pageNum,int pageSize) {  
  3.   
  4.         //设置分页的参数  
  5.         PageHelper.startPage(pageNum, pageSize);  
  6.         //查询数据  
  7.         List<TbUser> list=userMapper.selectAll();  
  8.         //创建一个返回值对象  
  9.         EUDataGridResult result=new EUDataGridResult();  
  10.         result.setRows(list);  
  11.           
  12.         //取记录总条数  
  13.         PageInfo<TbUser> pageInfo=new PageInfo<>(list);  
  14.         result.setTotal(pageInfo.getTotal());  
  15.           
  16.         return result;  
  17.     }</span>  

附:EUDataGridResult 类

[java]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package Angel.pojo;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class EUDataGridResult {  
  6.     private long total;  
  7.     private List<?> rows;  
  8.     public long getTotal() {  
  9.         return total;  
  10.     }  
  11.     public void setTotal(long total) {  
  12.         this.total = total;  
  13.     }  
  14.     public List<?> getRows() {  
  15.         return rows;  
  16.     }  
  17.     public void setRows(List<?> rows) {  
  18.         this.rows = rows;  
  19.     }  
  20. }</span>  

五、controller实现

[java]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">@RequestMapping("/user/findAll")  
  2.     @ResponseBody  
  3.     public EUDataGridResult getItemList(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="10")Integer rows,HttpServletResponse response) throws IOException{  
  4.         EUDataGridResult result=userService.selectAll(page, rows);  
  5.         return result;  
  6.     }</span>  

六、JSP页面

[plain]  view plain  copy
 print ?
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";  
  5. %>  
  6.   
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  8. <html>  
  9. <head>  
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  11.   
  12. <title>Insert title here</title>  
  13. <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.1/themes/default/easyui.css" />  
  14. <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.1/themes/icon.css" />  
  15. <script type="text/javascript" src="js/jquery-easyui-1.4.1/jquery.min.js"></script>  
  16. <script type="text/javascript" src="js/jquery-easyui-1.4.1/jquery.easyui.min.js"></script>  
  17. <script type="text/javascript" src="js/jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js"></script>  
  18.   
  19. </head>  
  20. <body>  
  21. <table class="easyui-datagrid" id="itemList" title="用户列表"  
  22.  data-options="url:'<%=basePath %>user/findAll',pageSize:10,pagination:true">  
  23.     <thead>  
  24.         <tr>   
  25.             <th data-options="field:'id',width:70">ID</th>  
  26.             <th data-options="field:'username',width:60">名称</th>  
  27.             <th data-options="field:'phone',width:70">电话</th>  
  28.             <th data-options="field:'email',width:70">邮箱</th>  
  29.             <th data-options="field:'created',width:130,align:'center'">创建日期</th>  
  30.             <th data-options="field:'updated',width:130,align:'center'">更新日期</th>  
  31.        </tr>  
  32.     </thead>  
  33. </table>  
  34. </body>  
  35. </html></span>  

七、总结

在最开始的时候,想自己写一个分页实现,但是,后来就发现有Mybatis已经封装好的分页插件,就简单的配置一下就可以使用了。到现在还是感觉,自己对已有资源的使用还不够,每次都闹着自己创新创造,但是,先向别人学习,这一步也很重要。

然后在实现的时候,先是和王高高弄这个分页插件,不知道为什么,她那儿弄了好久都没有成功,后来自己回来写demon,发现一下子就成功了。我想,肯定是少配置文件了!


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值