How to use QueryWrapper.select(columns) in Custom SQL statements in mybatis-plus?

This is my wrapper.

QueryWrapper<Tag> tagQueryWrapper = Wrappers.<Tag>query() .select("name", "count(*) num") .groupBy("name") .orderByDesc("num"); 

This is my mapper.

@Component public interface TagMapper extends BaseMapper<Tag> { @Select("select * from tag left join blog_tag bt on tag.id = bt.tag_id ${ew.customSqlSegment}") List<Tag> selectNameNum(IPage<Tag> page, @Param(Constants.WRAPPER) Wrapper<Tag> queryWrapper); } 

I don't know why this 'select(column...)' doesn't work. The SQL statements it generates are as follows:

select * from tag left join blog_tag bt on tag.id = bt.tag_id GROUP BY name ORDER BY num DESC LIMIT ?,?


please help me, I want to know how to replace the '*' in the SQL statement with select(column...)

1

1 Answer

Given that Constants.WRAPPER is ew you can use another getter in Wrapper to get the select columns list (similar to how you get customSqlSegment already) like this:

@Component public interface TagMapper extends BaseMapper<Tag> { @Select("select ${ew.sqlSelect} from tag left join blog_tag bt on tag.id = bt.tag_id ${ew.customSqlSegment}") List<Tag> selectNameNum(IPage<Tag> page, @Param(Constants.WRAPPER) Wrapper<Tag> queryWrapper); } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like