In-depth exploration of the lambdaUpdate usage and examples of the Service interface in MyBatis-Plus

In-depth exploration of the lambdaUpdate usage and examples of the Service interface in MyBatis-Plus

Introduction:
MyBatis-Plus is an excellent ORM framework that simplifies the interaction and operation with the database. Among them, lambdaUpdate is a powerful way to allow update operations to be performed in the Service interface. This article will explain in detail the usage of lambdaUpdate in MyBatis-Plus, and provide rich cases to help readers better understand and apply this feature.

case background

Let's take a user management system as an example. Suppose we have a User class as a user entity. After the user registers, it may be necessary to perform some modification operations on the user, such as updating the user name, mobile phone number and other information.

Update data using lambdaUpdate

First, define the method for updating the User object in the UserService interface. Here is an example:

import com.baomidou.mybatisplus.extension.service.IService;

public interface UserService extends IService<User> {
    
    

    boolean updateUser(User user);
}

In the above example, we defined updateUsermethods for updating the information of the User object.

Next, in the UserServiceImpl implementation class, we use lambdaUpdate to construct the update condition, and call the corresponding method to perform the update. Here is an example:

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
    

    @Override
    public boolean updateUser(User user) {
    
    
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, user.getId())
                     .set(User::getUsername, user.getUsername())
                     .set(User::getPhoneNumber, user.getPhoneNumber());
        int rows = baseMapper.update(null, updateWrapper);
        return rows > 0;
    }
}

In the above example, we use LambdaUpdateWrapper to create an updateWrapper object and set update conditions.

Through eqthe method, we specify (updateWrapper.eq) the field to be updated and the corresponding value. For example, we set the user name and mobile phone number of the User object as new values ​​respectively.

Then, we call the update method of baseMapper, pass in null as the entity object (because the update condition has been set in updateWrapper), and pass in the updateWrapper parameter to perform the update.

test

To verify that our update method is working, we can write unit tests. Here is a simple test example:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class UserServiceTest {
    
    

    @Autowired
    private UserService userService;

    @Test
    public void testUpdateUser() {
    
    
        User user = new User();
        user.setId(1L); // 假设要更新ID为1的用户信息
        user.setUsername("John Doe"); // 设置新的用户名
        user.setPhoneNumber("1234567890"); // 设置新的手机号码

        boolean result = userService.updateUser(user);
        System.out.println("Update successful: " + result);
    }
}

In the above test, we injected the UserService interface and called updateUserthe method to update the user information.

By writing and running these test cases, we can verify that the data update functionality using lambdaUpdate works as expected.

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132030243