chain add map api

This commit is contained in:
yulichang 2024-09-14 11:23:18 +08:00
parent 110e0cf8a3
commit 24212523d7
2 changed files with 60 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.toolkit.SqlHelper;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
@ -124,4 +125,45 @@ public interface Chain<T> extends MPJBaseJoin<T> {
default <R, P extends IPage<R>> P page(P page, Class<R> resultType) {
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinPage(page, resultType, this));
}
/**
* 链式调用
* 构造方法必须传 class entity 否则会报错<br />
* new MPJLambdaWrapper(User.class)<br />
* JoinWrappers.lambda(User.class)<br />
*/
default Map<String, Object> mapOne() {
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinMap(this));
}
/**
* 链式调用
* 构造方法必须传 class entity 否则会报错<br />
* new MPJLambdaWrapper(User.class)<br />
* JoinWrappers.lambda(User.class)<br />
*/
default Map<String, Object> mapFirst() {
List<Map<String, Object>> mapList = mapPage(new Page<Map<String, Object>>(1, 1).setSearchCount(false)).getRecords();
return Optional.of(mapList).filter(CollectionUtils::isNotEmpty).map(m -> m.get(0)).orElse(null);
}
/**
* 链式调用
* 构造方法必须传 class entity 否则会报错<br />
* new MPJLambdaWrapper(User.class)<br />
* JoinWrappers.lambda(User.class)<br />
*/
default List<Map<String, Object>> mapList() {
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinMaps(this));
}
/**
* 链式调用
* 构造方法必须传 class entity 否则会报错<br />
* new MPJLambdaWrapper(User.class)<br />
* JoinWrappers.lambda(User.class)<br />
*/
default <P extends IPage<Map<String, Object>>> P mapPage(P page) {
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinMapsPage(page, this));
}
}

View File

@ -1,7 +1,9 @@
package com.github.yulichang.test.join.unit;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.test.join.entity.UserDO;
import com.github.yulichang.test.util.Reset;
import com.github.yulichang.test.util.ThreadLocalUtils;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.github.yulichang.wrapper.UpdateJoinWrapper;
@ -43,4 +45,20 @@ public class ChainFirstTest {
System.out.println(first);
}
@Test
void chainFirst2() {
ThreadLocalUtils.set("SELECT t.id, t.pid, t.`name`, t.`json`, t.sex, t.head_img, t.create_time, t.address_id, " +
"t.address_id2, t.del, t.create_by, t.update_by FROM `user` t WHERE t.del = false AND (t.id = ?)");
System.out.println(JoinWrappers.lambda(UserDO.class).eq(UserDO::getId,1).mapOne());
ThreadLocalUtils.set("SELECT t.id, t.pid, t.`name`, t.`json`, t.sex, t.head_img, t.create_time, t.address_id, " +
"t.address_id2, t.del, t.create_by, t.update_by FROM `user` t WHERE t.del = false LIMIT ?");
System.out.println(JoinWrappers.lambda(UserDO.class).mapFirst());
ThreadLocalUtils.set("SELECT t.id, t.pid, t.`name`, t.`json`, t.sex, t.head_img, t.create_time, t.address_id, " +
"t.address_id2, t.del, t.create_by, t.update_by FROM `user` t WHERE t.del = false");
System.out.println(JoinWrappers.lambda(UserDO.class).mapList());
ThreadLocalUtils.set("SELECT t.id, t.pid, t.`name`, t.`json`, t.sex, t.head_img, t.create_time, t.address_id, " +
"t.address_id2, t.del, t.create_by, t.update_by FROM `user` t WHERE t.del = false LIMIT ?");
System.out.println(JoinWrappers.lambda(UserDO.class).mapPage(new Page<>(1, 3)));
}
}