Using MyBatis to perform label operations such as INSERT, UPDATE, Delete, and so on in the corresponding SQL mapper configuration, the database record is unchanged

Source: Internet
Author: User

I used the SPRINGMVC integration MyBatis to perform the insert operation in the Sqlmapper configuration file and found that the program did not error, but the database table did not have the record just inserted. Looked up a lot of information, and finally found the answer on a blog: After the completion of the method, must have Session.commit (), this sentence to commit the transaction. Because the transaction is turned on when the insert Update Delete is made, and the MyBatis is not automatically committed (and may be set, I don't know), the transaction must be committed manually. So I just call the method that contains the insert operation and add Session.commit () to record the successful storage.

Interface Definition section: Public InterfaceMenumapper { PublicList<menuvo> Getparentmenu (map<string,object>Parammap);  PublicList<menuvo> GetSubMenu (map<string,object>Parammap);  Public intSavemenu (Menuvo menuvo);} Interface Invocation section: Try{menumapper Menumapper=sqlsession.getmapper (Menumapper.class); System.out.println (NewGson (). ToJson (Menuvo)); intflag=Menumapper.savemenu (MENUVO);   Sqlsession.commit (); returnFlag;}Catch(Exception e) {logger.info ("Save menu failed, error:" +e.getmessage ());} finally{sqlsession.close ();} SQL Mapper configuration file:
<!--An SQL statement that performs an increase in operations. The ID and ParameterType are identical to the name and parameter type of the Savemenu method in the Menumapper interface, respectively. Usegeneratedkeys is set to "true" to indicate that MyBatis gets the primary key generated automatically by the database; keyproperty= "MenuId" Specifies to inject the acquired primary key value into Menuvo, MenuId properties-< Insert Id= "Savemenu" parametertype= "Menuvo" usegeneratedkeys= "true" keyproperty= "MenuId" >INSERT INTO menu (Parentmenuid,menuurl,menuname) VALUES (#{parentmenuid},#{menuurl},#{menuname})

</insert>

Using MyBatis to perform label operations such as INSERT, UPDATE, Delete, and so on in the corresponding SQL mapper configuration, the database record is unchanged

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.