Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The return value replacement in Mybatis Update operation is changed to the number of affected entries.

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to change the return value to the number of affected items in Mybatis Update operation. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something through the detailed introduction of this article.

What exactly does the update return value represent? Let's see if we have the following table and two pieces of data:

Let's write a simple unit test case to verify the following, first use mybatis to simply write a mapper to update the operation, where the content in xml is:

Database connections configured as:

Next, let's write a simple unit test to verify whether the return value of update is the number of records affected. The corresponding unit test code is as follows:

From the unit test code, we know that we are going to change the phone field value of two records in the database from 12345678 to 666666. Under normal circumstances, resultCode will return 2. Because the update operation affects these two records in the database, this is consistent with our expectation of 2. So if everything works, this unit test will pass, so let's run it and see what happens:

The unit test passed, and then check the records in the database:

This means that mybatis update does return the number of affected rows…is that true?

We know that when a record in the database has been modified, repeated update operations will not affect the new number of rows. To verify what I said, let's try:

So, following this logic: if we run the unit test again, the resultCode should return 0, which is inconsistent with our expected number of 2, which will cause the test to fail. Run the unit test again:

You can see that by default, mybatis update returns the number of matched records, not the number of affected records. Strictly speaking, this is not the return value of mybatis, mybatis is just the return value of the database connection driver (usually JDBC), that is, if the driver tells us to update 2 records affected, then we will get the return value of mybatis is 2 and mybatis itself is unrelated. I understand why, but if we had to ask mybatis update to explicitly return the number of records affected, is there any way? Of course there is. By explicitly specifying the useAffectedRows option to the JDBC URL, we get the number of records affected:

jdbc:mysql://${jdbc.host}/${jdbc.db}? useAffectedRows=true

We modified our database connection configuration slightly by adding the useAffectedRows field:

At this point, mybatis update operation should return the number of affected items, we run the unit test again to try:

The update operation returns the number of records affected, which we know is 0 and does not match our expectation of 2, so naturally the unit test fails.

The above content is the Mybatis Update operation return value replacement modified to the affected number, you have learned knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report