本文档基于配置好了的 Spring + Spring MVC + MyBatis,可以参考:
http://www.cnblogs.com/QingXiaxu/p/8144968.html
里面有源码下载,基于其中的 ssm 代码
1、添加 PageHelper 依赖
<!-- pagehelper分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
2、在 applicationContext.xml 中添加 PageHelper 配置(黄色背景处)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd "> <!-- mybatis:scan会将com.app.mapper包里的所有接口当作mapper配置,之后可以自动引入mapper类 --> <mybatis:scan base-package="com.app.mapper" /> <!-- 扫描com.app包下面的java文件,有Spring的相关注解的类,则把这些类注册为Spring的bean --> <context:component-scan base-package="com.app" /> <!-- 使用PropertyOverrideConfigurer后处理器加载数据源参数 --> <context:property-override location="classpath:db.properties" /> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" /> <!-- 配置SqlSessionFactory,org.mybatis.spring.SqlSessionFactoryBean是Mybatis社区开发用于整合Spring的bean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource"> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value>
helperDialect=mysql </value> </property> </bean> </array> </property> </bean> <!-- JDBC事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> <!-- 启用支持annotation注解方式事务管理 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
3、在 UserMapper.java 里面添加一个查询所有数据的方法(黄色背景处)
package com.app.mapper; import java.util.List; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; import com.app.entity.User; public interface UserMapper { @Select("select * from tb_user where id = #{id}") User findUserById(Integer id); @Insert("insert into tb_user(userName, birth) values(#{userName}, #{birth})") @Options(useGeneratedKeys = true, keyProperty = "id") Integer saveUser(User user); @Select("select * from tb_user") List<User> findAllUser(); }
4、Service 层
UserServer.java
package com.app.servcie; import java.util.List; import com.app.entity.User; public interface UserService { User findUserById(Integer id); Integer saveUser(User user); List<User> findAllUser(); }
UserServiceImp.java
package com.app.servcie; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.app.entity.User; import com.app.mapper.UserMapper; @Service(value = "userService") public class UserServiceImp implements UserService { @Autowired private UserMapper userMapper; @Override public Integer saveUser(User user) { return userMapper.saveUser(user); } @Override public User findUserById(Integer id) { return userMapper.findUserById(id); } @Override public List<User> findAllUser() { return userMapper.findAllUser(); } }
5、UserController.java 添加一个查询所有用户信息的接口(黄色背景处)
package com.app.controller; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.app.entity.User; import com.app.servcie.UserService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/insertUser") @ResponseBody public User insertUser(User user) { userService.saveUser(user); User resultUser = userService.findUserById(user.getId()); return resultUser; } @RequestMapping("/getAllUser") @ResponseBody public PageInfo<User> getAllUser(@RequestParam(required = false, defaultValue = "1") Integer startPage, @RequestParam(required = false, defaultValue = "2") Integer PageSize) { PageHelper.startPage(startPage, PageSize); List<User> users = new ArrayList<User>(); users = userService.findAllUser(); PageInfo<User> pageData = new PageInfo<>(users); return pageData; } }
PageInfo 类中定义了分页需要的各种数据,比如 PageNum,PageSize,total 等数据,因此直接返回一个 PageInfo 的对象数据即可,这样前端即可将这些数据用在页面的分页组件上。
6、测试
查询第 1 页的 2 条数据:
可以看到,查询到了“小明”、“小红” 2 条数据。
查询第 2 页的 2 条数据:
此处由于数据库中总共只有 3 条数据,所以查询第 2 页数据的时候只查询到了“小军”这 1 条数据。