mybatis-plus使用连表查询分页
- mybatis-plus在进行单表查询时确实很方便,但是一旦我们需要连表查询时,就不得不引用其他插件来进行操作。但是复杂的查询必然会造成我们的代码量往上涨。
当然mybatis-plus也不会脱离mybatis原本的特性,比如xml文件来操作数据库。本篇文章记录一下使用mybatis-plus来进行连表查询分页功能。
增加配置:
package com.xxxx.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Author: Eureka
* Date: 2023/11/10
* des: TODO 分页插件
*/
@Configuration
@MapperScan("com.xxx.mapper")
public class MybatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
//interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
return interceptor;
}
}
接口:
/**
* TODO: 分页查询操作
* @param param
* @return
* @throws Exception
*/
InPageResponse getInpageJobOperation(JobTmpOperateInpageParam param) throws Exception;
实现类:
@Override
public InPageResponse getInPagejobTmpForms(JobTmpFormInpageParam param) throws Exception {
Page<JobFormsVo> page = new Page<>(param.getCurpage(), param.getPagesize());
QueryWrapper<JobFormsVo> wrapper = new QueryWrapper<>();
IPage<JobFormsVo> jobFormsVos = this.jobTmpFormMp.getInPagejobTmpForms(param,page,wrapper);
InPageResponse inPageResponse = new InPageResponse();
inPageResponse.setItems(jobFormsVos.getRecords());
inPageResponse.setCount(jobFormsVos.getTotal());
inPageResponse.setPages(jobFormsVos.getPages());
return inPageResponse;
}
mapper:
/**
* Author: Eureka
* Date: 2023/12/5
* TODO:
*/
public interface JobTmpFormMp extends BaseMapper<JobTmpForm> {
IPage<JobFormsVo> getInPagejobTmpForms(@Param("param") JobTmpFormInpageParam param, @Param("page") Page<JobFormsVo> page, @Param(Constants.WRAPPER)
QueryWrapper<JobFormsVo> wrapper) throws Exception;
}
xml:
<?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.console.flow.mapper.mp.JobOperationMp">
<resultMap id="JobOperationVo" type="com.console.flow.classes.job.vo.JobOperationVo">
<id property="operateid" column="operateid"/>
<result property="name" column="name"/>
<result property="joname" column="joname"/>
<result property="selected" column="selected"/>
</resultMap>
<select id="getInpageJobOperation" parameterType="map" resultMap="JobOperationVo">
select a.operateid,
a.name,
b.joname,
ifnull(b.selected,'A003002') as selected
from gz_operation a
left join(
select b.name as joname,
b.operateid,
'A003001' as selected
from gz_jobtmp_operation b
where b.jobtmpid=#{param.jobtmpid}
) b on b.operateid = a.operateid
order by a.operateid
${ew.customSqlSegment}
</select>
</mapper>
如上就是在我们的原始查询sql最下面加上${ew.customSqlSegment}就可以实现如单表查询的丝滑操作了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!