Mybatis common Summary: parameter, return, execution SQL, include, etc. mybatissql

Source: Internet
Author: User

Mybatis common Summary: parameter, return, execution SQL, include, etc. mybatissql

1. Parameter Injection
1.1 In the form of # {0}, # {1}. 0 represents the first parameter, and 1 represents the second parameter.
Public List <RecordVo> queryList (String workerId, Integer topNum );

<Select id = "queryList" resultType = "com. demo. RecordVo">
Select id, WORKER_ID workerId, UPDATE_DATE updateDate
FROM USER_RECORDS t
WHERE t. WORKER_ID = # {0}
LIMIT # {1}
</Select>
1.2Map or encapsulated object, workerId is the key in map; if it is an object, workerId is the attribute in the object, this method is very common
Public Integer queryCountByWorkerId (Map queryParam );
<Select id = "queryCountByWorkerId" parameterType = "java. util. Map" resultType = "java. lang. Integer">
Select count (1)
FROM tableName F
Where f. WORKER_ID =#{ workerId}
</Select>

1.3 annotations
Public Integer queryCountByWorkerId (@ param ("workerId") String workerId );
<Select id = "queryCountByWorkerId" parameterType = "java. util. Map" resultType = "java. lang. Integer">
Select count (1)
FROM tableName F
Where f. WORKER_ID =#{ workerId}
</Select>
2. Return
2.1 ing <! -- Object Class corresponds to table fields -->
<ResultMap type = "com. demo. DataModule" id = "dataModule">
<Result column = "ID" property = "id"/>
<Result column = "CREATE_DATE" property = "createDate"/>
<Result column = "WORKERID" property = "workerId"/>
<Result column = "UPDATE_DATE" property = "updateDate"/>
<Result column = "STATUS" property = "status"/>
</ResultMap>
<Select id = "queryAll" resultType = "dataModule">
Select ID, CREATE_DATE, WORKERID from tableName
</Select>

In this way, the field queried by the query statement is simply the field in the database, and the ing column is defined.
2.2 directly return objects
<Select id = "queryAll" resultType = "com. demo. DataModule">
Select ID, CREATE_DATE createDate, WORKERID workerId from tableName
</Select>

The returned field alias must correspond to the attribute in the returned object.

3. Execute native SQL
3.1sql parameters:
Public class ParamVo {

Private String SQL;
// Getter setter omitted

}

3.2 interface:
/**
* @ Function Description: Create
* @ Param vo
* @ Return
*/
Public int excuteCreateSql (ParamVo vo );

/**
* @ Function Description: Query
* @ Param vo
* @ Return
*/
Public List <Map <String, Object> excuteSelectSql (ParamVo vo );

3.3xml:
<Update id = "excuteCreateSql">
$ {SQL}
</Update>
$ {} Does not compile the SQL statement for direct execution. If # {SQL} is used, an error may be reported.

<Select id = "excuteSelectSql" resultType = "java. util. Map">
$ {SQL}
</Select>
The returned type is map or hashmap.

4. include: Sometimes the fields to be returned by the two methods are the same or the where clause is the same. In this way, to avoid repeated code writing, the include
4.1 same part of definition clause
<SQL id = "queryChild">
FROM tableName1 F
Right join tableName2 c on f. WORKER_ID = C. WORKER_ID
Where f. STATUS = 1 and f. WORKER_ID = # {workerId}
Order by c. CREATE_DATE DESC
</SQL>
4.2 reference
<Select id = "queryCountByWorkerId" parameterType = "java. util. Map" resultType = "java. lang. Integer">
Select count (1)
<Include refid = "queryChild"/>
</Select>
<Select id = "queryListByWorkerId" parameterType = "java. util. Map" resultType = "com. demo. RecordVo">
Select id, WORKER_ID workerId, UPDATE_DATE updateDate ,......
<Include refid = "queryChild"/>
</Select>

 

For your reference only. Please forgive me for your shortcomings. please correct me! Indicate the source for reprinting. If you have any questions, please comment or contact my mailbox 1034570286@qq.com

 

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.