How to Use Querywrapper. Select(Columns) in Custom Sql Statements in Mybatis-Plus?
This Is My Wrapper. Querywrapper tagQueryWrapper = Wrappers. Query(). Select("Name", "Count(*) Num"). groupBy("name"). orderByDesc("num"); This Is My Mapper...
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...)
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);
}