mybatis-puls 多表分頁與數據表別名解決方案(自定義sql)

多表分頁, photoType 我之前的參數是用Wrappers傳入的使用上一樣 就是ew.xxx 而已 具體看官網 多參數時建議使用,並且在實體字段上增加表名前綴

    @Select("select * from photo p LEFT JOIN label_detail ld on ld.PhotoID = p.PhotoID where p.PhotoType = ${photoType}")
    IPage<PhotoEntity> getPageBySort(IPage<PhotoEntity> page, @Param("photoType") Integer photoType);

這裏的sql就基本與你手寫一直,但是在生成sql 時page會被轉換爲sql,注意在排序oder序列中各個字段名稱要寫上面的命名 p、ld 或者 直接寫表名,若直接寫表名實體應該如下,在字段前跟上表名,這樣生成時字段會加上前綴

@Data
@TableName("photo")
public class PhotoEntity extends Model<PhotoEntity> {

    @TableId(value = "photo.PhotoID",type = IdType.AUTO)
    private Integer photoId;
    
    @TableField("photo.PhotoName")
    private String photoName;

查詢時字段如下

{
  "asc": [
    "p.FlowerNum"
  ],
  "current": 1,
  "desc": [
  ],
  "size": 10
}

當然你要是在實體裏面寫了前綴後應該

{
  "asc": [
    "photo.FlowerNum"
  ],
  "current": 1,
  "desc": [
  ],
  "size": 10
}

生成的sql

SELECT * FROM photo p LEFT JOIN label_detail ld ON ld.PhotoID = p.PhotoID WHERE p.PhotoType = 1 ORDER BY p.FlowerNum LIMIT 0,10
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章