Skip to content

Commit

Permalink
fix order by
Browse files Browse the repository at this point in the history
  • Loading branch information
yulichang committed Jan 6, 2023
1 parent 4158b5a commit fedd137
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ private ResultMapping buildResult(MappedStatement ms, MybatisLabel<?, ?> mybatis
columnName = getColumn(columnSet, columnName, 0);
label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), field, columnName, StringUtils.isNotBlank(index), index);
} else {
columnSet.add(columnName);
label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), field, StringUtils.isNotBlank(index), index);
}
columnList.add(label);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,18 @@ public String getSqlSelect() {
return i.getColumn();
}
String prefix;
if (i.isLabel()) {
if (i.isHasTableAlias()) {
prefix = i.getTableAlias();
if (i.isHasTableAlias()) {
prefix = i.getTableAlias();
} else {
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(), true);
prefix = tableList.getPrefix(i.getIndex(), i.getClazz(), false);
}
} else {
prefix = tableList.getPrefix(i.getIndex(), i.getClazz(), false);
}
String str = prefix + StringPool.DOT + i.getColumn();
if (i.isFunc()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ default Children selectAll(Class<?> clazz) {
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 片段
*/
Expand Down
7 changes: 1 addition & 6 deletions mybatis-plus-join-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-boot-starter</artifactId>
Expand All @@ -101,7 +96,7 @@
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public class TableADTO {
private String name;

private List<TableBDTO> bList;

private TableBDTO b;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public class TableBDTO {

private String name;

private List<TableCDTO> ccList;
private List<TableCDTO> cList;

private TableCDTO c;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class TableCDTO {
private String name;

private List<TableDDTO> dList;

private TableDDTO d;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class TableDDTO {
private String name;

private List<TableEDTO> eList;

private TableEDTO e;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ class CollectionTest {
*/
@Test
void testJoinCollection() {
testAA();
//4层嵌套 a对多b b对多c c对多d d对多e
MPJLambdaWrapper<TableA> wrapper1 = new MPJLambdaWrapper<TableA>()
.selectAll(TableA.class)
.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(TableE.class, TableDDTO::getEList, e -> e
.id(TableE::getId)))))
Expand All @@ -52,7 +53,7 @@ void testJoinCollection() {
MPJLambdaWrapper<TableA> wrapper = new MPJLambdaWrapper<TableA>()
.selectAll(TableA.class)
.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(TableE.class, TableDDTO::getEList))))
.leftJoin(TableB.class, TableB::getAid, TableA::getId)
Expand All @@ -61,6 +62,24 @@ void testJoinCollection() {
.leftJoin(TableE.class, TableE::getDid, TableD::getId);
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class UserDO {

private Integer addressId;

private Integer addressId2;

@TableLogic
private Boolean del;

Expand All @@ -61,4 +63,10 @@ public class UserDO {

@TableField(exist = false)
private List<UserDO> children;

@TableField(exist = false)
private List<AddressDO> addressList;

@TableField(exist = false)
private List<AddressDO> addressList2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.yulichang.test.join.service;

public interface UserService {

}
Original file line number Diff line number Diff line change
@@ -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 {

}
46 changes: 23 additions & 23 deletions mybatis-plus-join-test/test-join/src/main/resources/db/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北

DELETE FROM `user`;

INSERT INTO `user` (id, pid, `name`, `json`, `address_id`, 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),
( 2, 1, '张三 2', '{"id": 2,"name":"张三 2"}', 1, 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),
( 4, 1, '张三 4', '{"id": 4,"name":"张三 4"}', 1, 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),
( 6, 1, '张三 6', '{"id": 6,"name":"张三 6"}', 1, 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),
( 8, 1, '张三 8', '{"id": 8,"name":"张三 8"}', 1, 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),
(10, 1, '张三10', '{"id":10,"name":"张三10"}', 1, 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 ),
(12, 1, '张三12', '{"id":12,"name":"张三12"}', 1, 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 ),
(14, 2, '张三14', '{"id":14,"name":"张三14"}', 1, 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),
(16, 2, '张三16', '{"id":16,"name":"张三16"}', 1, 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),
(18, 2, '张三18', '{"id":18,"name":"张三18"}', 1, 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 ),
(20, 2, '张三20', '{"id":20,"name":"张三20"}', 1, 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 ),
(22, 2, '张三22', '{"id":22,"name":"张三22"}', 1, 0, 'https://url-22', '2022-01-01 12:00:00', 1, 2, true );
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, 2, 1, 'https://url-01', '2022-01-01 12:00:00', 1, 2, 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, 2, 0, 'https://url-03', '2022-01-01 12:00:00', 3, 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, 2, 0, 'https://url-05', '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, 2, 0, 'https://url-07', '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, 2, 0, 'https://url-09', '2022-01-01 12:00:00', 1, 2, false),
(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, 2, 0, 'https://url-11', '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, 2, 0, 'https://url-13', '2022-01-01 12:00:00', 1, 2, true ),
(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, 2, 0, 'https://url-15', '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, 2, 0, 'https://url-17', '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, 2, 0, 'https://url-19', '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, 2, 0, 'https://url-21', '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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ create table `user`
`name` varchar(255) not null,
`json` varchar(255) not null,
`address_id` int not null,
`address_id2` int not null,
sex tinyint not null,
head_img varchar(255) not null,
create_time datetime not null,
Expand Down Expand Up @@ -55,4 +56,4 @@ create table user_dto
update_by int not null,
version int not null,
del bit null
);
);
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void testJoin() {
list.forEach(System.out::println);
}


@Test
void testJoin1() {
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
Expand Down Expand Up @@ -96,6 +97,7 @@ void testWrapper() {


@Test
@SuppressWarnings("unchecked")
void testMSCache() {
// PageHelper.startPage(1, 10);
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
Expand Down Expand Up @@ -240,6 +242,33 @@ void testAlias() {
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
Expand Down

0 comments on commit fedd137

Please sign in to comment.