分页插件的使用

    在项目中经常要用到分页功能,一种办法是自己去实现,另一种比较简单的办法是使用别人写好的插件。比如paginator或者pageHelper,下面介绍两种插件的使用。
mybatis-paginator
pom.xml

<!-- mybatis分页插件 -->
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>1.2.17</version>
</dependency>

mybatis.xml
<plugins>
<!-- 分页插件配置 -->
<plugin interceptor="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
<property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.MySQLDialect"/>
</plugin>
</plugins>

service.java
public class SaleChanceService {
@Autowired
private SaleChanceDao saleChanceDao;

public Map<String, Object> selectForPage(SaleChanceQuery query) {
// 构建一个分页对象
Integer page = query.getPage();
if (page == null) {
page = 1;
}
Integer pageSize = query.getRows();
if (pageSize == null) {
pageSize = 10;
}
String sort = query.getSort();
if (StringUtils.isBlank(sort)) {
sort = "id.desc"; // 数据库字段.desc/asc
}
PageBounds pageBounds = new PageBounds(page, pageSize, Order.formString(sort));
// 查询
List<SaleChance> saleChances = saleChanceDao.selectForPage(query, pageBounds);
PageList<SaleChance> result = (PageList<SaleChance>) saleChances;
// 返回分页结果
Paginator paginator = result.getPaginator();
Map<String, Object> map = new HashMap<>();
map.put("paginator", paginator);
map.put("rows", result);
map.put("total", paginator.getTotalCount());
return map;
}
}

dao
public interface SaleChanceDao {
// @Select("select * from t_sale_chance where is_valid = 1")
@SelectProvider(type = SaleChanceProvider.class, method="selectForPage")
List<SaleChance> selectForPage(SaleChanceQuery query, PageBounds pageBounds);
}

拼接sql语句
public class SaleChanceProvider {
private static Logger logger = LoggerFactory.getLogger(SaleChanceProvider.class);
public String selectForPage(final SaleChanceQuery query) {
String sql = new SQL(){{
SELECT("t.id, t.customer_name, t.overview, t.link_man, t.link_phone, t.create_man, "
+ " t.create_date, t.assign_man,t.assign_time,t.state");
FROM("t_sale_chance t");
WHERE("is_valid = 1");
if (StringUtils.isNoneBlank(query.getCustomerName())) {
AND().WHERE("customer_name like '%"+ query.getCustomerName() +"%'");
}
if (StringUtils.isNoneBlank(query.getOverview())) {
AND().WHERE("overview like '%"+ query.getOverview() +"%'");
}
if (StringUtils.isNoneBlank(query.getCreateMan())) {
AND().WHERE("create_man like '%"+ query.getCreateMan() +"%'");
}
if (query.getState() != null) {
AND().WHERE("state = #{state}");
}
}}.toString();
logger.debug("打印的sql是:{}", sql);
return sql;
}
}

mybatis-pageHelper
pom.xml
<!-- pageHelper 坐标添加 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
controller
@ResponseBody
@RequestMapping("queryAccountsByParams")
public Map<String, Object> queryAccountsByParams(AccountQuery accountQuery){
accountQuery.setUserId(1);
return accountService.queryAccountsByParams(accountQuery);
}
service
public Map<String, Object> queryAccountsByParams(AccountQuery accountQuery){
PageInfo<Account> pageInfo= queryForPage(accountQuery);
Map<String, Object> map=new HashMap<String, Object>();
map.put("total", pageInfo.getTotal());
map.put("rows", pageInfo.getList());
return map;
}
public PageInfo<T> queryForPage(BaseQuery baseQuery){
PageHelper.startPage(baseQuery.getPage(), baseQuery.getRows());
return new PageInfo<T>(baseDao.queryForPage(baseQuery));
}
BaseQuery.java
public class BaseQuery {
private Integer page=1;
private Integer rows=10;
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}

}
dao
public List<T> queryForPage(BaseQuery baseQuery);

mapper.xml
<!-- if 标签 参数校验 -->
<select id="queryForPage" parameterType="accountQuery"
resultType="account">
select
<include refid="account_sql" />
from t_account
<where>
user_id=#{userId}
<if test="aname !=null and aname !=''">
and aname like concat('%',#{aname},'%')
</if>
<if test="type !=null and type !=''">
and type=#{type}
</if>
<if test="time!=null and time!=''">
<![CDATA[
and create_time <=#{time}
]]>
</if>
</where>

</select>
PageHelper是Mybatis提供的分页插件,可以帮助我们方便地进行分页查询。首先,我们需要在项目中导入PageHelper的jar包,并在配置文件中进行相应的配置。配置文件中可以设置分页插件的一些参数,比如支持的数据库类型、启用合理化、参数映射等。 在使用PageHelper时,我们可以在Controller或Service实现类中调用PageHelper的方法来实现分页功能。可以通过调用PageHelper.startPage方法来指定当前页码和每页显示的记录数,然后执行相应的查询操作即可。PageHelper会自动拦截并处理对应的查询语句,实现分页查询的效果。 需要注意的是,PageHelper支持通过Mapper接口参数来传递分页参数,默认情况下会根据配置文件中的params字段来自动获取参数值。如果pageSize设置为0,表示查询所有结果而不进行分页查询。 以上是PageHelper分页插件使用方法,通过导入jar包、配置文件和调用方法即可实现分页功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [PageHelper 分页插件使用总结](https://blog.csdn.net/m0_46219348/article/details/124548406)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [分页插件pagehelper使用](https://blog.csdn.net/qq_45620896/article/details/115332873)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值