在使用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