Mybatis的批量操作
Posted 林被熊烟岛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis的批量操作相关的知识,希望对你有一定的参考价值。
批量在XML查询
collection: 指定要遍历的集合(三种情况 list,array,map) !!!!在这种使用注解sql的情况下,这里请填写mapper方法中集合的名称
item:将当前遍历出的元素赋值给指定的变量 (相当于for循环中的i)
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
index:索引。遍历list的时候是index就是索引,item就是当前值
#{变量名}就能取出变量的值也就是当前遍历出的元素
List在xml的方式
List<Integer> list = new ArrayList<Integer>(); 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的方式
List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); HashMap<String,Object> parames = new HashMap<String,Object>(); 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的方式
List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); //TestModel 的实现 public class TestModel { private List<Integer> searchList; public List<Integer> getSearchList() { return searchList; } public void setSearchList(List<Integer> 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>
批量在注解的方式:
@Select({ "<script>" + "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 " + "<if test=‘orderStatus != null‘>" + "AND orders.orderStatus IN " + "<foreach item=‘status‘ index=‘index‘ collection=‘orderStatus‘ open=‘(‘ separator=‘,‘ close=‘)‘>" + "#{status} " + "</foreach>" + "</if>" + "AND orders.shopId = shop.shopId " + "AND orders.shopId = shop_keeper.shopId " + "ORDER BY customer.mobile DESC, orders.shopId DESC ,orders.orderCreate DESC" + "</script>" }) List<Map<String, Object>> selectOrders(@Param(value="orderStatus")List<Short> orderStatus); @Update({ "<script>" + "UPDATE orders SET orderStatus = #{orderStatus} WHERE orderId in " + "<foreach item=‘item‘ index=‘index‘ collection=‘orderId‘ open=‘(‘ separator=‘,‘ close=‘)‘>" + "#{item}" + "</foreach>" +"</script>" }) int updateOrderStatus(@Param("orderStatus") Short orderStatus,@Param("orderId") String[] orderList);
以上是关于Mybatis的批量操作的主要内容,如果未能解决你的问题,请参考以下文章