This commit is contained in:
admin 2021-01-28 10:40:32 +08:00
parent 2e22c6b28d
commit fd51216322
6 changed files with 58 additions and 6 deletions

View File

@ -1,10 +1,55 @@
# mybatis-plus-join
支持连表查询的[mybatis-plus](https://gitee.com/baomidou/mybatis-plus)
## 运行环境
* mysql8
* jdk8
* mysql8
* jdk8
* mybatis-plus 3.4.2
## 使用方法
* [参考测试类](https://gitee.com/best_handsome/mybatis-plus-join/blob/master/src/test/java/com/example/mp/MpJoinTest.java)
### 使用
* entity继承MyBaseEntity
* mapper继承MyBaseMapper
* service继承MyBaseService
* serviceImpl继承MyBaseServiceImpl
### MyLambdaQueryWrapper用法
#### select(UserEntity::getId) 查询指定的字段,支持可变参数
查询user表中的head_img,name和user_address表中的address,tel
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/select.png)
对应sql
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectSql.png)
#### selectAll(UserEntity.class) 查询UserEntity全部字段
查询user全部字段和user_address表中的address,tel
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectAll.png)
对应sql
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectAllSql.png)
#### as(UserEntity::getHeadImg,UserDTO::getUserHeadImg)
查询字段head_img as userHeadImg
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectAs.png)
对应sql
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectAsSql.png)
#### 左连接 leftJoin(UserEntity::getId,UserAddressEntity::getUserId,right -> right)
前连个参数是两个表的连接条件 -> user left join user_address on user.id = User_address.user_id
第三个参数是右表wrapper对象,可以继续使用,以上方法.
#### 条件查询eq()
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectEq.png)
对应sql
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectEqSql.png)
#### [参考测试类](https://gitee.com/best_handsome/mybatis-plus-join/blob/master/src/test/java/com/example/mp/MpJoinTest.java)

BIN
doc/selectAs.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
doc/selectAsSql.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

View File

@ -12,6 +12,8 @@ public class UserDTO {
private String sex;
/** user */
private String headImg;
/** user */
private String userHeadImg;// headImg 别名测试
/** user_address */
private String tel;
/** user_address */

View File

@ -111,9 +111,9 @@ public class MyJoinLambdaQueryWrapper<T extends MyBaseEntity> extends MyAbstract
setEntityClass(clazz);
TableInfo info = TableInfoHelper.getTableInfo(getEntityClass());
info.getFieldList().forEach(s ->
selectColumnList.add(new SelectColumn(this.rUid, s.getColumn(), s.getColumn()/*s.getProperty()*/, null)));
selectColumnList.add(new SelectColumn(this.rUid, s.getColumn(), null, null)));
if (StringUtils.isNotBlank(info.getKeyColumn())) {
selectColumnList.add(new SelectColumn(this.rUid, info.getKeyColumn(),info.getKeyColumn()/* info.getKeyProperty()*/, null));
selectColumnList.add(new SelectColumn(this.rUid, info.getKeyColumn(), null, null));
}
return typedThis;
}

View File

@ -119,9 +119,14 @@ class MpJoinTest {
* <p>
* 自己去探索发现吧!
*/
@SuppressWarnings("all")
@Test
void test4() {
//todo
userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
.as(UserEntity::getHeadImg,UserDTO::getUserHeadImg)
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId,
right -> right.select(UserAddressEntity::getAddress, UserAddressEntity::getTel))
, UserDTO.class);
}