SoFunction
Updated on 2025-04-12

LambdaUpdate usage and example analysis of service interface in MyBatis-Plus

Deeply explore the usage and examples of the service interface in MyBatis-Plus

introduce:

  • MyBatis-Plus is an excellent ORM framework that can simplify interaction and operation with databases.
  • Among them, lambdaUpdate is a powerful way to allow update operations to be performed in the Service interface.

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, we may need 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 a method for updating the User object in the UserService interface.

Here is an example:

import ;

public interface UserService extends IService<User> {

    boolean updateUser(User user);
}

In the example above, we defineupdateUserMethod, used to update the information of the User object.

Next, in the UserServiceImpl implementation class, we use lambdaUpdate to build update conditions and call the corresponding method to perform the update.

Here is an example:

import ;
import ;
import ;

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

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

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

passeqMethod, we specify the field to be updated by() and the corresponding value. For example, we set the username and mobile phone number of the User object to the new value respectively.

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

test

To verify that our update method works properly, we can write unit tests.

Here is a simple test example:

import ;
import ;
import ;

@SpringBootTest
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void testUpdateUser() {
        User user = new User();
        (1L); // Assume that you want to update the user information with ID 1        ("John Doe"); // Set a new username        ("1234567890"); // Set a new mobile phone number
        boolean result = (user);
        ("Update successful: " + result);
    }
}

In the above test, we injected the UserService interface and calledupdateUserMethod to update user information.

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

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.