如何用LambdaWrapper去操作 根据条件去数据库修改值。至少提供三种解决方法,必须包含sql
时间: 2024-02-09 07:07:52 浏览: 24
LambdaWrapper 是 MyBatis-Plus 提供的一个实用工具,用于快速构建查询条件,可以通过它来操作数据库进行修改等操作。以下是三种使用 LambdaWrapper 去操作根据条件去数据库修改值的方法:
方法一:使用 update 方法
```java
LambdaUpdateWrapper<YourEntity> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(YourEntity::getId, id) // where id = ?
.set(YourEntity::getName, "newName"); // set name = 'newName'
int rows = yourMapper.update(null, updateWrapper);
```
对应的 SQL 语句:
```sql
update your_table set name = 'newName' where id = ?
```
方法二:使用 update 方法和 setSql 方法
```java
String sqlSet = "name = 'newName'";
LambdaUpdateWrapper<YourEntity> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(YourEntity::getId, id) // where id = ?
.setSql(sqlSet); // set name = 'newName'
int rows = yourMapper.update(null, updateWrapper);
```
对应的 SQL 语句:
```sql
update your_table set name = 'newName' where id = ?
```
方法三:使用 update 方法和 SQL 片段
```java
String sqlSet = "name = 'newName'";
LambdaUpdateWrapper<YourEntity> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(YourEntity::getId, id) // where id = ?
.set(new SqlSet(sqlSet)); // set name = 'newName'
int rows = yourMapper.update(null, updateWrapper);
```
对应的 SQL 语句:
```sql
update your_table set name = 'newName' where id = ?
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)