前言
对于单表为主的开发任务,MybatisPlus使用起来更加顺手和简便。但总免不了要有联表查询的时候,这时候xml的优势就体现出来了,记录一下如何使用MybatisPlus进行联表分页查询。
代码实现
分页配置
开启MybatisPlus的分页插件
@Configuration
@MapperScan("com.example.nb_road.mapper")
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
实体类及VO
实体类:路段。路段中的roadId代表了所属道路的id,因此需要联表查询所属道路的名称。
package com.example.nb_road.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
@TableName(value = "section")
@Data
public class RoadSection implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(type = IdType.AUTO)
private Long id;
private Long roadId;
private Double length;
private String start;
private String end;
private String coordinates;
private Integer state;
private Integer isDeleted;
}
VO类在继承实体类的基础上,增加道路名称属性。
package com.example.nb_road.entity.vo;
import com.example.nb_road.entity.RoadSection;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class RoadSectionVO extends RoadSection {
private String roadName;
}
Mapper接口
@Repository
public interface RoadSectionMapper extends BaseMapper<RoadSection> {
/**
* 自定义分页查询
*
* @param page 分页对象
* @param roadName 其中一个查询参数,路段名称
* @return 包含的查询结果的分页对象
*/
IPage<RoadSectionVO> selectPage(IPage<RoadSectionVO> page, @Param("roadName") String roadName);
}
Mapper文件(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.example.nb_road.mapper.RoadSectionMapper">
<select id="selectPage" resultType="com.example.nb_road.entity.vo.RoadSectionVO">
select s.id, r.road_name
from section s join road r on s.road_id = r.id
<where>
<if test="roadName != null and roadName != ''">and r.road_name = # {roadName}</if>
</where>
</select>
</mapper>
测试
@Slf4j
@SpringBootTest
class RoadSectionMapperTest {
@Autowired
private RoadSectionMapper roadSectionMapper;
@Test
void testPage(){
// 构建分页对象
IPage<RoadSectionVO> page = new Page<>(1, 2);
// 调用查询方法
IPage<RoadSectionVO> roadSectionIPage = roadSectionMapper.selectPage(page, "雪里段");
// 从分页对象中取出查询结果
List<RoadSectionVO> records = roadSectionIPage.getRecords();
records.forEach(System.out::println);
}
}
控制台输出
JDBC Connection [HikariProxyConnection@994185757 wrapping com.mysql.cj.jdbc.ConnectionImpl@7a2ab862] will not be managed by Spring
==> Preparing: SELECT COUNT(*) AS total FROM section s JOIN road r ON s.road_id = r.id WHERE r.road_name = ?
==> Parameters: 雪里段(String)
<== Columns: total
<== Row: 3
<== Total: 1
==> Preparing: select s.id, r.road_name from section s join road r on s.road_id = r.id WHERE r.road_name = ? LIMIT ?
==> Parameters: 雪里段(String), 2(Long)
<== Columns: id, road_name
<== Row: 1, 雪里段
<== Row: 2, 雪里段
<== Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2a53f215]
RoadSectionVO(roadName=雪里段)
RoadSectionVO(roadName=雪里段)
可以发现,只要将分页对象作为mapper方法的第一个参数,MybatisPlus会在开始查询前计算分页数据。完成相应的分页查询。