MyBatis its argument is a List object

SSM framework is a framework JavaWeb will learn, although basic CRUD is very simple, but when faced with some special cases, sometimes appear helpless, Cipian used to record some applications Mybatis framework of special scene.

Its argument is a List object

1. scene reproduction

First, there is a table as follows:

MySQL [test]> select * from t_entry_resource;
+----+-------------+------+----------+--------+--------+---------------------+
| id | resource_id | type | title    | banner | icon  | add_date            |
+----+-------------+------+----------+--------+--------+---------------------+
| 11 |          6  | 14   | 分类     | 1.jpg  | 2.jpg  | 2017-11-17 11:22:30 |
| 12 |          3  | 1    | 测试12   | 3.jpg  | 4.jpg  | 2017-11-17 11:22:30 |
| 13 |        653  | 1    | 测试34   | 5.jpg  | 6.jpg  | 2017-11-20 02:32:26 |
| 14 |          1  | 1    | 测试5    | 7.jpg  | 8.jpg  | 2017-11-20 02:32:51 |
| 15 |        3942 | 3    | 测试6    | 9.jpg  | 10.jpg | 2017-11-20 02:34:27 |
+----+-------------+------+----------+--------+--------+---------------------+
5 rows in set (0.01 sec)

If you want to record a batch query and resource_id according to type, how to write Mybatis statement?

2. Solution

Direct stickers solution as follows:

Dao层接口:

List<EntryResource> findByRidAndType(List<EntryResource> entryResources);

XML语句:

<select id="findByRidAndType" resultMap="entryResource" parameterType="list">
        SELECT
        *
        FROM
        t_entry_resource a
        WHERE
<foreach collection="list" index="index" item="entryResources" open="(" close=")" separator="or">
            ( `type`=#{entryResources.type} and resource_id=#{entryResources.resourceId} )
</foreach>

</select>

The foreach statement takes advantage of mybatis of dynamic stitching SQL.

3. foreach properties

Attributes description
item Specific target loop body. Support attribute path access points, such as item.age, item.info.details. DETAILED DESCRIPTION: is one of the objects in the list and the array, it is in the map value. This parameter is mandatory.
collection Do foreach object as the parameter, List <?> List instead of using the default objects as keys, there are array array object instead of as a key, Map object with the map instead of a key. Course be used @Param ( "keyName") when used as the key parameter setting, after setting keyName, list, array, map will be deactivated. In addition to the reference case, there is a field as a parameter when one kind of object. For example: If you have property User List ids. User object is the reference, then the collection = "ids" If User Ids IDS has attributes; where Ids is the object, Ids have an attribute List id; User object is the reference, then the collection = "ids.id" The above example only, What specific collection equal, do you want to see the cycle of elements. This parameter is mandatory.
separator A separator between the elements, for example in () time, separator = "," automatically with the intermediate element "," separated, leading to avoid manually enter a comma sql errors, such as in (1,2,) so. This parameter is optional.
open Foreach start symbol code, typically (and close = ")" in combination. When used in in (), values ​​(). This parameter is optional.
close Close foreach code symbols, usually) and open = "(" combination. Used in the (). When the parameter is optional, values ​​().
index And the list array, index is the number of elements in the map, index is the key element, the parameter is optional.

4. foreach several uses

(1) select count(*) from users id in (x1,x2,x3,...)

<select id="countByUserList" resultType="int" parameterType="list">    
select count(*) from users    
  <where>    
    id in    
    <foreach item="item" collection="list" separator="," open="(" close=")" index="">    
      #{item.id, jdbcType=NUMERIC}    
    </foreach>    
  </where>    
</select> 

(2) select count(*) from key_cols where col_a = ? AND col_b = ?

<select id="sel_key_cols" resultType="int">    
        select count(*) from key_cols where    
<foreach item="item" index="key" collection="map"  open="" separator="AND" close="">
        ${key} = #{item}
</foreach>    
</select>  

(3) select * from t_news n where n.tags like ? or n.tags like ?

<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
  select * from t_news n where 
  <foreach collection="listTag" index="index" item="tag" open="" separator="or" close="">
            n.tags like  '%'||#{tag}||'%'
  </foreach>
<select>

5. References

https://www.cnblogs.com/dflmg/p/6398033.html

http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html

pay

Guess you like

Origin www.cnblogs.com/coderzhw/p/11094300.html