This commit is contained in:
admin 2021-01-28 11:19:26 +08:00
parent 1f29d3c16d
commit e0aa9c3c0f
10 changed files with 124 additions and 18 deletions

108
README.md
View File

@ -24,26 +24,80 @@
#### select(UserEntity::getId) 查询指定的字段,支持可变参数 #### select(UserEntity::getId) 查询指定的字段,支持可变参数
查询user表中的head_img,name和user_address表中的address,tel 查询user表中的head_img,name和user_address表中的address,tel
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/select.png)
对应sql ```java
class test {
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectSql.png) void testJoin() {
List<UserDTO> list = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
.select(UserEntity::getHeadImg, UserEntity::getHeadImg)
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId,
right -> right.select(UserAddressEntity::getAddress, UserAddressEntity::getTel))
, UserDTO.class);
}
}
```
对应sql
```sql
SELECT t0.head_img, t0.head_img, t1.address, t1.tel
FROM user t0
LEFT JOIN user_address t1 ON t0.id = t1.user_id
```
#### selectAll(UserEntity.class) 查询UserEntity全部字段 #### selectAll(UserEntity.class) 查询UserEntity全部字段
查询user全部字段和user_address表中的address,tel 查询user全部字段和user_address表中的address,tel
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectAll.png)
对应sql ```java
class test {
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectAllSql.png) @Resource
private UserMapper userMapper;
void testJoin() {
List<UserDTO> list = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
.selectAll(UserEntity.class)
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId,
right -> right.select(UserAddressEntity::getAddress, UserAddressEntity::getTel))
, UserDTO.class);
}
}
```
对应sql
```sql
SELECT t0.name, t0.sex, t0.head_img, t0.id, t1.address, t1.tel
FROM user t0
LEFT JOIN user_address t1 ON t0.id = t1.user_id
```
#### as(UserEntity::getHeadImg,UserDTO::getUserHeadImg) #### as(UserEntity::getHeadImg,UserDTO::getUserHeadImg)
查询字段head_img as userHeadImg 查询字段head_img as userHeadImg
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectAs.png)
对应sql ```java
class test {
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectAsSql.png) @Resource
private UserMapper userMapper;
void testJoin() {
List<UserDTO> list = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
.as(UserEntity::getHeadImg, UserDTO::getUserHeadImg)
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId,
right -> right.select(UserAddressEntity::getAddress, UserAddressEntity::getTel))
, UserDTO.class);
}
}
```
对应sql
```sql
SELECT t0.head_img AS userHeadImg, t1.address, t1.tel
FROM user t0
LEFT JOIN user_address t1 ON t0.id = t1.user_id
```
#### 左连接 leftJoin(UserEntity::getId,UserAddressEntity::getUserId,right -> right) #### 左连接 leftJoin(UserEntity::getId,UserAddressEntity::getUserId,right -> right)
@ -53,10 +107,32 @@ user left join user_address on user.id = User_address.user_id
#### 条件查询eq() #### 条件查询eq()
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectEq.png) ```java
对应sql class test {
@Resource
![image](https://gitee.com/best_handsome/mybatis-plus-join/raw/master/doc/selectEqSql.png) private UserMapper userMapper;
void testJoin() {
List<UserDTO> list = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
.selectAll(UserEntity.class)
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId,
right -> right.select(UserAddressEntity::getAddress, UserAddressEntity::getTel))
.eq(true, UserEntity::getId, 1)
.like(UserAddressEntity::getTel, "1")
.eq(UserEntity::getId, UserAddressEntity::getUserId)
, UserDTO.class);
}
}
```
对应sql
```sql
SELECT t0.name, t0.sex, t0.head_img, t0.id, t1.address, t1.tel
FROM user t0
LEFT JOIN user_address t1 ON t0.id = t1.user_id
WHERE (t0.id = ? AND t1.tel LIKE ? AND t0.id = t1.user_id)
```
#### [参考测试类](https://gitee.com/best_handsome/mybatis-plus-join/blob/master/src/test/java/com/example/mp/MpJoinTest.java) #### [参考测试类](https://gitee.com/best_handsome/mybatis-plus-join/blob/master/src/test/java/com/example/mp/MpJoinTest.java)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

View File

@ -113,14 +113,44 @@ class MpJoinTest {
@Test @Test
void test4() { void select() {
userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>() List<UserDTO> list = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
.select(UserEntity::getHeadImg, UserEntity::getHeadImg)
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId,
right -> right.select(UserAddressEntity::getAddress, UserAddressEntity::getTel))
, UserDTO.class);
}
@Test
void selectAll() {
List<UserDTO> list = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
.selectAll(UserEntity.class)
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId,
right -> right.select(UserAddressEntity::getAddress, UserAddressEntity::getTel))
, UserDTO.class);
}
@Test
void selectAs() {
List<UserDTO> list = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
.as(UserEntity::getHeadImg, UserDTO::getUserHeadImg) .as(UserEntity::getHeadImg, UserDTO::getUserHeadImg)
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId, .leftJoin(UserEntity::getId, UserAddressEntity::getUserId,
right -> right.select(UserAddressEntity::getAddress, UserAddressEntity::getTel)) right -> right.select(UserAddressEntity::getAddress, UserAddressEntity::getTel))
, UserDTO.class); , UserDTO.class);
} }
@Test
void selectEq() {
List<UserDTO> list = userMapper.selectJoinList(new MyLambdaQueryWrapper<UserEntity>()
.selectAll(UserEntity.class)
.leftJoin(UserEntity::getId, UserAddressEntity::getUserId,
right -> right.select(UserAddressEntity::getAddress, UserAddressEntity::getTel))
.eq(true, UserEntity::getId, 1)
.like(UserAddressEntity::getTel, "1")
.eq(UserEntity::getId, UserAddressEntity::getUserId)
, UserDTO.class);
}
private void log(String format, String... args) { private void log(String format, String... args) {
System.err.println(String.format(format, args)); System.err.println(String.format(format, args));