码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis的批量操作

时间:2020-04-24 01:21:46      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:stat   date   and   之间   mybatis   注解   mobile   return   play   

批量在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的批量操作

标签:stat   date   and   之间   mybatis   注解   mobile   return   play   

原文地址:https://www.cnblogs.com/linhongwenBlog/p/12764754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!