Hao947: mybatis resultmap configuration insert and primary key auto-increment return: Good 947

Source: Internet
Author: User

Ing configuration file 947

<! -- Type: ID of the Data Type of the ing object class: Unique Identifier of resultmap --> <resultmap type = "person" id = "baseresultmap"> <! -- Column: Field Name property of the database table: property name in the object class --> <ID column = "person_id" property = "personid"/> <result column = "name" property = "name"/> <result column = "gender" property = "gender"/> <result column = "person_addr" property = "personaddr"/> <result column = "Birthday" property = "Birthday"/> </ resultmap>


The ing configuration query statement is 947 better

<Insert id = "insert" parametertype = "com. hao947.model. Person"> <! -- Keyproperty: Id attribute name in the object class. When the primary key is returned, it is returned to the keyproperty order: the order in which the primary key is generated and the insert statement is executed. In MySQL, it is after, use before resulttype in Oracle: the data type returned by the primary key. It is the same as the type of the primary key in the object class. Select last_insert_id (): SQL statement for generating the primary key in MySQL, and select XXXX in Oracle. nextval from dual --> <selectkey keyproperty = "personid" Order = "after" resulttype = "Java. lang. integer "> select last_insert_id (); </selectkey> insert into person (person_id, name, gender, person_addr, birthday) values (# {personid}, # {name }, # {gender}, # {personaddr}, # {birthday}) </Insert>


Ing management library table file hao947

<! -- Centrally manage the ing files of database tables --> <mappers> <mapper resource = "com/hao947/SQL/mapper/personmapper. xml"/> </mappers>


Test File hao947

@ Test public void insert () {sqlsession session = sqlsessionfactory. opensession (); try {person P = new person (); // P. setpersonid (3); p. setname ("architect"); p. setgender (1); p. setpersonaddr ("Beijing"); p. setbirthday (New simpledateformat ("yyyy-mm-dd "). parse ("1985-03-14"); Session. insert ("com. hao947. SQL. mapper. personmapper. insert ", P); // The transaction session must be submitted after the database and table changes are completed. commit ();} catch (parseexception e) {session. rollback (); E. printstacktrace ();} finally {session. close ();}}



Big sites like collection: Collect all the data! Havip 947: Product

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.