XML批量查询
collection: 指定要遍历的集合(三种情况 list,array,map) !!!!在这种使用注解sql的情况下,这里请填写mapper方法中集合的名称
item:将当前遍历出的元素赋值给指定的变量 (相当于for循环中的i)
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
index:索引。遍历list的时候是index就是索引,item就是当前值
#{变量名}就能取出变量的值也就是当前遍历出的元素
List在xml的方式
Listlist = new ArrayList (); list.add(1); list.add(2); list.add(3); TestDAO.getItermByList(list); xml中的sql: <select id="getItermByList" resultType="java.util.HashMap"> SELECT id,display_name as name from t_recommendation_info where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} foreach> select>
Map在xml的方式
Listlist = new ArrayList (); list.add(1); list.add(2); list.add(3); HashMap parames = new HashMap (); parames.put("mapList",list); TestDAO.getItermByList(parames); xml中的sql: <select id="getItermByList" resultType="java.util.HashMap"> SELECT id,display_name as name from t_recommendation_info where id in <foreach item="item" index="index" collection="mapList" open="(" separator="," close=")"> #{item} foreach>select>
对象在xml的方式
Listlist = new ArrayList (); list.add(1); list.add(2); list.add(3); //TestModel 的实现 public class TestModel { private List searchList; public List getSearchList() { return searchList; } public void setSearchList(List searchList) { this.searchList = searchList; } } TestModel tm = new TestModel(); tm.setSearchList(list); TestDAO.getItermByList(tm) //XML中的sql: <select id="getItermByList" resultType="java.util.HashMap"> SELECT id,display_name as name from t_recommendation_info where id in <foreach item="item" index="index" collection="searchList" open="(" separator="," close=")"> #{item} foreach> select>
xml批量insert
<!--批量增加测试--> ="insertList" parameterType="java.util.List"> insert into t_enterprise_water_ele ( /*方法一*/ -- WATER_ELE_ID, -- ENTERPRISE_ID, -- ENTERPRISE_USCC, -- ENTERPRISE_NAME, -- YEARMONTH, -- WATER_SIZE, -- WATER_AMOUNT, -- ELE_SIZE, -- ELE_AMOUNT, -- STATUS, -- OPERATOR, -- OPERATE_TIME /*方法二*/ ="Base_Column_List"/> ) VALUES <foreach collection="list" item="item" index="index" separator=","> ( #{item.waterEleId,jdbcType=VARCHAR}, #{item.enterpriseId,jdbcType=VARCHAR}, #{item.enterpriseUscc,jdbcType=VARCHAR}, #{item.enterpriseName,jdbcType=VARCHAR}, #{item.yearmonth,jdbcType=VARCHAR}, #{item.waterSize,jdbcType=DECIMAL}, #{item.waterAmount,jdbcType=VARCHAR}, #{item.eleSize,jdbcType=DOUBLE}, #{item.eleAmount,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER}, #{item.operator,jdbcType=VARCHAR}, #{item.operateTime,jdbcType=TIMESTAMP} ) </foreach> </insert>
对于foreach标签的解释参考了网上的资料,具体如下:
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名
index指定一个名字,用于表示在迭代过程中,每次迭代到的位置
open表示该语句以什么开始
separator表示在每次进行迭代之间以什么符号作为分隔 符
close表示以什么结束
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名
index指定一个名字,用于表示在迭代过程中,每次迭代到的位置
open表示该语句以什么开始
separator表示在每次进行迭代之间以什么符号作为分隔 符
close表示以什么结束
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map
xml批量删除
"deleteByBatch" parameterType="java.lang.String"> delete from t_enterprise_output_value where OUTPUT_ID IN <foreach collection="array" item="outputId" open="(" separator="," close=")"> #{outputId} foreach>
xml批量更新
"WHOLESALE-UPDATE-REBATE-CONFIG-BY-PRODUCTCODE" > UPDATE rebate_config SET rebate_type = 'BY_RATE',rebate_config_value = #rebateConfigValue# WHERE "list" conjunction="or" open="(" close=")"> product_code =#list[]#
标签说明:
- prepend 可被覆盖的 SQL 语句组成部分,添加在语句的前面(可选)
- property 类型为 java.util.List 的用于遍历的元素(必选)
- open 整个遍历内容体开始的字符串,用于定义括号(可选)
- close 整个遍历内容体结束的字符串,用于定义括号(可选)
- conjunction 每次遍历内容之间的字符串,用于定义 AND 或 OR(可选)
注意
-
这里传递过来的参数是一个Map
Map
conditions = new HashMap (); conditions.put("rebateConfigValue", rebateValue); //rebateValue是一个固定的值 conditions.put("list", productCodeList); -
使用时,在List元素名后面包括方括号[]非常重要,方括号[]将对象标记为List,以防解析器简单地将List输出成String
批量在注解的方式:
@Select({ "" + "SELECT " + "orders.orderId, product_sku.productId, product_sku.skuName, " + "orders.number, orders.orderPrice,product_sku.skuPrice, " + "orders.orderCreate, customer.mobile, shop_keeper.mobile1 as shopKeeperMobile, " + "shop.name, orders.shopId, shop.address, shop.cityCode " + "FROM orders, product_sku, customer, shop, shop_keeper " + "WHERE orders.skuId=product_sku.skuId " + "AND orders.customerId = customer.customerId " + "" + "AND orders.orderStatus IN " + " " + "#{status} " + "" + "" + "AND orders.shopId = shop.shopId " + "AND orders.shopId = shop_keeper.shopId " + "ORDER BY customer.mobile DESC, orders.shopId DESC ,orders.orderCreate DESC" + "" }) List