update = JoinWrappers.update(User.class)
.setUpdateEntity(address, area) // Set fields for sub-tables
// .setUpdateEntityAndNull(address, area) // Use this to update null and non-null fields
.leftJoin(Address.class, Address::getUserId, User::getId)
.leftJoin(Area.class, Area::getId, Address::getAreaId)
.eq(User::getId, 1); // Set WHERE condition
int affectedRows = userMapper.updateJoin(user, update);
```
### SQL Equivalent
```sql
UPDATE `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) LEFT JOIN area t2 ON (t2.id = t1.area_id) SET t.`name`=?, t.update_by=?, t1.tel=?, t1.address=?, t2.province=?, t2.city=? WHERE (t.id = ?)
```
### Note
By default, only non-null fields in the entities are updated. Use `setUpdateEntityAndNull` to update all fields (including nulls).
```
--------------------------------
### 调用映射查询方法
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/core/mapping/intro.md
通过 Service 层的方法(如 getByIdDeep, listDeep, pageDeep)执行映射查询。注意:关系映射通过多次单表查询实现,默认只查询两层。
```java
/**
* 一对一,一对多关系映射查询
* 以Deep结尾的方法会进行映射查询
* 如果不需要关系映射就使用mybatis plus原生方法即可,比如 getById listByIds 等
*
* 注意:关系映射不会去关联查询,而是执行多次单表查询(对结果汇总后使用in语句查询,再对结果进行匹配)
*/
@SpringBootTest
class MappingTest {
@Resource
private UserService userService;
@Test
void test1() {
User uesr = userService.getByIdDeep(2);
System.out.println(deep);
}
@Test
void test2() {
List list = userService.listDeep(Wrappers.emptyWrapper());
list.forEach(System.out::println);
}
@Test
void test3() {
Page page = new Page<>(2, 2);
Page result = userService.pageDeep(page, Wrappers.emptyWrapper());
result.getRecords().forEach(System.out::println);
}
}
```
--------------------------------
### Corresponding SQL for Aliasing
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/core/lambda/select/selectAs.md
This shows the resulting SQL when using the aliasing methods.
```sql
t.name AS nickname
```
--------------------------------
### Creating MyBatis-Plus Wrappers
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/core/other/condition-wrapper.md
Use the Wrappers factory class to quickly instantiate QueryWrapper, LambdaQueryWrapper, UpdateWrapper, and LambdaUpdateWrapper. This reduces boilerplate code.
```java
QueryWrapper queryWrapper = Wrappers.query();
queryWrapper.eq("name", "张三");
```
```java
LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(User::getName, "张三");
```
```java
UpdateWrapper updateWrapper = Wrappers.update();
updateWrapper.set("name", "李四");
```
```java
LambdaUpdateWrapper lambdaUpdateWrapper = Wrappers.lambdaUpdate();
lambdaUpdateWrapper.set(User::getName, "李四");
```
--------------------------------
### notLikeRight - QueryWrapper and LambdaQueryWrapper
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/core/other/condition-wrapper.md
Use notLikeRight to exclude records that do not start with the specified value (non-left fuzzy match). This method is applicable to string fields and supports both QueryWrapper and LambdaQueryWrapper.
```java
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.notLikeRight("name", "王");
```
```java
LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.notLikeRight(User::getName, "王");
```
--------------------------------
### likeLeft - QueryWrapper and LambdaQueryWrapper
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/core/other/condition-wrapper.md
Use likeLeft for right fuzzy matching, excluding records that do not start with the specified value. This method is suitable for string fields and supports both QueryWrapper and LambdaQueryWrapper.
```java
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.likeLeft("name", "王");
```
```java
LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.likeLeft(User::getName, "王");
```
```sql
SELECT * FROM user WHERE name LIKE '%王'
```
--------------------------------
### Call Custom Extension Methods
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/core/other/wrapper-ext.md
After implementing the `Ext` interface and excluding the default extension package, you can directly call your custom methods like `eqIfPresent` on `JoinWrappers`.
```java
JoinWrappers.lambda(User.class)
.selectAll()
.eqIfPresent(User::getId, 1)
.eq(User::getId, 1);
```
--------------------------------
### SQL - Corresponding Query
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/core/api/selectJoinPage.md
The SQL query generated by the `selectJoinPage` method with the provided wrapper configuration. This shows the structure of the join and the WHERE clause.
```sql
SELECT t.id,
t.name,
t.sex,
t.head_img,
t1.address
FROM user t
LEFT JOIN address t1 ON t1.user_id = t.id
WHERE (t.id = ?) LIMIT ?
```
--------------------------------
### Manual SET Clauses with updateJoin
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/core/api/updateJoin.md
Demonstrates how to manually set fields for both the primary and joined tables using the updateJoin method. The 'entity' parameter can be null when all SET clauses are defined in the wrapper.
```java
class MpJoinTest {
@Resource
private UserMapper userMapper;
/**
* 手动set条件
* 更新 user表name字段和address表address字段
*/
@Test
void update() {
UpdateJoinWrapper update = JoinWrappers.update(User.class)
.set(User::getName, "aaaaaa")
.set(Address::getAddress, "bbbbb")
.leftJoin(Address.class, Address::getUserId, User::getId)
.eq(User::getId, 1);
int i = userMapper.updateJoin(null, update);
}
```
--------------------------------
### Wrapper Join Delete (All Tables)
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/core/api/deleteJoin.md
Demonstrates deleting records from all joined tables, including the main table. The deleteAll() method is used to specify this behavior. Alternatively, specific tables can be targeted using the delete() method.
```java
class MpJoinTest {
@Resource
private UserMapper userMapper;
/**
* wrapper连表删除(删除全部表)
*/
@Test
void update() {
DeleteJoinWrapper wrapper = JoinWrappers.delete(User.class)
//删除全部的表数据 (主表和副表)
.deleteAll()
//也可以删除指定的表数据,调用 delete() 传要删除的实体类class 如下
//.delete(User.class, Address.class, Area.class)
.leftJoin(Address.class, Address::getUserId, User::getId)
.leftJoin(Area.class, Area::getId, Address::getAreaId)
.eq(User::getId, 1);
int i = userMapper.deleteJoin(wrapper);
}
}
```
```log
==> Preparing: DELETE t,t1,t2 FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) LEFT JOIN area t2 ON (t2.id = t1.area_id) WHERE (t.id = ?)
==> Parameters: 1(Integer)
<== Updates: 19
```
--------------------------------
### Implement JoinCrudRepository for User Repository (Optional)
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/quickstart/install.md
Optionally, implement JoinCrudRepository for your User repository. This provides enhanced CRUD operations with join capabilities and requires MPJ 1.5.2+ and MP 3.5.9+.
```java
@Repository
public class UserRepository extends JoinCrudRepository {
}
```
--------------------------------
### eq SubQuery Method
Source: https://github.com/mybatis-plus-join/mybatis-plus-join.github.io/blob/main/docs/pages/core/other/sub-query.md
Demonstrates the `eq` method for constructing subqueries. It supports a version with a condition flag to conditionally apply the subquery.
```APIDOC
## eq SubQuery Method
### Description
This method is used to construct an equality condition within a subquery. It allows specifying the column, the main entity class for the subquery, and a wrapper to define the subquery's conditions.
### Method Signature
```java
eq(SFunction colum, Class queryClass, Function wrapper)
```
### Overloaded Method with Condition
```java
eq(boolean condition, SFunction colum, Class queryClass, Function wrapper)
```
This overloaded version allows you to conditionally include the subquery based on the `condition` boolean parameter. If `condition` is `false`, the subquery will not be added.
```