springboot+mybatis-plus實現多表聯查+分頁
MyBatis-Plus是一款國產的框架,優化了許多操作。
本次主要記錄一下,多表聯查和分頁的使用。
pom文件
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7.1</version>
</dependency>
配置分頁插件
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
定義vo類
由於我的videos視頻表需要多兩個屬性,分別是
private String faceImage;
private String nickname;
,而這個兩個屬性來自於用戶users表,因此創建了一個videosVo類,把上面兩個屬性都放在里面,具體代碼如下:
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="Videos對象", description="視頻信息表")
public class VideosVo implements Serializable {
private static final long serialVersionUID=1L;
private String id;
private String userId;
private String audioId;
private String videoDesc;
private String videoPath;
private Float videoSeconds;
private Integer videoWidth;
private Integer videoHeight;
private String coverPath;
private Long likeCounts;
private Integer status;
private Date createTime;
private String faceImage;
private String nickname;
}
創建mapper+xml
參數為分頁對象,暫時不用管,后面controller層構造該對象傳進去就完事了。
public interface VideosMapperCustom extends BaseMapper<VideosVo> {
// 采用注解的方式也可以@Select("SELECT * FROM fy_user u LEFT JOIN fy_role r ON u.role = r.id")
// List<UserRoleVo> selectUserListPage(Page<UserRoleVo> pagination);
Page<VideosVo> queryAllVideos(Page<VideosVo> pagination);
}
<?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.wxapp.video.mapper.VideosMapperCustom">
<select id="queryAllVideos" resultType="com.wxapp.video.vo.VideosVo" >
select v.* ,u.face_image as face_image,u.nickname as nickname from videos as v
left join users as u
on u.id=v.user_id
where 1=1
and v.status=1
order by v.create_time DESC
</select>
</mapper>
測試類
@Autowired
private VideosMapperCustom videosMapperCustom;
//測試分頁+ 多表
@Test
public void test4(){
Page<VideosVo> page = new Page<>(2, 5);
Page<VideosVo> results = videosMapperCustom.queryAllVideos(page);
List<VideosVo> resultList = results.getRecords();
for (VideosVo r:resultList
) {
System.out.println("一條記錄:"+r);
}
System.out.println("============================");
results.hasNext();
System.out.println("是否有下一頁:"+results.hasNext());
System.out.println("當前頁:"+results.getCurrent());
System.out.println("總數:"+results.getTotal());
System.out.println("getPages():"+results.getPages());
System.out.println("getOrders():"+results.getOrders());
System.out.println("getSize():"+results.getSize());
}
結果
(數據部分,過多不贅述)
是否有下一頁:true
當前頁:2
總數:12
getPages():3
getOrders():[]
getSize():5
page
參數說明
// private List<T> records; 對象列表
// private long total; 總記錄
// private long size; 每頁記錄數
// private long current; 當前的頁數
// private List<OrderItem> orders; //和數據庫列有關
// private boolean optimizeCountSql; //是否記錄優化
// private boolean isSearchCount; //是否搜索