fix order by

This commit is contained in:
yulichang 2023-01-06 19:45:46 +08:00
parent 4158b5a3e0
commit fedd137dc2
15 changed files with 129 additions and 41 deletions

View File

@ -240,6 +240,7 @@ public class MPJInterceptor implements Interceptor {
columnName = getColumn(columnSet, columnName, 0); columnName = getColumn(columnSet, columnName, 0);
label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), field, columnName, StringUtils.isNotBlank(index), index); label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), field, columnName, StringUtils.isNotBlank(index), index);
} else { } else {
columnSet.add(columnName);
label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), field, StringUtils.isNotBlank(index), index); label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), field, StringUtils.isNotBlank(index), index);
} }
columnList.add(label); columnList.add(label);

View File

@ -191,14 +191,18 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
return i.getColumn(); return i.getColumn();
} }
String prefix; String prefix;
if (i.isLabel()) { if (i.isHasTableAlias()) {
if (i.isHasTableAlias()) { prefix = i.getTableAlias();
prefix = i.getTableAlias();
} else {
prefix = tableList.getPrefix(i.getIndex(), i.getClazz(), true);
}
} else { } else {
prefix = tableList.getPrefix(i.getIndex(), i.getClazz(), false); if (i.isLabel()) {
if (i.isHasTableAlias()) {
prefix = i.getTableAlias();
} else {
prefix = tableList.getPrefix(i.getIndex(), i.getClazz(), true);
}
} else {
prefix = tableList.getPrefix(i.getIndex(), i.getClazz(), false);
}
} }
String str = prefix + StringPool.DOT + i.getColumn(); String str = prefix + StringPool.DOT + i.getColumn();
if (i.isFunc()) { if (i.isFunc()) {

View File

@ -148,6 +148,15 @@ public interface Query<Children> extends Serializable {
return getChildren(); return getChildren();
} }
/**
* 查询实体类全部字段
*/
default Children selectAll(Class<?> clazz, String prefix) {
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().map(i ->
new SelectNormal(i, getIndex(), true, prefix)).collect(Collectors.toList()));
return getChildren();
}
/** /**
* select sql 片段 * select sql 片段
*/ */

View File

@ -77,11 +77,6 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.24</version> <version>1.18.24</version>
</dependency> </dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency> <dependency>
<groupId>com.github.yulichang</groupId> <groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-boot-starter</artifactId> <artifactId>mybatis-plus-join-boot-starter</artifactId>
@ -101,7 +96,7 @@
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version> <version>3.5.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>

View File

@ -14,4 +14,6 @@ public class TableADTO {
private String name; private String name;
private List<TableBDTO> bList; private List<TableBDTO> bList;
private TableBDTO b;
} }

View File

@ -15,5 +15,7 @@ public class TableBDTO {
private String name; private String name;
private List<TableCDTO> ccList; private List<TableCDTO> cList;
private TableCDTO c;
} }

View File

@ -16,4 +16,6 @@ public class TableCDTO {
private String name; private String name;
private List<TableDDTO> dList; private List<TableDDTO> dList;
private TableDDTO d;
} }

View File

@ -16,4 +16,6 @@ public class TableDDTO {
private String name; private String name;
private List<TableEDTO> eList; private List<TableEDTO> eList;
private TableEDTO e;
} }

View File

@ -35,11 +35,12 @@ class CollectionTest {
*/ */
@Test @Test
void testJoinCollection() { void testJoinCollection() {
testAA();
//4层嵌套 a对多b b对多c c对多d d对多e //4层嵌套 a对多b b对多c c对多d d对多e
MPJLambdaWrapper<TableA> wrapper1 = new MPJLambdaWrapper<TableA>() MPJLambdaWrapper<TableA> wrapper1 = new MPJLambdaWrapper<TableA>()
.selectAll(TableA.class) .selectAll(TableA.class)
.selectCollection(TableB.class, TableADTO::getBList, b -> b .selectCollection(TableB.class, TableADTO::getBList, b -> b
.collection(TableC.class, TableBDTO::getCcList, c -> c .collection(TableC.class, TableBDTO::getCList, c -> c
.collection(TableD.class, TableCDTO::getDList, d -> d .collection(TableD.class, TableCDTO::getDList, d -> d
.collection(TableE.class, TableDDTO::getEList, e -> e .collection(TableE.class, TableDDTO::getEList, e -> e
.id(TableE::getId))))) .id(TableE::getId)))))
@ -52,7 +53,7 @@ class CollectionTest {
MPJLambdaWrapper<TableA> wrapper = new MPJLambdaWrapper<TableA>() MPJLambdaWrapper<TableA> wrapper = new MPJLambdaWrapper<TableA>()
.selectAll(TableA.class) .selectAll(TableA.class)
.selectCollection(TableB.class, TableADTO::getBList, b -> b .selectCollection(TableB.class, TableADTO::getBList, b -> b
.collection(TableC.class, TableBDTO::getCcList, c -> c .collection(TableC.class, TableBDTO::getCList, c -> c
.collection(TableD.class, TableCDTO::getDList, d -> d .collection(TableD.class, TableCDTO::getDList, d -> d
.collection(TableE.class, TableDDTO::getEList)))) .collection(TableE.class, TableDDTO::getEList))))
.leftJoin(TableB.class, TableB::getAid, TableA::getId) .leftJoin(TableB.class, TableB::getAid, TableA::getId)
@ -61,6 +62,24 @@ class CollectionTest {
.leftJoin(TableE.class, TableE::getDid, TableD::getId); .leftJoin(TableE.class, TableE::getDid, TableD::getId);
List<TableADTO> dtos = tableAMapper.selectJoinList(TableADTO.class, wrapper); List<TableADTO> dtos = tableAMapper.selectJoinList(TableADTO.class, wrapper);
assert dtos.get(0).getBList().get(0).getCcList().get(0).getDList().get(0).getEList().get(0).getName() != null; assert dtos.get(0).getBList().get(0).getCList().get(0).getDList().get(0).getEList().get(0).getName() != null;
}
@Test
void testAA() {
MPJLambdaWrapper<TableA> wrapper1 = new MPJLambdaWrapper<TableA>()
.selectAll(TableA.class)
.selectAssociation(TableB.class, TableADTO::getB, b -> b
.association(TableC.class, TableBDTO::getC, c -> c
.association(TableD.class, TableCDTO::getD, d -> d
.association(TableE.class, TableDDTO::getE, e -> e
.id(TableE::getId)))))
.leftJoin(TableB.class, TableB::getAid, TableA::getId)
.leftJoin(TableC.class, TableC::getBid, TableB::getId)
.leftJoin(TableD.class, TableD::getCid, TableC::getId)
.leftJoin(TableE.class, TableE::getDid, TableD::getId)
.last("LIMIT 1");
List<TableADTO> dtos1 = tableAMapper.selectJoinList(TableADTO.class, wrapper1);
System.out.println(1);
} }
} }

View File

@ -42,6 +42,8 @@ public class UserDO {
private Integer addressId; private Integer addressId;
private Integer addressId2;
@TableLogic @TableLogic
private Boolean del; private Boolean del;
@ -61,4 +63,10 @@ public class UserDO {
@TableField(exist = false) @TableField(exist = false)
private List<UserDO> children; private List<UserDO> children;
@TableField(exist = false)
private List<AddressDO> addressList;
@TableField(exist = false)
private List<AddressDO> addressList2;
} }

View File

@ -0,0 +1,5 @@
package com.github.yulichang.test.join.service;
public interface UserService {
}

View File

@ -0,0 +1,9 @@
package com.github.yulichang.test.join.service.impl;
import com.github.yulichang.test.join.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
}

View File

@ -29,29 +29,29 @@ INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北
DELETE FROM `user`; DELETE FROM `user`;
INSERT INTO `user` (id, pid, `name`, `json`, `address_id`, sex, head_img, create_time, create_by, update_by, del) VALUES INSERT INTO `user` (id, pid, `name`, `json`, `address_id`, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES
( 1, 1, '张三 1', '{"id": 1,"name":"张三 1"}', 1, 1, 'https://url-01', '2022-01-01 12:00:00', 1, 2, false), ( 1, 1, '张三 1', '{"id": 1,"name":"张三 1"}', 1, 2, 1, 'https://url-01', '2022-01-01 12:00:00', 1, 2, false),
( 2, 1, '张三 2', '{"id": 2,"name":"张三 2"}', 1, 0, 'https://url-02', '2022-01-01 12:00:00', 2, 3, false), ( 2, 1, '张三 2', '{"id": 2,"name":"张三 2"}', 1, 2, 0, 'https://url-02', '2022-01-01 12:00:00', 2, 3, false),
( 3, 1, '张三 3', '{"id": 3,"name":"张三 3"}', 1, 0, 'https://url-03', '2022-01-01 12:00:00', 3, 2, false), ( 3, 1, '张三 3', '{"id": 3,"name":"张三 3"}', 1, 2, 0, 'https://url-03', '2022-01-01 12:00:00', 3, 2, false),
( 4, 1, '张三 4', '{"id": 4,"name":"张三 4"}', 1, 0, 'https://url-04', '2022-01-01 12:00:00', 9, 2, false), ( 4, 1, '张三 4', '{"id": 4,"name":"张三 4"}', 1, 2, 0, 'https://url-04', '2022-01-01 12:00:00', 9, 2, false),
( 5, 1, '张三 5', '{"id": 5,"name":"张三 5"}', 1, 0, 'https://url-05', '2022-01-01 12:00:00', 1, 2, false), ( 5, 1, '张三 5', '{"id": 5,"name":"张三 5"}', 1, 2, 0, 'https://url-05', '2022-01-01 12:00:00', 1, 2, false),
( 6, 1, '张三 6', '{"id": 6,"name":"张三 6"}', 1, 0, 'https://url-06', '2022-01-01 12:00:00', 1, 2, false), ( 6, 1, '张三 6', '{"id": 6,"name":"张三 6"}', 1, 2, 0, 'https://url-06', '2022-01-01 12:00:00', 1, 2, false),
( 7, 1, '张三 7', '{"id": 7,"name":"张三 7"}', 1, 0, 'https://url-07', '2022-01-01 12:00:00', 1, 2, false), ( 7, 1, '张三 7', '{"id": 7,"name":"张三 7"}', 1, 2, 0, 'https://url-07', '2022-01-01 12:00:00', 1, 2, false),
( 8, 1, '张三 8', '{"id": 8,"name":"张三 8"}', 1, 0, 'https://url-08', '2022-01-01 12:00:00', 1, 2, false), ( 8, 1, '张三 8', '{"id": 8,"name":"张三 8"}', 1, 2, 0, 'https://url-08', '2022-01-01 12:00:00', 1, 2, false),
( 9, 1, '张三 9', '{"id": 9,"name":"张三 9"}', 1, 0, 'https://url-09', '2022-01-01 12:00:00', 1, 2, false), ( 9, 1, '张三 9', '{"id": 9,"name":"张三 9"}', 1, 2, 0, 'https://url-09', '2022-01-01 12:00:00', 1, 2, false),
(10, 1, '张三10', '{"id":10,"name":"张三10"}', 1, 0, 'https://url-10', '2022-01-01 12:00:00', 1, 2, true ), (10, 1, '张三10', '{"id":10,"name":"张三10"}', 1, 2, 0, 'https://url-10', '2022-01-01 12:00:00', 1, 2, true ),
(11, 1, '张三11', '{"id":11,"name":"张三11"}', 1, 0, 'https://url-11', '2022-01-01 12:00:00', 1, 2, true ), (11, 1, '张三11', '{"id":11,"name":"张三11"}', 1, 2, 0, 'https://url-11', '2022-01-01 12:00:00', 1, 2, true ),
(12, 1, '张三12', '{"id":12,"name":"张三12"}', 1, 0, 'https://url-12', '2022-01-01 12:00:00', 1, 2, true ), (12, 1, '张三12', '{"id":12,"name":"张三12"}', 1, 2, 0, 'https://url-12', '2022-01-01 12:00:00', 1, 2, true ),
(13, 1, '张三13', '{"id":13,"name":"张三13"}', 1, 0, 'https://url-13', '2022-01-01 12:00:00', 1, 2, true ), (13, 1, '张三13', '{"id":13,"name":"张三13"}', 1, 2, 0, 'https://url-13', '2022-01-01 12:00:00', 1, 2, true ),
(14, 2, '张三14', '{"id":14,"name":"张三14"}', 1, 0, 'https://url-14', '2022-01-01 12:00:00', 1, 2, false), (14, 2, '张三14', '{"id":14,"name":"张三14"}', 1, 2, 0, 'https://url-14', '2022-01-01 12:00:00', 1, 2, false),
(15, 2, '张三15', '{"id":15,"name":"张三15"}', 1, 0, 'https://url-15', '2022-01-01 12:00:00', 1, 2, false), (15, 2, '张三15', '{"id":15,"name":"张三15"}', 1, 2, 0, 'https://url-15', '2022-01-01 12:00:00', 1, 2, false),
(16, 2, '张三16', '{"id":16,"name":"张三16"}', 1, 0, 'https://url-16', '2022-01-01 12:00:00', 1, 2, false), (16, 2, '张三16', '{"id":16,"name":"张三16"}', 1, 2, 0, 'https://url-16', '2022-01-01 12:00:00', 1, 2, false),
(17, 2, '张三17', '{"id":17,"name":"张三17"}', 1, 0, 'https://url-17', '2022-01-01 12:00:00', 1, 2, false), (17, 2, '张三17', '{"id":17,"name":"张三17"}', 1, 2, 0, 'https://url-17', '2022-01-01 12:00:00', 1, 2, false),
(18, 2, '张三18', '{"id":18,"name":"张三18"}', 1, 0, 'https://url-18', '2022-01-01 12:00:00', 1, 2, false), (18, 2, '张三18', '{"id":18,"name":"张三18"}', 1, 2, 0, 'https://url-18', '2022-01-01 12:00:00', 1, 2, false),
(19, 2, '张三19', '{"id":19,"name":"张三19"}', 1, 0, 'https://url-19', '2022-01-01 12:00:00', 1, 2, true ), (19, 2, '张三19', '{"id":19,"name":"张三19"}', 1, 2, 0, 'https://url-19', '2022-01-01 12:00:00', 1, 2, true ),
(20, 2, '张三20', '{"id":20,"name":"张三20"}', 1, 0, 'https://url-20', '2022-01-01 12:00:00', 1, 2, true ), (20, 2, '张三20', '{"id":20,"name":"张三20"}', 1, 2, 0, 'https://url-20', '2022-01-01 12:00:00', 1, 2, true ),
(21, 2, '张三21', '{"id":21,"name":"张三21"}', 1, 0, 'https://url-21', '2022-01-01 12:00:00', 1, 2, true ), (21, 2, '张三21', '{"id":21,"name":"张三21"}', 1, 2, 0, 'https://url-21', '2022-01-01 12:00:00', 1, 2, true ),
(22, 2, '张三22', '{"id":22,"name":"张三22"}', 1, 0, 'https://url-22', '2022-01-01 12:00:00', 1, 2, true ); (22, 2, '张三22', '{"id":22,"name":"张三22"}', 1, 2, 0, 'https://url-22', '2022-01-01 12:00:00', 1, 2, true );
DELETE FROM address; DELETE FROM address;

View File

@ -25,6 +25,7 @@ create table `user`
`name` varchar(255) not null, `name` varchar(255) not null,
`json` varchar(255) not null, `json` varchar(255) not null,
`address_id` int not null, `address_id` int not null,
`address_id2` int not null,
sex tinyint not null, sex tinyint not null,
head_img varchar(255) not null, head_img varchar(255) not null,
create_time datetime not null, create_time datetime not null,
@ -55,4 +56,4 @@ create table user_dto
update_by int not null, update_by int not null,
version int not null, version int not null,
del bit null del bit null
); );

View File

@ -53,6 +53,7 @@ class LambdaWrapperTest {
list.forEach(System.out::println); list.forEach(System.out::println);
} }
@Test @Test
void testJoin1() { void testJoin1() {
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>() MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
@ -96,6 +97,7 @@ class LambdaWrapperTest {
@Test @Test
@SuppressWarnings("unchecked")
void testMSCache() { void testMSCache() {
// PageHelper.startPage(1, 10); // PageHelper.startPage(1, 10);
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>() MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
@ -240,6 +242,33 @@ class LambdaWrapperTest {
System.out.println(list); System.out.println(list);
} }
@Test
void testTableAlias() {
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
// .disableLogicDel()//关闭主表逻辑删除
.selectAll(UserDO.class)
.selectAll(AddressDO.class, "aa")
// .selectCollection(UserDO.class, UserDO::getChildren)
.leftJoin(AddressDO.class, "aa", AddressDO::getUserId, UserDO::getId);
List<UserDO> list = userMapper.selectJoinList(UserDO.class, wrapper);
System.out.println(list);
}
@Test
void testLabel() {
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
.disableSubLogicDel()
.selectAll(UserDO.class)
.selectCollection("t1", AddressDO.class, UserDO::getAddressList)
.selectCollection("t2", AddressDO.class, UserDO::getAddressList2)
.leftJoin(AddressDO.class, AddressDO::getId, UserDO::getAddressId)
.leftJoin(AddressDO.class, AddressDO::getId, UserDO::getAddressId2);
List<UserDO> list = userMapper.selectJoinList(UserDO.class, wrapper);
System.out.println(list);
}
/** /**
* 简单的分页关联查询 lambda * 简单的分页关联查询 lambda