What does the insert and delete sqlsession in Ssm-mybatis-07:mybatis do?

Source: Internet
Author: User

Let's go in and look at the Insert method.

Use CTRL and the left mouse button to go inside.

Discovery is an interface sqlsession method, not implemented, but by the idea of the Richter substitution principle, he is the interface that receives the implementation class, so find his implementation class Defaultsqlsession (ideas shortcut key Ctrl+h)

After entering, you can see the overloads of multiple insert methods by using Ctrl+f to find the insert, but their method implementation calls this

 Public int Insert (String statement) {        returnthis. Insert (statement, (Object)null) ;    }      Public int Insert (String statement, Object parameter) {        returnthis. Update ( statement, parameter);    }

  The method above calls the following method, and the following insert method does call the Update method

Take a look at the Delete method,

He is similar to insert, is the interface sqlsession method, so still find his implementation class Defalutsqlsession

Keep looking for the Delete method after you go in

 Public int Delete (String statement) {        returnthis. Update (statement, (Object)null );}  Public int Delete (String statement, Object parameter) {        returnthis. Update ( statement, parameter);}

It can be seen that he also called the Update method, so it was curious to update the method with the two parameters what is there? Why do you call it all?

Take a look at the Update method

 Public intUpdate (String statement, Object parameter) {intVAR4; Try {             This. Dirty =true; Mappedstatement e= This. Configuration.getmappedstatement (statement); VAR4= This. Executor.update (E, This. Wrapcollection (parameter)); } Catch(Exception var8) {ThrowExceptionfactory.wrapexception ("Error Updating database. Cause:"+var8, VAR8); } finally{errorcontext.instance (). reset (); }        returnVAR4; }

  Here's a simple mention,

1) Dirty=true; represents that he has become dirty data, meaning that in-memory data is inconsistent with the database, why change here, perhaps to talk about why the additions and deletions need to manually commit the transaction (Session.commit ()) and

Why do session.close cause things to roll back, maybe the next few chapters blog will have a simple analysis of

2) Mappedstatement This I find some information, but from Baidu search not tell understand this is what thing, simply say, if wrong, please help me to point out, it should be get to mybatis configuration in the specific SQL

3) VAR4 is to execute SQL with the executor, receive the return value, the value returned by the update is int, and is VAR4, can infer that he should return the affected number of rows

4) Errorcontext from the literal meaning of the context of the error, instance is the meaning of the instance, he source code is through a static method, get Errorcontext object instance or create an instance and return back, a single example, reset is reset, reset,

Meaning of the initialization

What does the insert and delete sqlsession in Ssm-mybatis-07:mybatis do?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.