mybatis return value problem

simple

If you encounter a problem, you have to solve it, otherwise you will always be confused. It's not so difficult to find it after solving it



Question: what is the meaning of adding, modifying, deleting and returning in Mybatis?

Initial stage: Mybatis, who just started the internship and self-taught, can only use the basic skills and doesn't care about these details at all

At the beginning of internship: it's good to see that others are so skilled and follow them. Why do you care so much

BI technical intermediate stage: later, I thought of a problem. When I saw the result returned by others directly compared with 0, I found that it was not quite right. It seems that it has not only changed so many lines

The code is as follows:

        // Update classification status
        int result = categoryMapper.updateCategoryStatus(categoryStatusUpdateDTO.getCategoryStatus(),
                categoryStatusUpdateDTO.getCategoryId(), new Date(), uid);

        if (result == 0) {
            // Update failed rollback
            throw new RollBackException("update is error");
        }

Reflection: a lot of confusion comes to my mind. I can't stand it. It's too uncomfortable. If there are more modifications, the company's similar code always returns 0. No matter what Integer is returned, the number of affected lines or the number of matched lines, this is an incorrect idea. It can only show that the company's code is not standardized and there are a lot of problems. This is just one of them

The correct way is to check the number of rows satisfying the response from the database, return a size, compare the result with the size, and throw an exception for rollback if it is not equal

First, a specification problem is solved, and then the problem comes again. What exactly is the returned result? Is it really the number of affected rows (I always thought so before). If it is the number of affected rows, my previous idea may be problematic. If a person's name is Dongdong and his name is changed to Dongdong, is the number of unaffected rows 0? For this reason, Decide to test:



Conclusion:

Run the test case to prove that the number of matching rows returned by mapper is indeed

However, you can return the number of records actually modified, which needs to be added to the JDBC URL

useAffectedRows=true

After adding this, the mapper returns the number of affected rows, which is similar to the returned value of mysql (mysql returns the number of affected rows. For example, if the modified value is the original value, mysql will directly return 0)

This proves that my above idea is correct. If the number of matching rows is returned, you can retrieve data from the database and judge whether it is equal. On the contrary, if it is affected, it is absolutely impossible, which will lead to wrong judgment


Relevant parameters:

Other common parameters:
UsePerformanceMonitor,userperfmon, perfmon: Enable performance monitoring, default false
IgnorePrepare:     Ignore Prepare() Call, default true
UseProcedureBodies,procedure bodies: Whether to check the validity of stored procedure body and parameters. The default value is true
AutoEnlist:     Whether to automatically use the active connection, default true
TreatTinyAsBoolean: Will TINYINT(1) The column is treated as Boolean and the default value is true
AllowUserVariables: Allow SQL User variables appear in the default false
FunctionsReturnString: Whether all server functions are processed as returned strings is the default false
UseAffectedRows: Whether to return data by replacing the number of rows found with the number of affected rows. The default is false
Keepalive:     keep TCP The number of seconds to connect. The default is 0. It is not maintained.
ConnectionLifeTime: The minimum time (in seconds) that a connection remains in the connection pool before it is destroyed. Default 0
Pooling:     Whether to use thread pool, default true
MinimumPoolSize, min pool size: The minimum number of threads allowed in the thread pool. The default is 0
MaximumPoolSize,max pool size: The maximum number of threads allowed in the thread pool. The default is 100
ConnectionReset: Whether to reset automatically after the connection expires is the default false
CharacterSet, charset: The character set used to request a connection from the server. Default: none

Other common parameters:
UsePerformanceMonitor,userperfmon, perfmon: Enable performance monitoring, default false
IgnorePrepare:     Ignore Prepare() Call, default true
UseProcedureBodies,procedure bodies: Whether to check the validity of stored procedure body and parameters. The default value is true
AutoEnlist:     Whether to automatically use the active connection, default true
TreatTinyAsBoolean: Will TINYINT(1) The column is treated as Boolean and the default value is true
AllowUserVariables: Allow SQL User variables appear in the default false
FunctionsReturnString: Whether all server functions are processed as returned strings is the default false
UseAffectedRows: Whether to return data by replacing the number of rows found with the number of affected rows. The default is false
Keepalive:     keep TCP The number of seconds to connect. The default is 0. It is not maintained.
ConnectionLifeTime: The minimum time (in seconds) that a connection remains in the connection pool before it is destroyed. Default 0
Pooling:     Whether to use thread pool, default true
MinimumPoolSize, min pool size: The minimum number of threads allowed in the thread pool. The default is 0
MaximumPoolSize,max pool size: The maximum number of threads allowed in the thread pool. The default is 100
ConnectionReset: Whether to reset automatically after the connection expires is the default false
CharacterSet, charset: The character set used to request a connection from the server. Default: none



Related articles:

Article 1

Article 2

Article III

Tags: Java Database MySQL Back-end

Posted on Wed, 29 Sep 2021 22:03:01 -0400 by vishal99