前言:
解读一下这个标题
mybatis-plus 自定义sql +使用wapper 分页
自定义sql:指的是程序员自己写的sql语句
使用wapper 分页:使用mybatis-plus的wrapper的分页功能
一、返回 IPage 对象
1.1 第一步:Dao接口创建一个自定义方法
代码如下:
/**
* 自定义sql分页
* @param page
* @param queryWrapper 看这里看这里,如果自定义的方法中需要用到wrapper查询条件,需要这样写
* @return
*/
IPage<CategoryVo> listJoinInfoPages(IPage<CategoryVo> page, @Param(Constants.WRAPPER) Wrapper<CategoryVo> queryWrapper);
-
CategoryVo
:这个是我自定义的类,根据你的需要换成你的 -
listJoinInfoPages
:自定义的方法名,换成你的 - 其他的格式请按照我的代码格式(参数类型,返回值类型)
- 关于引用如下(其中的引用要使用正确):
import com.baomidou.mybatisplus.core.conditions.Wrapper; // Constants的引用
import com.baomidou.mybatisplus.core.metadata.IPage; // IPage的引用
import com.baomidou.mybatisplus.core.toolkit.Constants; // Constants 引用
如图:
1.2 第二步:mapper.xml 写自己的sql语句
这里可以是单表,也可以是多表关联查询
但是一定不要写where条件
(后面会说)
<select id="listJoinInfoPages" resultType="com.ctra.ctraplatform.race.vo.CategoryVo">
select a.*,b.race_name,b.year from race_category a
INNER JOIN race_info b on a.race_id=b.id ${ew.customSqlSegment}
</select>
-
${ew.customSqlSegment}
:这个关键字是固定写法,不要怀疑,直接给上 - 前面说过,一定不要写where条件,因为这边使用了wrapper,在service层添加条件时(也就是第二个参数queryWrapper传入时)会带上
where 关键字
如图:
1.3 第三步:service 接口
PageUtils listJoinInfo(Map<String, Object> params);
-
PageUtils
:这里我使用了一个page的包装类,出自 gitee:人人开源 的 renren-fast中 - listJoinInfo 方法名随便起,在实现类中调用正确的dao的方法就行了
如图:
1.4 第四步:service 接口 实现类
@Override
public PageUtils listJoinInfo(Map<String, Object> params) {
Long current = Long.parseLong(params.get("page").toString()) ;
Long size = Long.parseLong(params.get("limit").toString()) ;
String key = (String) params.get("key");
Page page = new Page(current,size);
QueryWrapper<CategoryVo> wrapper = new QueryWrapper<CategoryVo>()
// 都必须是非逻辑删除的
.eq("a.show_status",0).eq("b.show_status",0);
if (!StringUtils.isEmpty(key)) {
wrapper.and(obj->{
obj.like("a.race_category",key).or().like("b.race_name",key);
});
}
IPage<CategoryVo> categoryVoIPage = baseMapper.listJoinInfoPages(page,wrapper);
return new PageUtils(categoryVoIPage);
}
- Page类:这个类做为分页的功能,实例化时需要传入current,size分页的俩个参数
- QueryWrapper:条件查询wrapper拼接,这处会自带where 条件
注意:这里多表和单表查询的区别,多表需要表名.字段名
- IPage类:最后通过调用dao的方法生成 IPage页
- 关于引用如下:
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; // Page 类
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;// wrapper 类
import com.baomidou.mybatisplus.core.metadata.IPage;//IPage
如图:
二、返回 Page 对象
注意这里:Page的包如下(这里千万不要搞错)
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
2.1 service 业务
直接通过 new Page(current,size) 来创建 page对象
注意这里直接传入一个 Page的对象current:为传入的当前页
size:为传入的页面展示大小
List<TopicItemVo> topicItemVoList = baseMapper.listJoinMemberBaseInfo(new Page(current,size),wrapper);
2.2 Dao 层
传入 Page 对象的好处:可以自由的返回List,而不是返回IPage
灵活好用,可以再次处理List后输出
List<TopicItemVo> listJoinMemberBaseInfo(Page page, @Param(Constants.WRAPPER) Wrapper<TopicItemVo> queryWrapper);
2.3 Mapper 层
${ew.customSqlSegment}
可以接收page 和 wrapper 参数
<select id="listJoinMemberBaseInfo" resultType="io.renren.modules.social.entity.vo.TopicItemVo">
select a.id as topicItemId,
a.topic_id,
a.openid,
a.page_view,
a.forward,
a.likes,
a.content,
a.update_time,
a.create_time,
from social_topic_item a
${ew.customSqlSegment}
</select>