PageHelper在Spring+Spring MVC+MyBatis中的配置使用

本文档基于配置好了的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")
    public Integer saveUser(User user);
    
    @Select("select * from tb_user")
   public 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 List<User> getAllUser(@RequestParam(required = false, defaultValue = "1") Integer startPage,
            @RequestParam(required = false, defaultValue = "5") Integer PageSize) {
        PageHelper.startPage(startPage, PageSize);
        List<User> users = new ArrayList<>();
        users = userService.findAllUser();
        PageInfo<User> pi = new PageInfo<>(users);
        return users;
    }
}

复制代码

 

6、测试(查询第2页的5条数据)

 

展开阅读全文

没有更多推荐了,返回首页