SpringBoot中PageHelper分页插件的简单使用

今天写写这个我认为比较好用的分页插件,插件可从GitHub获得:

PageHelper maven依赖

PageHelper SpringBoot依赖

我使用的是Spring Boot项目,第一步,先引入依赖:

  <!-- ojdbc -->
  <dependency>
	<groupId>com.oracle</groupId>
	<artifactId>ojdbc7</artifactId>
	<version>${ojdbc7.version}</version>
  </dependency>
  <!-- pagehelper -->
  <dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper-spring-boot-starter</artifactId>
	<version>1.2.5</version>
  </dependency>
  <!-- mybatis -->
  <dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>${mybatis-spring-boot-starter.version}</version>
  </dependency>

编写配置文件:

#项目运行端口
server.port=8080

#spring configuration start
#datasource start
#指明连接池类型为:HikariCP
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
#改成自己的URL,用户名和密码
spring.datasource.url=jdbc:oracle:thin:@10.1.13.64:1521:orcl
spring.datasource.username=c##ECHO
spring.datasource.password=orcl
#连接池配置
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=DatebookHikariCP
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=select * from dual
#datasource end
#spring configuration end

#mybatis start
#指定应起别名的类所在位置
mybatis.type-aliases-package=com.ovo.entity,com.ovo.condition
#指明mapper映射文件的位置
mybatis.mapper-locations=classpath:mappers/*Mapper.xml
#mybatis end

#pagehelper start
pagehelper.helperDialect=oracle
pagehelper.reasonable=true
pagehelper.params=
pagehelper.supportMethodsArguments=true
pagehelper.page-size-zero=true
#pagehelper end

编写查询所需的条件类:

/**
 * @author EchoMao
 * @date 2019年1月17日下午2:07:01
 * 
 */
@Component
@Scope(scopeName = "prototype")
public class UserCondition {

    private Integer qUserId;     //查询用户id
    private String qUserName;    //查询用户名
    private Integer qAge;        //查询年龄

    //getter/setter、toString()方法省略

编写DAO层接口:

 /**  
  * 根据条件查询所有
  *
  * @param condition 传入的条件参数
  * @return
  */
 List<UserInfo> findAll(UserCondition condition);

编写mapper映射文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ovo.dao.UserDao">
    <resultMap type="userInfo" id="userWithTable">
        <id property="userId" column="USERID"/>
        <result property="userName" column="USERNAME"/>
        <result property="age" column="AGE"/>
    </resultMap>
    
    <!--本节类名皆已设置别名为原类名首字母小写-->
    <select id="findAll" parameterType="userCondition" resultMap="userWithTable" >
        SELECT <include refid="allOfInsurance" /> FROM USER_INFO
        <where>
            <!--此处"_parameter"即传入的"parameterType"-->
            <if test="_parameter!=null">
                <if test="_parameter.qUserId>0">
                    AND USERID = #{qUserId}
                </if>
                <if test="_parameter.qUserName!=null && _parameter.qUserName!=''">
                    AND USERNAME LIKE '%'||#{qUserName}||'%'
                </if>
                <if test="_parameter.qAge>0">
                    AND AGE = #{qAge}
                </if>
            </if>
        </where>
    </select>
    
    <sql id="allOfUser">
        USERID,USERNAME,AGE
    </sql>
    
</mapper>

编写Service层接口:

/**
 * 根据条件查
 * 
 * @param condition    条件参数
 * @param page         当前页数
 * @param size         当前页容量
 * @return
 */
PageInfo<UserInfo> findAll(UserCondition condition, Integer page, Integer size);

实现Service层接口:

/**
 * @author EchoMao
 * @date 2019年1月17日下午2:00:26
 * 
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    IUserDao userDao;

    /**
     * 条件分页查询
     * 
     */
    @Override
    public PageInfo<UserInfo> findAll(UserCondition condition, Integer page,
            Integer size) {
        //首先调用PageHelper的分页方法
        PageHelper.startPage(page, size);
        //需要分页的查询方法必须紧跟在分页方法后
        List<UserInfo> userInfos = userDao.findAll(condition);
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfos);
        return pageInfo;
    }
    
}

最后是Controller层:

/**
 * @author EchoMao
 * @date 2019年1月17日下午5:09:00
 * 
 */
@RestController
@RequestMapping(value = "/userInfo")
public class UserController {

    @Autowired
    IUserService userService;

    /**
     * >分页与多条件查询
     * 
     * @param condition  传入的条件参数
     * @param page       当前页数
     * @param size       当前页容量
     * @return RetResult<PageInfo<UserInfo>> 统一返回的数据格式
     */
    @RequestMapping(value = "/userList")
    public RetResult<PageInfo<UserInfo>> toUserList(UserCondition condition,
            @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) { // 将page及size的默认值设为0,以防空指针
        // 使用pagehelper帮助分页
        PageInfo<UserInfo> pageInfo = userService.findAll(condition, page, size);
        return RetResponse.makeOKRsp(pageInfo);
    }
}

就是这样了。

第一次写博客,如果有什么纰漏还请不客气地指出,谢谢。

Ciao~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值