This commit is contained in:
yulichang 2023-11-15 20:31:27 +08:00
parent 5251af216e
commit 5a7d561367
2 changed files with 84 additions and 3 deletions

View File

@ -62,18 +62,18 @@ class LambdaWrapperTest {
}
@Test
void testSelectSort(){
void testSelectSort() {
ThreadLocalUtils.set("SELECT t.id, t.user_id, t.tenant_id FROM user_tenant t WHERE t.tenant_id = 1");
MPJLambdaWrapper<UserTenantDO> lambda = JoinWrappers.lambda(UserTenantDO.class);
lambda.selectAsClass(UserTenantDO.class, UserTenantDTO.class);
List<UserTenantDO> list = userTenantMapper.selectJoinList(UserTenantDO.class,lambda);
List<UserTenantDO> list = userTenantMapper.selectJoinList(UserTenantDO.class, lambda);
assert list.size() == 5 && list.get(0).getIdea() != null;
ThreadLocalUtils.set("SELECT t.tenant_id, t.user_id, t.id FROM user_tenant t WHERE t.tenant_id = 1");
MPJLambdaWrapper<UserTenantDO> lambda1 = JoinWrappers.lambda(UserTenantDO.class);
lambda1.selectAsClass(UserTenantDO.class, UserTenantDescDTO.class);
List<UserTenantDO> list1 = userTenantMapper.selectJoinList(UserTenantDO.class,lambda1);
List<UserTenantDO> list1 = userTenantMapper.selectJoinList(UserTenantDO.class, lambda1);
assert list1.size() == 5 && list1.get(0).getIdea() != null;
}
@ -1134,4 +1134,22 @@ class LambdaWrapperTest {
assert list.size() == 7;
}
@Test
void unionAll() {
ThreadLocalUtils.set("SELECT t.id FROM `user` t WHERE t.del = false AND (t.id = ?) UNION ALL SELECT t.id FROM address t UNION ALL SELECT t.id FROM area t WHERE t.del = false AND (t.id = ?)");
MPJLambdaWrapper<UserDO> wrapper = JoinWrappers.lambda(UserDO.class)
.select(UserDO::getId)
.eq(UserDO::getId, 1);
MPJLambdaWrapper<AddressDO> wrapper1 = JoinWrappers.lambda(AddressDO.class)
.select(AddressDO::getId)
.disableLogicDel();
MPJLambdaWrapper<AreaDO> wrapper2 = JoinWrappers.lambda(AreaDO.class)
.select(AreaDO::getId)
.eq(AreaDO::getId, 2);
wrapper.unionAll(wrapper1, wrapper2);
List<UserDO> list = wrapper.list();
assert list.size() == 23 && list.get(0).getId() != null;
}
}

View File

@ -0,0 +1,63 @@
package com.github.yulichang.test.join.m;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.test.join.entity.UserDO;
import com.github.yulichang.test.join.mapper.UserMapper;
import com.github.yulichang.test.util.Reset;
import com.github.yulichang.test.util.ThreadLocalUtils;
import com.github.yulichang.toolkit.Ref;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import java.util.Objects;
@SpringBootTest
public class CustomWrapperTest {
@Autowired
private UserMapper userMapper;
@BeforeEach
void setUp() {
Reset.reset();
}
//自定义wrapper扩展
public static class CWrapper<T> extends MPJLambdaWrapper<T> {
public static <T> CWrapper<T> toCWrapper() {
return null;
}
public <X> CWrapper<T> eqIfAbsent(SFunction<X, ?> column, Object val) {
super.eq(Objects.nonNull(val), column, val);
return this;
}
}
@Test
void testWrapperCustomer() {
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 = ?)");
CWrapper<UserDO> wrapper = new CWrapper<UserDO>()
.selectAll(UserDO.class)
// .toChildren(new Ref<CWrapper<UserDO>>())
.toChildren(CWrapper::toCWrapper)
.eqIfAbsent(UserDO::getId, 1);
List<UserDO> dos = userMapper.selectList(wrapper);
dos.forEach(System.out::println);
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");
CWrapper<UserDO> wrapper1 = new CWrapper<UserDO>()
.selectAll(UserDO.class)
.toChildren(new Ref<CWrapper<UserDO>>())
// .toChildren(CWrapper::toCWrapper)
.eqIfAbsent(UserDO::getId, null);
List<UserDO> dos1 = userMapper.selectList(wrapper1);
dos1.forEach(System.out::println);
}
}