MyBatis Common Operational Database knowledge

Source: Internet
Author: User
Tags uuid

    • MyBatis generating UUID         
<InsertId="Insert"ParameterType="Custom attribute class name"><Selectkey Keyproperty=The UserId//Your primary key property name "Resulttype="String"Order="Before">Select Replace(UUID (),'-',"') fromDual//copy as is</Selectkey>Insert  intoSys_user (user_id,user_name, Login_name, user_pwd)Values(#{userid,jdbctype=VARCHAR}, #{username,jdbctype=VARCHAR}, #{loginname,jdbctype=VARCHAR}, #{userpwd,jdbctype=VARCHAR})</Insert>
    • Select

<InsertId="Addbean" ParameterType="Com.test.bean.ScenicPunish">       <!--generate UUID --      <Selectkey Keyproperty="PUNISH_ID" Resulttype="Java.lang.String"Order="Before">        Select Replace(UUID (),'-',"') fromDual</Selectkey>      Insert  intot_scenic_punish (punish_id,punish_prov,punish_city,punish_country,punish_scenic,punish_org, Punish_type, Punish_date,punish_title,punish_content,punish_url)Values(#{punish_id},#{punish_prov},#{punish_city},#{punish_country},#{punish_scenic},#{punish_org},#{punish_type },#{punish_date},#{punish_title},#{punish_content},#{punish_url})</Insert>

SELECT statement Property configuration details:
Properties Description Take value default
Id A unique identifier in this mode, which can be referenced by other statements
ParameterType The full class name or alias of the argument passed to this statement
Resulttype Statement returns the whole class name or alias of a value type. Note that if it is a collection, it is filled with the entire class name or alias of the item in the collection, not the class name of the collection itself. (Resulttype and Resultmap cannot be used)
Resultmap The external resultmap name of the reference. Result set mapping is the most powerful feature in MyBatis. Many complex mappings can be easily resolved. (Resulttype and Resultmap cannot be used)
Flushcache If set to True, the cache will be emptied every time the statement is called. The SELECT statement is set to False by default True/false False
UseCache If set to true, the result set of the statement is cached. The SELECT statement is set to False by default True/false False
Timeout Sets the maximum time that a drive waits for a response before throwing an exception, with no value set by default and determined by the drive itself Positive integers Not set
Fetchsize After setting a value, the drive returns after the number of result sets reaches this value, default is no value, determined by the drive itself Positive integers Drive decision
StatementType Statement,preparedstatement,callablestatement. Pre-prepared statements, callable statements STATEMENT, PREPARED, callable PREPARED
ResultsetType Forward_only, Scroll_sensitive, scroll_insensitive only forward, scrolling sensitive, case-insensitive scrolling Forward_only, Scroll_sensitive, scroll_insensitive Drive decision
Insert can use the automatically generated primary key policy supported by the database, set usegeneratedkeys= "true", and then set the Keyproperty to the corresponding column, it is done. For example, the studententity above uses auto-generated to generate a primary key for the ID column.
You can also define automatic Updates in the database.
or update the ID value with the UUID


<!--insert Student Auto PRIMARY KEY--<insert id= "Insertstudentautokey" parametertype= "studententity" > <selectKey keyproperty= "StudentID" resultType = "String" order= "BEFORE" > Select Nextval (' Student ') </selectkey> INSERT into Student_ TBL (student_id, Student_name, Student_sex, Student_birthday, class_id) VALUES (#{studentid}, #{studentname}, #{ Studentsex}, #{studentbirthday}, #{classentity.classid}) </ Insert>


       
    • Insert

<insert id="insertstudent" parametertype="studententity" Usegeneratedkeys ="true" keyproperty= "studentid" >

Id A unique identifier in this mode, which can be referenced by other statements
ParameterType The full class name or alias of the argument passed to this statement
Flushcache If set to True, the cache will be emptied every time the statement is called. The SELECT statement is set to False by default True/false False
UseCache If set to true, the result set of the statement is cached. The SELECT statement is set to False by default True/false False
Timeout Sets the maximum time that a drive waits for a response before throwing an exception, with no value set by default and determined by the drive itself Positive integers Not set
Fetchsize After setting a value, the drive returns after the number of result sets reaches this value, default is no value, determined by the drive itself Positive integers Drive decision
StatementType Statement, PreparedStatement, CallableStatement. Pre-prepared statements, callable statements STATEMENT, PREPARED, callable PREPARED
Usegeneratedkeys Tell MyBatis to use the JDBC Getgeneratedkeys method to get the primary key generated by the database itself (MySQL, SQL Server, and other relational databases will have automatically generated fields). Default: False True/false False
Keyproperty Identifies a value returned by a key that will be set into Getgeneratedkeys by MyBatis, or a selectkey child element for the INSERT statement.

Selectkey Statement Property configuration details:

MyBatis Common Operational Database knowledge

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.