5.5 Mybatis Update tag actual combat, what is the return value? Teach you how to deal with it usually

CSDN achieves 100 million technicians



foreword

Through " Above ", we learned how to use the insert tag in Mybatis, and learned how to use two methods to obtain the returned primary key id. This article mainly explains the update tag , which is used to map with the update SQL statement. It is still combined with actual combat while doing it. .
In fact, the update tag is very simple, and there are not many knowledge points! Therefore, the main knowledge points arranged in this article: What is the return value? Why do we talk about the return value? It is indeed because some students raised questions about the return value of the insert tag above in the group, and the actual update tag is more representative. Because the return value of a single insert is always 1 as long as there is no exception, so we don't need to deal with it! But update is different. An update statement can modify N records at the same time, so what is its return value? How do you usually deal with it? Come with me to start learning today, Let's Go!


1. Update tag actual combat

The update tag is used to map the update statement in the SQL statement
Let's take the sum above UserMapperas UserMapper.xmlan example to actually modify the password function!

① Add update method in UserMapper interface

Add an interface method in UserMapper.java, and pass in two parameters of id and password.

int updatePassword(@Param("id") Integer id, @Param("password") String password);

Specify the parameter name by @Param

② MybatisX plugin generates update tag

insert image description here
After clicking, it will be generated in UserMapper.xml as follows (if you don’t use a plug-in to generate it, it’s the same if you write it yourself):

<update id="updatePassword"></update>

说明:Like the select and insert tags, it is associated with the interface method through a unique id.

③ Write update SQL statement

Next, write the corresponding update SQL statement, as follows:

<update id="updatePassword">
    update user set password = #{password} where id = #{id}
</update>

In this way, the password can be changed using the Mybatis update tag, isn't it So Easy? The user experience of Mybatis is very friendly, as long as you can write SQL, you can learn it in minutes!


2. What is the return value of update sql?

The return value of the update tag is actuallyThe return value of the update sql statement, then we first understand two concepts

  • Affected rows: the actual number of rows affected after execution
  • Number of matching lines: the number of lines matching the condition before execution

Please see the example, we modify the user password, notice that the password has changed at this time, and see what is returned:
insert image description here

There are two main return values, pay attention to the difference:
1 row affected it means that 1 row is affected, that is to say, the data of 1 row has changed; it means that
Rows matched: 11 row has been matched, it can only mean that there is a row that matches the update condition, and the data does not necessarily change

Let's execute the same sql again, and carefully see that the return value has changed:
insert image description here

Return value:
0 row affected 这时为什么0行受影响?because the password was 123 before it was changed, changing it to 123 is equivalent to no modification!
Rows matched: 1 still 1 row has been matched


3. What is the return value of the Mybatis update tag?

There are two return values ​​for update sql, but the Mybatis update tag only returns an int. Which one is it? By default, the number of matching rows
is returned , because the number of affected rows is more meaningful to us, through which we can determine the number of rows actually modified by the data, so usually specify useAffectedRows=true in spring.datasource.url to return affected rows number

spring.datasource.url=jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=utf8&useSSL=false&useAffectedRows=true

4. Implement a simple password modification API

It is not appropriate to change the password like this, it is only an example

1. dal layer

insert image description here

2. service layer

insert image description here

Create a new UserService interface:

Here I directly return the number of rows, so that everyone can see the actual number of rows affected more intuitively.
If the business needs, in fact, the service layer can be judged according to the number of affected rows, for example: judgment > 0 means the number of affected rows, and then related logical processing will be carried out!

package org.tg.book.service;

public interface UserService {
    
    

    /**
     * 修改密码
     */
    Integer updatePassword(Integer id, String password);
}

Corresponding UserServiceImpl implementation class:

@Service
public class UserServiceImpl implements UserService {
    
    
    @Autowired
    private UserMapper userMapper;
    @Override
    public Integer updatePassword(Integer id, String password) {
    
    
        return userMapper.updatePassword(id, password);
    }
}

3. web layer

insert image description here

New UserController:

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*")
public class UserController {
    
    

    @Autowired
    private UserService userService;

    @PostMapping("/password")
    public TgResult<Integer> updatePassword(@RequestBody PasswordVO passwordVO){
    
    
        Integer rows = userService.updatePassword(passwordVO.getId(), passwordVO.getPassword());
        return TgResult.ok(rows);
    }
}

Corresponding PasswordVO:

@Data
public class PasswordVO implements Serializable {
    
    
    private Integer id;
    private String password;
}

Pass the self-test

insert image description here
insert image description here


Five, Git submission

Develop a good habit of submitting in small steps and submitting multiple times to avoid accumulating a lot of code. When submitting CodeReview at the end, one place needs to be modified, resulting in the failure of all codes to be merged!
insert image description here


at last

If you like it, if you want to learn more practical things, if you want to find someone to help you grow quickly, if you want to invest in yourself, click the link to subscribe to the column:

Actual server-side combat : SpringBoot+Vue front-end separation project actual combat
Front-end actual combat : Vue + SpringBoot front-end separation project actual combat

Specific advantages, planning, and technology selection can all be read in the " Opening Chapter ", with detailed descriptions! After subscribing, add my WX below, one-on-one guidance!

In addition, don't forget to pay attention to: Tiangang gg , it is not easy to miss the release of new articles: https://blog.csdn.net/scm_2008

Old rules, please vote for me, thank you for your support!

Guess you like

Origin blog.csdn.net/scm_2008/article/details/130415134