MyBatis SQL in Query

Source: Internet
Author: User

MyBatis Official Study Document: http://www.mybatis.org/core/getting-started.html

This article transferred from: http://www.blogjava.net/xmatthew/archive/2011/08/31/355879.html

1. When the parameter of the query is only one
Findbyids (list<long> IDs)
1.1 If the type of the argument is list, then when used, the collection property must be specified as a list

XML code
  1. <select id= "findbyidsmap" resultmap="Baseresultmap">
  2. Select
  3. <include refid="base_column_list" />
  4. From Jria where ID in
  5. <foreach item="item" index="index" collection="list" open="(" Separator="," close=")">
  6. #{item}
  7. </foreach>
  8. </Select>



Findbyids (long[] IDs)
1.2 If the type of the argument is an array, the collection property must be specified as an array when used

XML code
  1. <select id= "findbyidsmap" resultmap="Baseresultmap">
  2. Select
  3. <include refid="base_column_list" />
  4. From tabs where ID in
  5. <foreach item="item" index="index" collection="Array" open="(" Separator="," close=")">
  6. #{item}
  7. </foreach>
  8. </Select>


2. When there are multiple parameters for the query, such as Findbyids (String name, long[] IDs)
This situation requires special attention, when passing parameters, be sure to use the map mode, so that the collection property can specify the name
Here is an example
map<string, object> params = new hashmap<string, object> (2);
Params.put ("name", name);
Params.put ("IDs", IDS);
Mapper.findbyidsmap (params);

XML code
  1. <select id= "findbyidsmap" resultmap="Baseresultmap">
  2. Select
  3. <include refid="base_column_list" />
  4. From tabs where ID in
  5. <foreach item="item" index="index" collection="IDs" open="(" Separator="," close=")">
  6. #{item}
  7. </foreach>
  8. </Select>

MyBatis SQL in Query

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.