日常开发中,部分sql mybatis-plus 自带Mapper无法实现,需要手写,此时需要分页的话,用mybatis-plus 自带分页最方便了,简单介绍下。
Mapper 特殊的地方就是再参数那里加入com.baomidou.mybatisplus.core.metadata包下的IPage 类
public interface UserMapper{
public MyPage<User> getUserList(
IPage pageQuery,
@Param("parma") param
);
}
然后就正常在xml写sql,User类无特殊之处,就正常属性字段,加上@Data注解(或get set方法即可)
自定义MyPage类
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.ApiModelProperty;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import project.jtjr.financialtrade.LabelJsonViewBase;
import springfox.documentation.annotations.ApiIgnore;
@JsonIgnoreProperties(
value = {"orders", "hitCount", "pages", "searchCount"},
ignoreUnknown = true
)
public class MyPage<T> implements IPage<T> {
@JsonView({LabelJsonViewBase.class})
@ApiModelProperty("当前分页记录集")
private List<T> records;
@JsonView({LabelJsonViewBase.class})
@ApiModelProperty("总数据条数")
private long total;
@JsonView({LabelJsonViewBase.class})
@ApiModelProperty("每页显示条数")
private long size;
@JsonView({LabelJsonViewBase.class})
@ApiModelProperty("当前页")
private long current;
@ApiModelProperty(
hidden = true
)
private List<OrderItem> orders;
@ApiModelProperty(
hidden = true
)
private boolean optimizeCountSql;
@ApiModelProperty(
hidden = true
)
private boolean isSearchCount;
@ApiModelProperty(
hidden = true
)
private boolean hitCount;
public MyPage() {
this.records = new ArrayList();
this.total = 0L;
this.size = 9223372036854775807L;
this.current = 1L;
this.orders = new ArrayList();
this.optimizeCountSql = true;
this.isSearchCount = true;
this.hitCount = false;
}
public MyPage(long current, long size) {
this(current, size, 0L);
}
public MyPage(long current, long size, long total) {
this(current, size, total, true);
}
public MyPage(long current, long size, boolean isSearchCount) {
this(current, size, 0L, isSearchCount);
}
public MyPage(long current, long size, long total, boolean isSearchCount) {
this.records = new ArrayList();
this.total = 0L;
this.size = 9223372036854775807L;
this.current = 1L;
this.orders = new ArrayList();
this.optimizeCountSql = true;
this.isSearchCount = true;
this.hitCount = false;
if (current > 1L) {
this.current = current;
}
this.size = size;
this.total = total;
this.isSearchCount = isSearchCount;
}
@JsonView({LabelJsonViewBase.class})
@ApiModelProperty("是否存在上一页")
public boolean hasPrevious() {
return this.current > 1L;
}
@JsonView({LabelJsonViewBase.class})
@ApiModelProperty("是否存在下一页")
public boolean hasNext() {
return this.current < this.getPages();
}
public List<T> getRecords() {
return this.records;
}
public <A> Map<A, T> getRecords(String key) {
try {
Map<A, T> map = new HashMap();
Iterator var3 = this.records.iterator();
while(var3.hasNext()) {
T t = var3.next();
Object value = getValueByFieldName(t, key);
map.put(value, t);
}
return map;
} catch (Throwable var6) {
throw var6;
}
}
public <A, B> Map<A, B> getRecords(String key, Class<B> tClass) {
try {
Map<A, B> map = new HashMap();
Iterator var4 = this.records.iterator();
while(var4.hasNext()) {
T t = var4.next();
Object value = getValueByFieldName(t, key);
B b = JSONObject.parseObject(JSONObject.toJSONString(t), tClass);
map.put(value, b);
}
return map;
} catch (Throwable var8) {
throw var8;
}
}
private static Object getValueByFieldName(Object object, String fieldName) throws NoSuchFieldException, IllegalAccessException {
Class<?> clazz = object.getClass();
Field field = null;
while(clazz != null && field == null) {
try {
field = clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException var5) {
clazz = clazz.getSuperclass();
}
}
if (field == null) {
throw new NoSuchFieldException("无法找到指定的字段:" + fieldName);
} else {
field.setAccessible(true);
return field.get(object);
}
}
public MyPage<T> setRecords(List<T> records) {
this.records = records;
return this;
}
public long getTotal() {
return this.total;
}
public MyPage<T> setTotal(long total) {
this.total = total;
return this;
}
public long getSize() {
return this.size;
}
public MyPage<T> setSize(long size) {
this.size = size;
return this;
}
public long getCurrent() {
return this.current;
}
public MyPage<T> setCurrent(long current) {
this.current = current;
return this;
}
/** @deprecated */
@Nullable
@Deprecated
public String[] ascs() {
return CollectionUtils.isNotEmpty(this.orders) ? this.mapOrderToArray(OrderItem::isAsc) : null;
}
private String[] mapOrderToArray(Predicate<OrderItem> filter) {
List<String> columns = new ArrayList(this.orders.size());
this.orders.forEach((i) -> {
if (filter.test(i)) {
columns.add(i.getColumn());
}
});
return (String[])columns.toArray(new String[0]);
}
private void removeOrder(Predicate<OrderItem> filter) {
for(int i = this.orders.size() - 1; i >= 0; --i) {
if (filter.test(this.orders.get(i))) {
this.orders.remove(i);
}
}
}
public MyPage<T> addOrder(OrderItem... items) {
this.orders.addAll(Arrays.asList(items));
return this;
}
public MyPage<T> addOrder(List<OrderItem> items) {
this.orders.addAll(items);
return this;
}
/** @deprecated */
@Deprecated
public MyPage<T> setAscs(List<String> ascs) {
return CollectionUtils.isNotEmpty(ascs) ? this.setAsc((String[])ascs.toArray(new String[0])) : this;
}
/** @deprecated */
@Deprecated
public MyPage<T> setAsc(String... ascs) {
this.removeOrder(OrderItem::isAsc);
String[] var2 = ascs;
int var3 = ascs.length;
for(int var4 = 0; var4 < var3; ++var4) {
String s = var2[var4];
this.addOrder(OrderItem.asc(s));
}
return this;
}
/** @deprecated */
@Deprecated
public String[] descs() {
return this.mapOrderToArray((i) -> {
return !i.isAsc();
});
}
/** @deprecated */
@Deprecated
public MyPage<T> setDescs(List<String> descs) {
if (CollectionUtils.isNotEmpty(descs)) {
this.removeOrder((item) -> {
return !item.isAsc();
});
Iterator var2 = descs.iterator();
while(var2.hasNext()) {
String s = (String)var2.next();
this.addOrder(OrderItem.desc(s));
}
}
return this;
}
/** @deprecated */
@Deprecated
public MyPage<T> setDesc(String... descs) {
this.setDescs(Arrays.asList(descs));
return this;
}
public List<OrderItem> orders() {
return this.getOrders();
}
@ApiIgnore
public List<OrderItem> getOrders() {
return this.orders;
}
public void setOrders(List<OrderItem> orders) {
this.orders = orders;
}
public boolean optimizeCountSql() {
return this.optimizeCountSql;
}
@ApiModelProperty(
hidden = true
)
public boolean isSearchCount() {
return this.total < 0L ? false : this.isSearchCount;
}
public MyPage<T> setSearchCount(boolean isSearchCount) {
this.isSearchCount = isSearchCount;
return this;
}
public MyPage<T> setOptimizeCountSql(boolean optimizeCountSql) {
this.optimizeCountSql = optimizeCountSql;
return this;
}
public void hitCount(boolean hit) {
this.hitCount = hit;
}
public boolean isHitCount() {
return this.hitCount;
}
@ApiModelProperty(
hidden = true
)
public long getPages() {
if (this.getSize() == 0L) {
return 0L;
} else {
long pages = this.getTotal() / this.getSize();
if (this.getTotal() % this.getSize() != 0L) {
++pages;
}
return pages;
}
}
public <A> void clone(MyPage<A> target, Class targetClass) {
target.setRecords(JSON.parseArray(JSON.toJSONString(this.getRecords()), targetClass));
target.setTotal(this.getTotal());
target.setSize(this.getSize());
target.setCurrent(this.getCurrent());
target.setOrders(this.orders());
target.setOptimizeCountSql(this.optimizeCountSql());
target.setSearchCount(this.isSearchCount());
target.hitCount(this.isHitCount());
}
}
userService调用mapper
MyPage<User> myPage = new MyPage<User>(pageNum,pageSize);//也可加入sort等属性,具体用法阅读源码
MyPage<User> page = userMapper.getUserList(myPage,条件);
此时得到的page,里面已包含分页查询相关返回内容
注解的方式,跟xml区别不大
public interface UserMapper{
@select(select * from user where gender = #{param})
public MyPage<User> getUserList(
IPage pageQuery,
@Param("parma") param
);
}