SpringBoot學習筆記37——MybatisPlus通過@Select註解配合Wrapper類實現查詢

在使用mybatisPlus時,遇到複雜的sql還是需要自己來寫,但是如果不想將sql語句放到xml文件中的話,我們可以使用@Select註解實現查詢功能。

代碼如下:

@Select("select res.* ,tree.SORT from TS_CUR_RESOURCE  res " +
            "left join TR_CUR_TREE tree on res.ID = tree.QUESTION_RESOURCE_ID ${ew.customSqlSegment}")
    List<CurResourceVo> selectResourceByIds(@Param(Constants.WRAPPER) Wrapper<CurResource> wrapper);

調用代碼:


List<String> idList = new ArrayList<>();
        idList.add("1");
        idList.add("2");
//查詢資源並排序
        QueryWrapper<CurResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("res.ID", idList);
        queryWrapper.orderByAsc("SORT");
        List<CurResourceVo> curResources = curResourceDao.selectResourceByIds(queryWrapper);

說明:

${ew.customSqlSegment}此參數mybatisPlus會自動轉換

上述示例中會將代碼${ew.customSqlSegment}替換成 where res.ID in ('1','2')  order by SORT

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章