SpringBoot与PageHelper的整合示例详解
1.PageHelper简介
PageHelper官网地址:
https://pagehelper.github.io/
摘要: com.github.pagehelper.PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件。
PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件,其实我并不想加上好用两个字,但是为了表扬插件作者开源免费的崇高精神,我毫不犹豫的加上了好用一词作为赞美。
原本以为分页插件,应该是很简单的,然而PageHelper比我想象的要复杂许多,它做的很强大,也很彻底,强大到使用者可能并不需要这么多功能,彻底到一参可以两用。但是,我认为,作为分页插件,完成物理分页任务是根本,其它的很多智能并不是必要的,保持它够傻够憨,专业术语叫stupid,简单就是美。
我们将简单介绍PageHelper的基本使用和配置参数的含义,重点分析PageHelper作为Mybatis分页插件的实现原理。
2.SpringBoot与PageHelper的整合示例
结构图如下:
pom.xml:
4.0.0
com.home
springbootdemo
0.0.1-SNAPSHOT
jar
springbootdemo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.2.0
mysql
mysql-connector-java
com.alibaba
druid
1.0.25
org.springframework.boot
spring-boot-starter-data-redis
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-maven-plugin
true
OrderPresentController:
package com.home.orderpresentdemo.controller;
import com.github.pagehelper.PageInfo;
import com.home.orderpresentdemo.entity.OrderPresentInfo;
import com.home.orderpresentdemo.service.OrderPresentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
@Controller
@RequestMapping("/")
public class OrderPresentController {
@Autowired
private OrderPresentInfoService orderPresentService;
/**
* 跳转到应用列表页面
* @param pageNo 要显示第几页内容
* @param pageSize 一页显示多少条
* @return
*/
@RequestMapping("/list")
@ResponseBody
public PageInfo list(@RequestParam(value="pageNo",defaultValue="1")int pageNo, @RequestParam(value="pageSize",defaultValue="10")int pageSize) {
PageInfo page = orderPresentService.getAllOrderPresentForPage(pageNo,pageSize);
return page;
}
@RequestMapping("/")
public String helloHtml(HashMap map, Model model) {
model.addAttribute("say","欢迎欢迎,热烈欢迎");
map.put("hello", "欢迎进入HTML页面");
return "index";
}
@RequestMapping("/goToAdd")
public String goToAdd() {
return "add";
}
@RequestMapping("/add")
public String add(OrderPresentInfo orderPresent) {
return "添加成功";
}
}
OrderPresentInfoService:
package com.home.orderpresentdemo.service;
import com.github.pagehelper.PageInfo;
import com.home.orderpresentdemo.entity.OrderPresentInfo;
import java.util.List;
public interface OrderPresentInfoService {
List getAllOrderPresent();
PageInfo getAllOrderPresentForPage(int pageNo, int pageSize);
}
OrderPresentInfoServiceImpl:
package com.home.orderpresentdemo.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.home.orderpresentdemo.entity.OrderPresentInfo;
import com.home.orderpresentdemo.mapper.OrderPresentInfoMapper;
import com.home.orderpresentdemo.service.OrderPresentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderPresentInfoServiceImpl implements OrderPresentInfoService {
@Autowired
private OrderPresentInfoMapper orderPresentMapper;
@Override
public List getAllOrderPresent() {
return orderPresentMapper.getAllOrderPresent();
}
@Override
public PageInfo getAllOrderPresentForPage(int pageNo, int pageSize) {
PageHelper.startPage(pageNo,pageSize);
List allOrderPresentList = orderPresentMapper.getAllOrderPresent();
PageInfo pageInfo = new PageInfo<>(allOrderPresentList);
return pageInfo;
}
}
OrderPresentInfo:
package com.home.orderpresentdemo.entity;
import java.math.BigDecimal;
import java.util.Date;
public class OrderPresentInfo {
private Long id;
private String activityName;
private Date beginTime;
private Date endTime;
private Integer activityStoresSelectType;
private String activityStoresIds;
private Integer memberLevelSelectType;
private String memberLevelIds;
private BigDecimal activityOrderConsume;
private String paymentChannelIds;
private Integer equityType;
private Long couponId;
private Long luckyTurningId;
private Integer activityStatus;
private Date createTime;
private Date updateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getActivityName() {
return activityName;
}
public void setActivityName(String activityName) {
this.activityName = activityName == null ? null : activityName.trim();
}
public Date getBeginTime() {
return beginTime;
}
public void setBeginTime(Date beginTime) {
this.beginTime = beginTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public Integer getActivityStoresSelectType() {
return activityStoresSelectType;
}
public void setActivityStoresSelectType(Integer activityStoresSelectType) {
this.activityStoresSelectType = activityStoresSelectType;
}
public String getActivityStoresIds() {
return activityStoresIds;
}
public void setActivityStoresIds(String activityStoresIds) {
this.activityStoresIds = activityStoresIds == null ? null : activityStoresIds.trim();
}
public Integer getMemberLevelSelectType() {
return memberLevelSelectType;
}
public void setMemberLevelSelectType(Integer memberLevelSelectType) {
this.memberLevelSelectType = memberLevelSelectType;
}
public String getMemberLevelIds() {
return memberLevelIds;
}
public void setMemberLevelIds(String memberLevelIds) {
this.memberLevelIds = memberLevelIds == null ? null : memberLevelIds.trim();
}
public BigDecimal getActivityOrderConsume() {
return activityOrderConsume;
}
public void setActivityOrderConsume(BigDecimal activityOrderConsume) {
this.activityOrderConsume = activityOrderConsume;
}
public String getPaymentChannelIds() {
return paymentChannelIds;
}
public void setPaymentChannelIds(String paymentChannelIds) {
this.paymentChannelIds = paymentChannelIds == null ? null : paymentChannelIds.trim();
}
public Integer getEquityType() {
return equityType;
}
public void setEquityType(Integer equityType) {
this.equityType = equityType;
}
public Long getCouponId() {
return couponId;
}
public void setCouponId(Long couponId) {
this.couponId = couponId;
}
public Long getLuckyTurningId() {
return luckyTurningId;
}
public void setLuckyTurningId(Long luckyTurningId) {
this.luckyTurningId = luckyTurningId;
}
public Integer getActivityStatus() {
return activityStatus;
}
public void setActivityStatus(Integer activityStatus) {
this.activityStatus = activityStatus;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
OrderPresentInfoMapper:
package com.home.orderpresentdemo.mapper;
import com.home.orderpresentdemo.entity.OrderPresentInfo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface OrderPresentInfoMapper {
int deleteByPrimaryKey(Long id);
int insert(OrderPresentInfo record);
int insertSelective(OrderPresentInfo record);
OrderPresentInfo selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(OrderPresentInfo record);
int updateByPrimaryKey(OrderPresentInfo record);
List getAllOrderPresent();
}
OrderPresentInfoMapper.xml:
ID, ACTIVITY_NAME, BEGIN_TIME, END_TIME, ACTIVITY_STORES_SELECT_TYPE, ACTIVITY_STORES_IDS,
MEMBER_LEVEL_SELECT_TYPE, MEMBER_LEVEL_IDS, ACTIVITY_ORDER_CONSUME, PAYMENT_CHANNEL_IDS,
EQUITY_TYPE, COUPON_ID, LUCKY_TURNING_ID, ACTIVITY_STATUS, CREATE_TIME, UPDATE_TIME
delete from dss_wsh_order_present_info
where ID = #{id,jdbcType=BIGINT}
insert into dss_wsh_order_present_info (ID, ACTIVITY_NAME, BEGIN_TIME,
END_TIME, ACTIVITY_STORES_SELECT_TYPE, ACTIVITY_STORES_IDS,
MEMBER_LEVEL_SELECT_TYPE, MEMBER_LEVEL_IDS,
ACTIVITY_ORDER_CONSUME, PAYMENT_CHANNEL_IDS,
EQUITY_TYPE, COUPON_ID, LUCKY_TURNING_ID,
ACTIVITY_STATUS, CREATE_TIME, UPDATE_TIME
)
values (#{id,jdbcType=BIGINT}, #{activityName,jdbcType=VARCHAR}, #{beginTime,jdbcType=TIMESTAMP},
#{endTime,jdbcType=TIMESTAMP}, #{activityStoresSelectType,jdbcType=INTEGER}, #{activityStoresIds,jdbcType=VARCHAR},
#{memberLevelSelectType,jdbcType=INTEGER}, #{memberLevelIds,jdbcType=VARCHAR},
#{activityOrderConsume,jdbcType=DECIMAL}, #{paymentChannelIds,jdbcType=VARCHAR},
#{equityType,jdbcType=INTEGER}, #{couponId,jdbcType=BIGINT}, #{luckyTurningId,jdbcType=BIGINT},
#{activityStatus,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
)
insert into dss_wsh_order_present_info
ID,
ACTIVITY_NAME,
BEGIN_TIME,
END_TIME,
ACTIVITY_STORES_SELECT_TYPE,
ACTIVITY_STORES_IDS,
MEMBER_LEVEL_SELECT_TYPE,
MEMBER_LEVEL_IDS,
ACTIVITY_ORDER_CONSUME,
PAYMENT_CHANNEL_IDS,
EQUITY_TYPE,
COUPON_ID,
LUCKY_TURNING_ID,
ACTIVITY_STATUS,
CREATE_TIME,
UPDATE_TIME,
#{id,jdbcType=BIGINT},
#{activityName,jdbcType=VARCHAR},
#{beginTime,jdbcType=TIMESTAMP},
#{endTime,jdbcType=TIMESTAMP},
#{activityStoresSelectType,jdbcType=INTEGER},
#{activityStoresIds,jdbcType=VARCHAR},
#{memberLevelSelectType,jdbcType=INTEGER},
#{memberLevelIds,jdbcType=VARCHAR},
#{activityOrderConsume,jdbcType=DECIMAL},
#{paymentChannelIds,jdbcType=VARCHAR},
#{equityType,jdbcType=INTEGER},
#{couponId,jdbcType=BIGINT},
#{luckyTurningId,jdbcType=BIGINT},
#{activityStatus,jdbcType=INTEGER},
#{createTime,jdbcType=TIMESTAMP},
#{updateTime,jdbcType=TIMESTAMP},
update dss_wsh_order_present_info
ACTIVITY_NAME = #{activityName,jdbcType=VARCHAR},
BEGIN_TIME = #{beginTime,jdbcType=TIMESTAMP},
END_TIME = #{endTime,jdbcType=TIMESTAMP},
ACTIVITY_STORES_SELECT_TYPE = #{activityStoresSelectType,jdbcType=INTEGER},
ACTIVITY_STORES_IDS = #{activityStoresIds,jdbcType=VARCHAR},
MEMBER_LEVEL_SELECT_TYPE = #{memberLevelSelectType,jdbcType=INTEGER},
MEMBER_LEVEL_IDS = #{memberLevelIds,jdbcType=VARCHAR},
ACTIVITY_ORDER_CONSUME = #{activityOrderConsume,jdbcType=DECIMAL},
PAYMENT_CHANNEL_IDS = #{paymentChannelIds,jdbcType=VARCHAR},
EQUITY_TYPE = #{equityType,jdbcType=INTEGER},
COUPON_ID = #{couponId,jdbcType=BIGINT},
LUCKY_TURNING_ID = #{luckyTurningId,jdbcType=BIGINT},
ACTIVITY_STATUS = #{activityStatus,jdbcType=INTEGER},
CREATE_TIME = #{createTime,jdbcType=TIMESTAMP},
UPDATE_TIME = #{updateTime,jdbcType=TIMESTAMP},
where ID = #{id,jdbcType=BIGINT}
update dss_wsh_order_present_info
set ACTIVITY_NAME = #{activityName,jdbcType=VARCHAR},
BEGIN_TIME = #{beginTime,jdbcType=TIMESTAMP},
END_TIME = #{endTime,jdbcType=TIMESTAMP},
ACTIVITY_STORES_SELECT_TYPE = #{activityStoresSelectType,jdbcType=INTEGER},
ACTIVITY_STORES_IDS = #{activityStoresIds,jdbcType=VARCHAR},
MEMBER_LEVEL_SELECT_TYPE = #{memberLevelSelectType,jdbcType=INTEGER},
MEMBER_LEVEL_IDS = #{memberLevelIds,jdbcType=VARCHAR},
ACTIVITY_ORDER_CONSUME = #{activityOrderConsume,jdbcType=DECIMAL},
PAYMENT_CHANNEL_IDS = #{paymentChannelIds,jdbcType=VARCHAR},
EQUITY_TYPE = #{equityType,jdbcType=INTEGER},
COUPON_ID = #{couponId,jdbcType=BIGINT},
LUCKY_TURNING_ID = #{luckyTurningId,jdbcType=BIGINT},
ACTIVITY_STATUS = #{activityStatus,jdbcType=INTEGER},
CREATE_TIME = #{createTime,jdbcType=TIMESTAMP},
UPDATE_TIME = #{updateTime,jdbcType=TIMESTAMP}
where ID = #{id,jdbcType=BIGINT}
SqlMapperConfig.xml:
index.html(这里测试写的比较随意,可以忽略):
第一个HTML页面
Hello Spring Boot!!!
添加
application.properties:
#server.port=80
logging.level.org.springframework=DEBUG
#springboot mybatis
#jiazai mybatis peizhiwenjian
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
#mybatis.config-location = classpath:mybatis/sqlMapConfig.xml
#mybatis.type-aliases-package = com.demo.bean
#shujuyuan
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/dss_wshop?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
spring.thymeleaf.prefix=classpath:/templates/
#禁止thymeleaf缓存(建议:开发环境设置为false,生成环境设置为true)
spring.thymeleaf.cache=false
#pagehelper分页插件配置 以下属性不加也可以实现分页功能
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql