Skip to content

IPage 支持在 Map 参数中生效. #6512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.baomidou.mybatisplus.core.override;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.ParameterUtils;
import org.apache.ibatis.binding.BindingException;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.cursor.Cursor;
Expand Down Expand Up @@ -107,14 +107,8 @@ public Object execute(SqlSession sqlSession, Object[] args) {

@SuppressWarnings("all")
private <E> Object executeForIPage(SqlSession sqlSession, Object[] args) {
IPage<E> result = null;
for (Object arg : args) {
if (arg instanceof IPage) {
result = (IPage<E>) arg;
break;
}
}
Assert.notNull(result, "can't found IPage for args!");
IPage<E> result = (IPage<E>) ParameterUtils.findPage(args)
.orElseThrow(() -> new IllegalArgumentException("can't found IPage for args!"));
Object param = method.convertArgsToSqlCommandParam(args);
List<E> list = sqlSession.selectList(command.getName(), param);
result.setRecords(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

Expand All @@ -36,22 +37,30 @@ private ParameterUtils() {
/**
* 查找分页参数
*
* @param parameterObject 参数对象
* @param args 参数对象(可以为 null)
* @return 分页参数
*/
public static Optional<IPage> findPage(Object parameterObject) {
if (parameterObject != null) {
if (parameterObject instanceof Map) {
Map<?, ?> parameterMap = (Map<?, ?>) parameterObject;
for (Map.Entry entry : parameterMap.entrySet()) {
if (entry.getValue() != null && entry.getValue() instanceof IPage) {
return Optional.of((IPage) entry.getValue());
}
public static <E> Optional<IPage<E>> findPage(Object... args) {
if (args == null) {
return Optional.empty();
}

if (args.length == 1 && args[0] instanceof Map) {
Collection<?> argValues = ((Map<?, ?>) args[0]).values();
for (Object arg : argValues) {
if (arg instanceof IPage) {
return Optional.of((IPage<E>) arg);
}
} else if (parameterObject instanceof IPage) {
return Optional.of((IPage) parameterObject);
}
return Optional.empty();
}

for (Object arg : args) {
if (arg instanceof IPage) {
return Optional.of((IPage<E>) arg);
}
}
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ void test() {
Page<H2User> page = new Page<>();
userMapper.testPage1(new H2User(), page);
userMapper.testPage2(page, new H2User());
Map<String, Object> map = Map.of("pageable", page, "name", "mybatis-plus");
userMapper.testPage3(map);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public interface H2UserMapper extends SuperMapper<H2User> {
@Select("select * from h2user")
IPage<H2User> testPage2(@Param(value = "user") Page page, @Param(value = "page") H2User h2User);

@Select("select * from h2user where name=#{name}")
IPage<H2User> testPage3(Map<String, Object> param);

@Select("select count(*) from h2user")
Long selectCountLong();

Expand Down