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
- <select id= "findbyidsmap" resultmap="Baseresultmap">
- Select
- <include refid="base_column_list" />
- From Jria where ID in
- <foreach item="item" index="index" collection="list" open="(" Separator="," close=")">
- #{item}
- </foreach>
- </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
- <select id= "findbyidsmap" resultmap="Baseresultmap">
- Select
- <include refid="base_column_list" />
- From tabs where ID in
- <foreach item="item" index="index" collection="Array" open="(" Separator="," close=")">
- #{item}
- </foreach>
- </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
- <select id= "findbyidsmap" resultmap="Baseresultmap">
- Select
- <include refid="base_column_list" />
- From tabs where ID in
- <foreach item="item" index="index" collection="IDs" open="(" Separator="," close=")">
- #{item}
- </foreach>
- </Select>
MyBatis SQL in Query