Mybatis分页插件

springmvc与spring-mybatis的配置与普通的SSM项目配置是一样的,

只不过在spring-mybatis中需要加入分页拦截器

一、spring-mybatis.xml的配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 7 http://www.springframework.org/schema/context
 8 http://www.springframework.org/schema/context/spring-context-4.3.xsd
 9 http://www.springframework.org/schema/tx
10 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
11 ">
12     <!-- 自动扫描包 -->
13     <context:component-scan base-package="com.llh.*" />
14     
15     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
16         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
17         <property name="url" value="jdbc:mysql://localhost:3306/pagetext"></property> 
18         <property name="username" value="root"></property>
19         <property name="password" value="123456"></property>
20     </bean>
21     <!-- mybatis配置 -->
22     <bean class="org.mybatis.spring.SqlSessionFactoryBean">
23         <property name="dataSource" ref="dataSource"></property>
24         <!-- 分页拦截器 -->
25         <property name="plugins">
26             <array>
27                 <bean class="com.github.pagehelper.PageInterceptor">
28                     <property name="properties">
29                         <value>
30                             helperDialect=mysql
31                             rowBoundsWithCount=true
32                             supportMethodsArguments=true
33                             reasonable=true
34                         </value>
35                     </property>
36                 </bean>
37             </array>
38         </property>
39 
40     </bean>
41     <!-- 映射配置器 -->
42     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
43         <property name="basePackage" value="com.llh.dao"></property>
44     </bean>
45     <!-- 事务管理器 -->
46     <bean id="transactionManager"
47         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
48         <property name="dataSource" ref="dataSource"></property>
49     </bean>
50 
51     <!-- 开启事务注解 -->
52     <tx:annotation-driven />
53 
54 </beans>

二、在Controller里直接调用即可

 1 package com.llh.controller;
 2 
 3 import java.util.List;
 4 
 5 import javax.annotation.Resource;
 6 import javax.servlet.http.HttpSession;
 7 
 8 import org.springframework.context.annotation.Scope;
 9 import org.springframework.stereotype.Controller;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 
13 import com.github.pagehelper.PageHelper;
14 import com.github.pagehelper.PageInfo;
15 import com.llh.entity.Student;
16 import com.llh.service.StudentService;
17 
18 import net.sf.json.JSONArray;
19 
20 @Controller
21 @Scope("prototype")
22 public class StudentController {
23 
24     @Resource
25     private StudentService studentService;
26     @RequestMapping(value="selectAllStudent",produces="text/html;charset=utf-8")
27     private @ResponseBody String selectAllStudent(HttpSession session,int pageCode,int pageSize){
28         
29         PageHelper.startPage(pageCode, pageSize);
30         
31         List<Student> slist =  studentService.selectAll();
32         PageInfo<Student> spi = new PageInfo<Student>(slist);
33         int count = (int) spi.getTotal();
34         JSONArray json = JSONArray.fromObject(slist);
35         String str="{"total":"+count+","rows":"+json.toString()+"}";
36         return str;
37     }
38     
39 }

说明一下,这里使用到的是bootstrap前端,有需要的话可以查看本人的博客园首页,有bootstrap-fileinput上传的实例

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 <link href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet"></link>
 9 <link href="${pageContext.request.contextPath }/bootstrap-table-develop/docs/dist/bootstrap-table.css" rel="stylesheet"></link>
10 <script src="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/js/jquery.js"></script>
11 <script src="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
12 <script src="${pageContext.request.contextPath }/bootstrap-table-develop/docs/dist/bootstrap-table.js"></script>
13 <script src="${pageContext.request.contextPath }/bootstrap-table-develop/docs/dist/js/bootstrap-table-locale-all.js"></script>
14 <script src="${pageContext.request.contextPath }/bootstrap-table-develop/src/locale/bootstrap-table-zh-CN.js"></script>
15 </head>
16 <body>
17 <table id="result" class="table table-hover" style="text-align: center;">
18     <thead>
19         <th data-field="stuname">部门编号</th>
20         <th data-field="stuid">部门名称</th>
21         <th data-field="classes.classname">部门名称</th>
22     </thead>
23 </table>
24 </body>
25 <script type="text/javascript">
26 $(function () {
27     $("#result").bootstrapTable({
28         url:"selectAllStudent.do",
29         method:"post",
30         cache:false,
31         dataType:"json",
32         contentType:"application/x-www-form-urlencoded",//post的方式提交的话需要写
33         toolbar:"#toolbar",
34         toolbarAlign:"left",
35         striped:true,
36         pagination:true,
37         sidePagination:"server",
38         pageNumber:1,
39         pageSize:5,
40         pageList:[5,10,15],
41         locale:"zh-CN",
42         queryParamsType:"limit",
43         queryParams:queryParams
44     });
45 });
46     
47 function queryParams(params){
48     var params={
49             pageSize:params.limit,
50             pageCode:params.offset/params.limit+1
51     };
52     return params;
53 }
54 </script>
55 </html>