MyBatisPlus优雅的自定义SQL实现表联查并且使用IPage分页

一直追求优雅代码和逻辑,一般正常自定义SQL使用分页工具分页,还得再写一个查询行数的接口,业务性能不说,感觉多此一举。

参考文章:https://www.cnblogs.com/jiaozhang/p/14473344.html

mapper

//自定义查询数据
IPage<Map<String,Object>> query(@Param("page") Page<Map<String,Object>> page,@Param("params") Map<String, Object> params);

service

IPage<Map<String,Object>> query(@Param("page") Page<Map<String,Object>> page, @Param("params") Map<String, Object> params);

serviceImpl

@Override
    public IPage<Map<String, Object>> query(Page<Map<String, Object>> page, Map<String, Object> params) {
        return userMapper.query(page,params);
    }

xml

<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

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

 

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注