一直追求优雅代码和逻辑,一般正常自定义SQL使用分页工具分页,还得再写一个查询行数的接口,业务性能不说,感觉多此一举。
参考文章:https://www.cnblogs.com/jiaozhang/p/14473344.html
mapper
代码语言:javascript复制//自定义查询数据
IPage<Map<String,Object>> query(@Param("page") Page<Map<String,Object>> page,@Param("params") Map<String, Object> params);
service
代码语言:javascript复制IPage<Map<String,Object>> query(@Param("page") Page<Map<String,Object>> page, @Param("params") Map<String, Object> params);
serviceImpl
代码语言:javascript复制@Override
public IPage<Map<String, Object>> query(Page<Map<String, Object>> page, Map<String, Object> params) {
return userMapper.query(page,params);
}
xml
代码语言:javascript复制<select id="query" resultType="Map">
select
user.username,
dict.name
from user
inner join dict on (user.sex = dict.id)
where 1=1
<if test="params.username != null and params.username != ''">
and user.username like concat('%',#{params.username}, '%')
</if>
</select>
controller
代码语言:javascript复制Map<String, Object> params = new HashMap<>();
params.put("username",username);
Page<Map<String,Object>> page = new Page<>(1,10);
IPage<Map<String,Object>> iPage = userService.query(page,params);