The return value update mybatis

Excerpt: https: //www.jianshu.com/p/80270b93082a

 

If, define a updatefunction, the return value of the function in the end is what does it mean? Is the number of rows affected you?

Before we look at the data verification records in the database. A total of two data records!

Database links configured to:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=123456

Let's look at our unit test classes.

We get a record based on ID, then the user name was changed from root root001. If the return value is the number of rows affected, then we should return 1. With the expected result 1 is consistent. Unit test will pass.

Unit test passes, we look at the records in the database have not changed.

It seems, without any problems. By unit testing, database record is really only one change. This shows that the update operation mybatis return value is indeed the number of rows affected.

Is it really

We know that record when the database is modified after we perform the same update statement at times it will not affect the number of rows of data records.

Then, according to this logic executed here in terms of this test unit, the return value should be zero, with our different expectations 1, unit tests should not pass. Running unit tests again:

I went, unit testing actually magically passed. . . Please look, we at the command line to perform an update statement that picture, the number of matched returned to 1. So by default, the return value of update operations mybatis is the number of records matched, not the number of records affected.

Is there a way to update the operating mybatis the return value is the number of rows affected by it. Because our business logic is sometimes based on the return value to do business judgment. The answer of course is yes.
Modify the database link is configured to: increase the useAffectedRows field information.

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useAffectedRows=true
jdbc.username=root
jdbc.password=123456

 Running unit tests again:

We can clearly see from the error message, the return value is 0, with our different expectations 1.




 

Guess you like

Origin www.cnblogs.com/xinruyi/p/11403132.html