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