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

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);

}
Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article